OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "net/test/url_request/url_request_hanging_read_job.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_util.h" |
| 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_filter.h" |
| 18 |
| 19 namespace net { |
| 20 namespace { |
| 21 |
| 22 const char kMockHostname[] = "mock.hanging.read"; |
| 23 |
| 24 GURL GetMockUrl(const std::string& scheme, const std::string& hostname) { |
| 25 return GURL(scheme + "://" + hostname + "/"); |
| 26 } |
| 27 |
| 28 class MockJobInterceptor : public URLRequestInterceptor { |
| 29 public: |
| 30 MockJobInterceptor() {} |
| 31 ~MockJobInterceptor() override {} |
| 32 |
| 33 // URLRequestInterceptor implementation |
| 34 URLRequestJob* MaybeInterceptRequest( |
| 35 URLRequest* request, |
| 36 NetworkDelegate* network_delegate) const override { |
| 37 return new URLRequestHangingReadJob(request, network_delegate); |
| 38 } |
| 39 |
| 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 URLRequestHangingReadJob::URLRequestHangingReadJob( |
| 47 URLRequest* request, |
| 48 NetworkDelegate* network_delegate) |
| 49 : URLRequestJob(request, network_delegate), |
| 50 content_length_(10), // non-zero content-length |
| 51 weak_factory_(this) {} |
| 52 |
| 53 void URLRequestHangingReadJob::Start() { |
| 54 // Start reading asynchronously so that all error reporting and data |
| 55 // callbacks happen as they would for network requests. |
| 56 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 57 FROM_HERE, base::Bind(&URLRequestHangingReadJob::StartAsync, |
| 58 weak_factory_.GetWeakPtr())); |
| 59 } |
| 60 |
| 61 URLRequestHangingReadJob::~URLRequestHangingReadJob() {} |
| 62 |
| 63 int URLRequestHangingReadJob::ReadRawData(IOBuffer* buf, int buf_size) { |
| 64 // Make read hang. It never completes. |
| 65 return ERR_IO_PENDING; |
| 66 } |
| 67 |
| 68 int URLRequestHangingReadJob::GetResponseCode() const { |
| 69 HttpResponseInfo info; |
| 70 GetResponseInfoConst(&info); |
| 71 return info.headers->response_code(); |
| 72 } |
| 73 |
| 74 // Public virtual version. |
| 75 void URLRequestHangingReadJob::GetResponseInfo(HttpResponseInfo* info) { |
| 76 // Forward to private const version. |
| 77 GetResponseInfoConst(info); |
| 78 } |
| 79 |
| 80 // Private const version. |
| 81 void URLRequestHangingReadJob::GetResponseInfoConst( |
| 82 HttpResponseInfo* info) const { |
| 83 // Send back mock headers. |
| 84 std::string raw_headers; |
| 85 raw_headers.append( |
| 86 "HTTP/1.1 200 OK\n" |
| 87 "Content-type: text/plain\n"); |
| 88 raw_headers.append( |
| 89 base::StringPrintf("Content-Length: %1d\n", content_length_)); |
| 90 info->headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders( |
| 91 raw_headers.c_str(), static_cast<int>(raw_headers.length()))); |
| 92 } |
| 93 |
| 94 void URLRequestHangingReadJob::StartAsync() { |
| 95 set_expected_content_size(content_length_); |
| 96 NotifyHeadersComplete(); |
| 97 } |
| 98 |
| 99 // static |
| 100 void URLRequestHangingReadJob::AddUrlHandler() { |
| 101 // Add |hostname| to URLRequestFilter for HTTP and HTTPS. |
| 102 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
| 103 filter->AddHostnameInterceptor("http", kMockHostname, |
| 104 base::WrapUnique(new MockJobInterceptor())); |
| 105 filter->AddHostnameInterceptor("https", kMockHostname, |
| 106 base::WrapUnique(new MockJobInterceptor())); |
| 107 } |
| 108 |
| 109 // static |
| 110 GURL URLRequestHangingReadJob::GetMockHttpUrl() { |
| 111 return GetMockUrl("http", kMockHostname); |
| 112 } |
| 113 |
| 114 // static |
| 115 GURL URLRequestHangingReadJob::GetMockHttpsUrl() { |
| 116 return GetMockUrl("https", kMockHostname); |
| 117 } |
| 118 |
| 119 } // namespace net |
OLD | NEW |