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 "components/captive_portal/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 = net::HttpUtil::AssembleRawHeaders(
18      response_headers.c_str(), static_cast<int>(response_headers.length()));
19  return new net::HttpResponseHeaders(raw_headers);
20}
21
22}  // namespace
23
24namespace captive_portal {
25
26CaptivePortalDetectorTestBase::CaptivePortalDetectorTestBase()
27    : detector_(NULL) {
28}
29
30CaptivePortalDetectorTestBase::~CaptivePortalDetectorTestBase() {
31}
32
33void CaptivePortalDetectorTestBase::SetTime(const base::Time& time) {
34  detector()->set_time_for_testing(time);
35}
36
37void CaptivePortalDetectorTestBase::AdvanceTime(const base::TimeDelta& delta) {
38  detector()->advance_time_for_testing(delta);
39}
40
41bool CaptivePortalDetectorTestBase::FetchingURL() {
42  return detector()->FetchingURL();
43}
44
45void CaptivePortalDetectorTestBase::CompleteURLFetch(
46    int net_error,
47    int status_code,
48    const char* response_headers) {
49  if (net_error != net::OK) {
50    DCHECK(!response_headers);
51    fetcher()->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
52                                                net_error));
53  } else {
54    fetcher()->set_response_code(status_code);
55    if (response_headers) {
56      scoped_refptr<net::HttpResponseHeaders> headers(
57          CreateResponseHeaders(response_headers));
58      DCHECK_EQ(status_code, headers->response_code());
59      fetcher()->set_response_headers(headers);
60    }
61  }
62  detector()->OnURLFetchComplete(fetcher());
63}
64
65}  // namespace captive_portal
66