Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / test / net / url_request_abort_on_end_job.cc
blobf7ff5a9dd84bf0f1e759e62211fddbbb74fb0035
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 // This class simulates what wininet does when a dns lookup fails.
6 #include <algorithm>
7 #include <cstring>
9 #include "base/compiler_specific.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/test/net/url_request_abort_on_end_job.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_filter.h"
21 #include "net/url_request/url_request_interceptor.h"
22 #include "net/url_request/url_request_status.h"
24 namespace content {
25 namespace {
27 const char kPageContent[] = "some data\r\n";
29 class Interceptor : public net::URLRequestInterceptor {
30 public:
31 Interceptor() {}
32 ~Interceptor() override {}
34 // URLRequestInterceptor implementation:
35 net::URLRequestJob* MaybeInterceptRequest(
36 net::URLRequest* request,
37 net::NetworkDelegate* network_delegate) const override {
38 DCHECK_CURRENTLY_ON(BrowserThread::IO);
39 return new URLRequestAbortOnEndJob(request, network_delegate);
42 private:
43 DISALLOW_COPY_AND_ASSIGN(Interceptor);
46 void AddUrlHandlerOnIOThread() {
47 DCHECK_CURRENTLY_ON(BrowserThread::IO);
48 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
49 filter->AddUrlInterceptor(
50 GURL(URLRequestAbortOnEndJob::k400AbortOnEndUrl),
51 scoped_ptr<net::URLRequestInterceptor>(new Interceptor()));
54 } // anonymous namespace
56 const char URLRequestAbortOnEndJob::k400AbortOnEndUrl[] =
57 "http://url.handled.by.abort.on.end/400";
59 // static
60 void URLRequestAbortOnEndJob::AddUrlHandler() {
61 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
62 base::Bind(AddUrlHandlerOnIOThread));
65 // Private const version.
66 void URLRequestAbortOnEndJob::GetResponseInfoConst(
67 net::HttpResponseInfo* info) const {
68 // Send back mock headers.
69 std::string raw_headers;
70 if (base::LowerCaseEqualsASCII(k400AbortOnEndUrl,
71 request_->url().spec().c_str())) {
72 raw_headers.append(
73 "HTTP/1.1 400 This is not OK\n"
74 "Content-type: text/plain\n");
75 } else {
76 NOTREACHED();
78 // ParseRawHeaders expects \0 to end each header line.
79 base::ReplaceSubstringsAfterOffset(
80 &raw_headers, 0, "\n", base::StringPiece("\0", 1));
81 info->headers = new net::HttpResponseHeaders(raw_headers);
84 URLRequestAbortOnEndJob::URLRequestAbortOnEndJob(
85 net::URLRequest* request, net::NetworkDelegate* network_delegate)
86 : URLRequestJob(request, network_delegate),
87 sent_data_(false),
88 weak_factory_(this) {
91 URLRequestAbortOnEndJob::~URLRequestAbortOnEndJob() {
94 void URLRequestAbortOnEndJob::GetResponseInfo(net::HttpResponseInfo* info) {
95 GetResponseInfoConst(info);
98 bool URLRequestAbortOnEndJob::GetMimeType(std::string* mime_type) const {
99 net::HttpResponseInfo info;
100 GetResponseInfoConst(&info);
101 return info.headers.get() && info.headers->GetMimeType(mime_type);
104 void URLRequestAbortOnEndJob::StartAsync() {
105 NotifyHeadersComplete();
108 void URLRequestAbortOnEndJob::Start() {
109 base::ThreadTaskRunnerHandle::Get()->PostTask(
110 FROM_HERE, base::Bind(&URLRequestAbortOnEndJob::StartAsync,
111 weak_factory_.GetWeakPtr()));
114 bool URLRequestAbortOnEndJob::ReadRawData(net::IOBuffer* buf,
115 const int max_bytes,
116 int* bytes_read) {
117 if (!sent_data_) {
118 *bytes_read = std::min(size_t(max_bytes), sizeof(kPageContent));
119 std::memcpy(buf->data(), kPageContent, *bytes_read);
120 sent_data_ = true;
121 return true;
124 SetStatus(net::URLRequestStatus(net::URLRequestStatus::FAILED,
125 net::ERR_CONNECTION_ABORTED));
126 *bytes_read = -1;
127 return false;
130 } // namespace content