OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <cstddef> | 9 #include <cstddef> |
10 #include <memory> | 10 #include <memory> |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 using net::test::IsError; | 41 using net::test::IsError; |
42 using net::test::IsOk; | 42 using net::test::IsOk; |
43 | 43 |
44 namespace net { | 44 namespace net { |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 using ::testing::Return; | 48 using ::testing::Return; |
49 | 49 |
| 50 const char kSimpleGetMockWrite[] = |
| 51 "GET / HTTP/1.1\r\n" |
| 52 "Host: www.example.com\r\n" |
| 53 "Connection: keep-alive\r\n" |
| 54 "User-Agent:\r\n" |
| 55 "Accept-Encoding: gzip, deflate\r\n" |
| 56 "Accept-Language: en-us,fr\r\n\r\n"; |
| 57 |
50 // Inherit from URLRequestHttpJob to expose the priority and some | 58 // Inherit from URLRequestHttpJob to expose the priority and some |
51 // other hidden functions. | 59 // other hidden functions. |
52 class TestURLRequestHttpJob : public URLRequestHttpJob { | 60 class TestURLRequestHttpJob : public URLRequestHttpJob { |
53 public: | 61 public: |
54 explicit TestURLRequestHttpJob(URLRequest* request) | 62 explicit TestURLRequestHttpJob(URLRequest* request) |
55 : URLRequestHttpJob(request, request->context()->network_delegate(), | 63 : URLRequestHttpJob(request, |
56 request->context()->http_user_agent_settings()) {} | 64 request->context()->network_delegate(), |
| 65 request->context()->http_user_agent_settings()), |
| 66 use_null_source_stream_(false) {} |
| 67 |
57 ~TestURLRequestHttpJob() override {} | 68 ~TestURLRequestHttpJob() override {} |
58 | 69 |
| 70 // URLRequestJob implementation: |
| 71 std::unique_ptr<SourceStream> SetUpSourceStream() override { |
| 72 if (use_null_source_stream_) |
| 73 return nullptr; |
| 74 return URLRequestHttpJob::SetUpSourceStream(); |
| 75 } |
| 76 |
| 77 void set_use_null_source_stream(bool use_null_source_stream) { |
| 78 use_null_source_stream_ = use_null_source_stream; |
| 79 } |
| 80 |
59 using URLRequestHttpJob::SetPriority; | 81 using URLRequestHttpJob::SetPriority; |
60 using URLRequestHttpJob::Start; | 82 using URLRequestHttpJob::Start; |
61 using URLRequestHttpJob::Kill; | 83 using URLRequestHttpJob::Kill; |
62 using URLRequestHttpJob::priority; | 84 using URLRequestHttpJob::priority; |
| 85 |
| 86 private: |
| 87 bool use_null_source_stream_; |
63 }; | 88 }; |
64 | 89 |
| 90 class URLRequestHttpJobSetUpSourceTest : public ::testing::Test { |
| 91 protected: |
| 92 URLRequestHttpJobSetUpSourceTest() : context_(true) { |
| 93 test_job_interceptor_ = new TestJobInterceptor(); |
| 94 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| 95 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| 96 context_.set_job_factory(&test_job_factory_); |
| 97 context_.set_client_socket_factory(&socket_factory_); |
| 98 context_.Init(); |
| 99 } |
| 100 |
| 101 MockClientSocketFactory socket_factory_; |
| 102 // |test_job_interceptor_| is owned by |test_job_factory_|. |
| 103 TestJobInterceptor* test_job_interceptor_; |
| 104 URLRequestJobFactoryImpl test_job_factory_; |
| 105 |
| 106 TestURLRequestContext context_; |
| 107 TestDelegate delegate_; |
| 108 }; |
| 109 |
| 110 // Tests that if SetUpSourceStream() returns nullptr, the request fails. |
| 111 TEST_F(URLRequestHttpJobSetUpSourceTest, SetUpSourceFails) { |
| 112 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| 113 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 114 "Content-Length: 12\r\n\r\n"), |
| 115 MockRead("Test Content")}; |
| 116 |
| 117 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 118 arraysize(writes)); |
| 119 socket_factory_.AddSocketDataProvider(&socket_data); |
| 120 |
| 121 std::unique_ptr<URLRequest> request = context_.CreateRequest( |
| 122 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate_); |
| 123 std::unique_ptr<TestURLRequestHttpJob> job( |
| 124 new TestURLRequestHttpJob(request.get())); |
| 125 job->set_use_null_source_stream(true); |
| 126 test_job_interceptor_->set_main_intercept_job(std::move(job)); |
| 127 request->SetPriority(LOW); |
| 128 request->Start(); |
| 129 |
| 130 base::RunLoop().Run(); |
| 131 EXPECT_FALSE(request->status().is_success()); |
| 132 EXPECT_EQ(ERR_CONTENT_DECODING_INIT_FAILED, request->status().error()); |
| 133 } |
| 134 |
65 class URLRequestHttpJobTest : public ::testing::Test { | 135 class URLRequestHttpJobTest : public ::testing::Test { |
66 protected: | 136 protected: |
67 URLRequestHttpJobTest() : context_(true) { | 137 URLRequestHttpJobTest() : context_(true) { |
68 context_.set_http_transaction_factory(&network_layer_); | 138 context_.set_http_transaction_factory(&network_layer_); |
69 | 139 |
70 // The |test_job_factory_| takes ownership of the interceptor. | 140 // The |test_job_factory_| takes ownership of the interceptor. |
71 test_job_interceptor_ = new TestJobInterceptor(); | 141 test_job_interceptor_ = new TestJobInterceptor(); |
72 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | 142 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
73 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | 143 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
74 context_.set_job_factory(&test_job_factory_); | 144 context_.set_job_factory(&test_job_factory_); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 context_->set_client_socket_factory(&socket_factory_); | 199 context_->set_client_socket_factory(&socket_factory_); |
130 context_->set_network_delegate(&network_delegate_); | 200 context_->set_network_delegate(&network_delegate_); |
131 context_->Init(); | 201 context_->Init(); |
132 } | 202 } |
133 | 203 |
134 MockClientSocketFactory socket_factory_; | 204 MockClientSocketFactory socket_factory_; |
135 TestNetworkDelegate network_delegate_; | 205 TestNetworkDelegate network_delegate_; |
136 std::unique_ptr<TestURLRequestContext> context_; | 206 std::unique_ptr<TestURLRequestContext> context_; |
137 }; | 207 }; |
138 | 208 |
139 const char kSimpleGetMockWrite[] = | |
140 "GET / HTTP/1.1\r\n" | |
141 "Host: www.example.com\r\n" | |
142 "Connection: keep-alive\r\n" | |
143 "User-Agent:\r\n" | |
144 "Accept-Encoding: gzip, deflate\r\n" | |
145 "Accept-Language: en-us,fr\r\n\r\n"; | |
146 | |
147 TEST_F(URLRequestHttpJobWithMockSocketsTest, | 209 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
148 TestContentLengthSuccessfulRequest) { | 210 TestContentLengthSuccessfulRequest) { |
149 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; | 211 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
150 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | 212 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
151 "Content-Length: 12\r\n\r\n"), | 213 "Content-Length: 12\r\n\r\n"), |
152 MockRead("Test Content")}; | 214 MockRead("Test Content")}; |
153 | 215 |
154 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, | 216 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
155 arraysize(writes)); | 217 arraysize(writes)); |
156 socket_factory_.AddSocketDataProvider(&socket_data); | 218 socket_factory_.AddSocketDataProvider(&socket_data); |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 &delegate_)) { | 876 &delegate_)) { |
815 } | 877 } |
816 | 878 |
817 TestDelegate delegate_; | 879 TestDelegate delegate_; |
818 std::unique_ptr<URLRequest> req_; | 880 std::unique_ptr<URLRequest> req_; |
819 }; | 881 }; |
820 | 882 |
821 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { | 883 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { |
822 public: | 884 public: |
823 // GoogleMock does not appear to play nicely with move-only types like | 885 // GoogleMock does not appear to play nicely with move-only types like |
824 // scoped_ptr, so this forwarding method acts as a workaround. | 886 // std::unique_ptr, so this forwarding method acts as a workaround. |
825 WebSocketHandshakeStreamBase* CreateBasicStream( | 887 WebSocketHandshakeStreamBase* CreateBasicStream( |
826 std::unique_ptr<ClientSocketHandle> connection, | 888 std::unique_ptr<ClientSocketHandle> connection, |
827 bool using_proxy) override { | 889 bool using_proxy) override { |
828 // Discard the arguments since we don't need them anyway. | 890 // Discard the arguments since we don't need them anyway. |
829 return CreateBasicStreamMock(); | 891 return CreateBasicStreamMock(); |
830 } | 892 } |
831 | 893 |
832 MOCK_METHOD0(CreateBasicStreamMock, | 894 MOCK_METHOD0(CreateBasicStreamMock, |
833 WebSocketHandshakeStreamBase*()); | 895 WebSocketHandshakeStreamBase*()); |
834 | 896 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 base::RunLoop().RunUntilIdle(); | 1002 base::RunLoop().RunUntilIdle(); |
941 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); | 1003 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); |
942 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1004 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
943 } | 1005 } |
944 | 1006 |
945 #endif // defined(ENABLE_WEBSOCKETS) | 1007 #endif // defined(ENABLE_WEBSOCKETS) |
946 | 1008 |
947 } // namespace | 1009 } // namespace |
948 | 1010 |
949 } // namespace net | 1011 } // namespace net |
OLD | NEW |