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.
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"
27 const char kPageContent
[] = "some data\r\n";
29 class Interceptor
: public net::URLRequestInterceptor
{
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
);
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";
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())) {
73 "HTTP/1.1 400 This is not OK\n"
74 "Content-type: text/plain\n");
78 // ParseRawHeaders expects \0 to end each header line.
79 ReplaceSubstringsAfterOffset(&raw_headers
, 0, "\n", std::string("\0", 1));
80 info
->headers
= new net::HttpResponseHeaders(raw_headers
);
83 URLRequestAbortOnEndJob::URLRequestAbortOnEndJob(
84 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
)
85 : URLRequestJob(request
, network_delegate
),
90 URLRequestAbortOnEndJob::~URLRequestAbortOnEndJob() {
93 void URLRequestAbortOnEndJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
94 GetResponseInfoConst(info
);
97 bool URLRequestAbortOnEndJob::GetMimeType(std::string
* mime_type
) const {
98 net::HttpResponseInfo info
;
99 GetResponseInfoConst(&info
);
100 return info
.headers
.get() && info
.headers
->GetMimeType(mime_type
);
103 void URLRequestAbortOnEndJob::StartAsync() {
104 NotifyHeadersComplete();
107 void URLRequestAbortOnEndJob::Start() {
108 base::ThreadTaskRunnerHandle::Get()->PostTask(
109 FROM_HERE
, base::Bind(&URLRequestAbortOnEndJob::StartAsync
,
110 weak_factory_
.GetWeakPtr()));
113 bool URLRequestAbortOnEndJob::ReadRawData(net::IOBuffer
* buf
,
117 *bytes_read
= std::min(size_t(max_bytes
), sizeof(kPageContent
));
118 std::memcpy(buf
->data(), kPageContent
, *bytes_read
);
123 SetStatus(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
124 net::ERR_CONNECTION_ABORTED
));
129 } // namespace content