Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 using net::test::IsError; | 40 using net::test::IsError; |
| 41 using net::test::IsOk; | 41 using net::test::IsOk; |
| 42 | 42 |
| 43 namespace net { | 43 namespace net { |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 using ::testing::Return; | 47 using ::testing::Return; |
| 48 | 48 |
| 49 const char kSimpleGetMockWrite[] = | |
| 50 "GET / HTTP/1.1\r\n" | |
| 51 "Host: www.example.com\r\n" | |
| 52 "Connection: keep-alive\r\n" | |
| 53 "User-Agent:\r\n" | |
| 54 "Accept-Encoding: gzip, deflate\r\n" | |
| 55 "Accept-Language: en-us,fr\r\n\r\n"; | |
| 56 | |
| 49 // Inherit from URLRequestHttpJob to expose the priority and some | 57 // Inherit from URLRequestHttpJob to expose the priority and some |
| 50 // other hidden functions. | 58 // other hidden functions. |
| 51 class TestURLRequestHttpJob : public URLRequestHttpJob { | 59 class TestURLRequestHttpJob : public URLRequestHttpJob { |
| 52 public: | 60 public: |
| 53 explicit TestURLRequestHttpJob(URLRequest* request) | 61 explicit TestURLRequestHttpJob(URLRequest* request) |
| 54 : URLRequestHttpJob(request, request->context()->network_delegate(), | 62 : TestURLRequestHttpJob(request, false) {} |
| 55 request->context()->http_user_agent_settings()) {} | 63 TestURLRequestHttpJob(URLRequest* request, bool use_null_source) |
|
mmenke
2016/07/28 18:40:13
May be simpler to make a setter for use_null_sourc
xunjieli
2016/08/01 16:46:23
Done.
| |
| 64 : URLRequestHttpJob(request, | |
| 65 request->context()->network_delegate(), | |
| 66 request->context()->http_user_agent_settings()), | |
| 67 use_null_source_(use_null_source) {} | |
| 68 | |
| 56 ~TestURLRequestHttpJob() override {} | 69 ~TestURLRequestHttpJob() override {} |
| 57 | 70 |
| 71 // URLRequestJob implementation: | |
| 72 std::unique_ptr<StreamSource> SetupSource() override { | |
| 73 if (use_null_source_) | |
| 74 return nullptr; | |
| 75 return URLRequestHttpJob::SetupSource(); | |
| 76 } | |
| 77 | |
| 58 using URLRequestHttpJob::SetPriority; | 78 using URLRequestHttpJob::SetPriority; |
| 59 using URLRequestHttpJob::Start; | 79 using URLRequestHttpJob::Start; |
| 60 using URLRequestHttpJob::Kill; | 80 using URLRequestHttpJob::Kill; |
| 61 using URLRequestHttpJob::priority; | 81 using URLRequestHttpJob::priority; |
| 82 | |
| 83 private: | |
| 84 const bool use_null_source_; | |
| 62 }; | 85 }; |
| 63 | 86 |
| 87 class URLRequestHttpJobSetupSourceTest : public ::testing::Test { | |
| 88 protected: | |
| 89 URLRequestHttpJobSetupSourceTest() : context_(true) { | |
| 90 test_job_interceptor_ = new TestJobInterceptor(); | |
| 91 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | |
| 92 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | |
| 93 context_.set_job_factory(&test_job_factory_); | |
| 94 context_.set_client_socket_factory(&socket_factory_); | |
| 95 context_.Init(); | |
| 96 } | |
| 97 | |
| 98 MockClientSocketFactory socket_factory_; | |
| 99 // |test_job_interceptor_| is owned by |test_job_factory_|. | |
| 100 TestJobInterceptor* test_job_interceptor_; | |
| 101 URLRequestJobFactoryImpl test_job_factory_; | |
| 102 | |
| 103 TestURLRequestContext context_; | |
| 104 TestDelegate delegate_; | |
| 105 }; | |
| 106 | |
| 107 // Tests that if SetupSource() returns nullptr, the request fails. | |
| 108 TEST_F(URLRequestHttpJobSetupSourceTest, SetupSourceFails) { | |
| 109 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; | |
| 110 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | |
| 111 "Content-Length: 12\r\n\r\n"), | |
| 112 MockRead("Test Content")}; | |
| 113 | |
| 114 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, | |
| 115 arraysize(writes)); | |
| 116 socket_factory_.AddSocketDataProvider(&socket_data); | |
| 117 | |
| 118 std::unique_ptr<URLRequest> request = context_.CreateRequest( | |
| 119 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate_); | |
| 120 test_job_interceptor_->set_main_intercept_job(base::WrapUnique( | |
| 121 new TestURLRequestHttpJob(request.get(), /*use_null_source=*/false))); | |
| 122 request->SetPriority(LOW); | |
| 123 request->Start(); | |
| 124 | |
| 125 base::RunLoop().Run(); | |
| 126 EXPECT_TRUE(request->status().is_success()); | |
| 127 } | |
| 128 | |
| 64 class URLRequestHttpJobTest : public ::testing::Test { | 129 class URLRequestHttpJobTest : public ::testing::Test { |
| 65 protected: | 130 protected: |
| 66 URLRequestHttpJobTest() : context_(true) { | 131 URLRequestHttpJobTest() : context_(true) { |
| 67 context_.set_http_transaction_factory(&network_layer_); | 132 context_.set_http_transaction_factory(&network_layer_); |
| 68 | 133 |
| 69 // The |test_job_factory_| takes ownership of the interceptor. | 134 // The |test_job_factory_| takes ownership of the interceptor. |
| 70 test_job_interceptor_ = new TestJobInterceptor(); | 135 test_job_interceptor_ = new TestJobInterceptor(); |
| 71 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | 136 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| 72 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | 137 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| 73 context_.set_job_factory(&test_job_factory_); | 138 context_.set_job_factory(&test_job_factory_); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 context_->set_backoff_manager(&manager_); | 195 context_->set_backoff_manager(&manager_); |
| 131 context_->Init(); | 196 context_->Init(); |
| 132 } | 197 } |
| 133 | 198 |
| 134 MockClientSocketFactory socket_factory_; | 199 MockClientSocketFactory socket_factory_; |
| 135 TestNetworkDelegate network_delegate_; | 200 TestNetworkDelegate network_delegate_; |
| 136 URLRequestBackoffManager manager_; | 201 URLRequestBackoffManager manager_; |
| 137 std::unique_ptr<TestURLRequestContext> context_; | 202 std::unique_ptr<TestURLRequestContext> context_; |
| 138 }; | 203 }; |
| 139 | 204 |
| 140 const char kSimpleGetMockWrite[] = | |
| 141 "GET / HTTP/1.1\r\n" | |
| 142 "Host: www.example.com\r\n" | |
| 143 "Connection: keep-alive\r\n" | |
| 144 "User-Agent:\r\n" | |
| 145 "Accept-Encoding: gzip, deflate\r\n" | |
| 146 "Accept-Language: en-us,fr\r\n\r\n"; | |
| 147 | |
| 148 TEST_F(URLRequestHttpJobWithMockSocketsTest, | 205 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 149 TestContentLengthSuccessfulRequest) { | 206 TestContentLengthSuccessfulRequest) { |
| 150 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; | 207 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| 151 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | 208 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 152 "Content-Length: 12\r\n\r\n"), | 209 "Content-Length: 12\r\n\r\n"), |
| 153 MockRead("Test Content")}; | 210 MockRead("Test Content")}; |
| 154 | 211 |
| 155 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, | 212 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 156 arraysize(writes)); | 213 arraysize(writes)); |
| 157 socket_factory_.AddSocketDataProvider(&socket_data); | 214 socket_factory_.AddSocketDataProvider(&socket_data); |
| (...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 838 &delegate_)) { | 895 &delegate_)) { |
| 839 } | 896 } |
| 840 | 897 |
| 841 TestDelegate delegate_; | 898 TestDelegate delegate_; |
| 842 std::unique_ptr<URLRequest> req_; | 899 std::unique_ptr<URLRequest> req_; |
| 843 }; | 900 }; |
| 844 | 901 |
| 845 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { | 902 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { |
| 846 public: | 903 public: |
| 847 // GoogleMock does not appear to play nicely with move-only types like | 904 // GoogleMock does not appear to play nicely with move-only types like |
| 848 // scoped_ptr, so this forwarding method acts as a workaround. | 905 // std::unique_ptr, so this forwarding method acts as a workaround. |
| 849 WebSocketHandshakeStreamBase* CreateBasicStream( | 906 WebSocketHandshakeStreamBase* CreateBasicStream( |
| 850 std::unique_ptr<ClientSocketHandle> connection, | 907 std::unique_ptr<ClientSocketHandle> connection, |
| 851 bool using_proxy) override { | 908 bool using_proxy) override { |
| 852 // Discard the arguments since we don't need them anyway. | 909 // Discard the arguments since we don't need them anyway. |
| 853 return CreateBasicStreamMock(); | 910 return CreateBasicStreamMock(); |
| 854 } | 911 } |
| 855 | 912 |
| 856 MOCK_METHOD0(CreateBasicStreamMock, | 913 MOCK_METHOD0(CreateBasicStreamMock, |
| 857 WebSocketHandshakeStreamBase*()); | 914 WebSocketHandshakeStreamBase*()); |
| 858 | 915 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 968 base::RunLoop().RunUntilIdle(); | 1025 base::RunLoop().RunUntilIdle(); |
| 969 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 1026 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 970 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1027 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 971 } | 1028 } |
| 972 | 1029 |
| 973 #endif // defined(ENABLE_WEBSOCKETS) | 1030 #endif // defined(ENABLE_WEBSOCKETS) |
| 974 | 1031 |
| 975 } // namespace | 1032 } // namespace |
| 976 | 1033 |
| 977 } // namespace net | 1034 } // namespace net |
| OLD | NEW |