1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/captive_portal/testing_utils.h"
6
7#include "base/logging.h"
8#include "base/memory/ref_counted.h"
9#include "net/base/net_errors.h"
10#include "net/http/http_response_headers.h"
11#include "net/http/http_util.h"
12
13namespace {
14
15scoped_refptr<net::HttpResponseHeaders> CreateResponseHeaders(
16    const std::string& response_headers) {
17  std::string raw_headers =
18      net::HttpUtil::AssembleRawHeaders(response_headers.c_str(),
19                                        response_headers.length());
20  return new net::HttpResponseHeaders(raw_headers);
21}
22
23}  // namespace
24
25namespace captive_portal {
26
27CaptivePortalDetectorTestBase::CaptivePortalDetectorTestBase()
28    : detector_(NULL) {
29}
30
31CaptivePortalDetectorTestBase::~CaptivePortalDetectorTestBase() {
32}
33
34void CaptivePortalDetectorTestBase::SetTime(const base::Time& time) {
35  detector()->set_time_for_testing(time);
36}
37
38void CaptivePortalDetectorTestBase::AdvanceTime(const base::TimeDelta& delta) {
39  detector()->advance_time_for_testing(delta);
40}
41
42bool CaptivePortalDetectorTestBase::FetchingURL() {
43  return detector()->FetchingURL();
44}
45
46void CaptivePortalDetectorTestBase::CompleteURLFetch(
47    int net_error,
48    int status_code,
49    const char* response_headers) {
50  if (net_error != net::OK) {
51    DCHECK(!response_headers);
52    fetcher()->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
53                                                net_error));
54  } else {
55    fetcher()->set_response_code(status_code);
56    if (response_headers) {
57      scoped_refptr<net::HttpResponseHeaders> headers(
58          CreateResponseHeaders(response_headers));
59      DCHECK_EQ(status_code, headers->response_code());
60      fetcher()->set_response_headers(headers);
61    }
62  }
63  detector()->OnURLFetchComplete(fetcher());
64}
65
66}  // namespace captive_portal
67