| 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 : URLRequestHttpJob(request, |
| 55 request->context()->http_user_agent_settings()) {} | 63 request->context()->network_delegate(), |
| 64 request->context()->http_user_agent_settings()), |
| 65 use_null_source_stream_(false) {} |
| 66 |
| 56 ~TestURLRequestHttpJob() override {} | 67 ~TestURLRequestHttpJob() override {} |
| 57 | 68 |
| 69 // URLRequestJob implementation: |
| 70 std::unique_ptr<SourceStream> SetUpSourceStream() override { |
| 71 if (use_null_source_stream_) |
| 72 return nullptr; |
| 73 return URLRequestHttpJob::SetUpSourceStream(); |
| 74 } |
| 75 |
| 76 void set_use_null_source_stream(bool use_null_source_stream) { |
| 77 use_null_source_stream_ = use_null_source_stream; |
| 78 } |
| 79 |
| 58 using URLRequestHttpJob::SetPriority; | 80 using URLRequestHttpJob::SetPriority; |
| 59 using URLRequestHttpJob::Start; | 81 using URLRequestHttpJob::Start; |
| 60 using URLRequestHttpJob::Kill; | 82 using URLRequestHttpJob::Kill; |
| 61 using URLRequestHttpJob::priority; | 83 using URLRequestHttpJob::priority; |
| 84 |
| 85 private: |
| 86 bool use_null_source_stream_; |
| 62 }; | 87 }; |
| 63 | 88 |
| 89 class URLRequestHttpJobSetUpSourceTest : public ::testing::Test { |
| 90 protected: |
| 91 URLRequestHttpJobSetUpSourceTest() : context_(true) { |
| 92 test_job_interceptor_ = new TestJobInterceptor(); |
| 93 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| 94 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| 95 context_.set_job_factory(&test_job_factory_); |
| 96 context_.set_client_socket_factory(&socket_factory_); |
| 97 context_.Init(); |
| 98 } |
| 99 |
| 100 MockClientSocketFactory socket_factory_; |
| 101 // |test_job_interceptor_| is owned by |test_job_factory_|. |
| 102 TestJobInterceptor* test_job_interceptor_; |
| 103 URLRequestJobFactoryImpl test_job_factory_; |
| 104 |
| 105 TestURLRequestContext context_; |
| 106 TestDelegate delegate_; |
| 107 }; |
| 108 |
| 109 // Tests that if SetUpSourceStream() returns nullptr, the request fails. |
| 110 TEST_F(URLRequestHttpJobSetUpSourceTest, SetUpSourceFails) { |
| 111 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| 112 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 113 "Content-Length: 12\r\n\r\n"), |
| 114 MockRead("Test Content")}; |
| 115 |
| 116 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 117 arraysize(writes)); |
| 118 socket_factory_.AddSocketDataProvider(&socket_data); |
| 119 |
| 120 std::unique_ptr<URLRequest> request = context_.CreateRequest( |
| 121 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate_); |
| 122 std::unique_ptr<TestURLRequestHttpJob> job( |
| 123 new TestURLRequestHttpJob(request.get())); |
| 124 job->set_use_null_source_stream(true); |
| 125 test_job_interceptor_->set_main_intercept_job(std::move(job)); |
| 126 request->SetPriority(LOW); |
| 127 request->Start(); |
| 128 |
| 129 base::RunLoop().Run(); |
| 130 EXPECT_FALSE(request->status().is_success()); |
| 131 EXPECT_EQ(ERR_CONTENT_DECODING_INIT_FAILED, request->status().error()); |
| 132 } |
| 133 |
| 64 class URLRequestHttpJobTest : public ::testing::Test { | 134 class URLRequestHttpJobTest : public ::testing::Test { |
| 65 protected: | 135 protected: |
| 66 URLRequestHttpJobTest() : context_(true) { | 136 URLRequestHttpJobTest() : context_(true) { |
| 67 context_.set_http_transaction_factory(&network_layer_); | 137 context_.set_http_transaction_factory(&network_layer_); |
| 68 | 138 |
| 69 // The |test_job_factory_| takes ownership of the interceptor. | 139 // The |test_job_factory_| takes ownership of the interceptor. |
| 70 test_job_interceptor_ = new TestJobInterceptor(); | 140 test_job_interceptor_ = new TestJobInterceptor(); |
| 71 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | 141 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| 72 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | 142 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| 73 context_.set_job_factory(&test_job_factory_); | 143 context_.set_job_factory(&test_job_factory_); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 context_->set_client_socket_factory(&socket_factory_); | 198 context_->set_client_socket_factory(&socket_factory_); |
| 129 context_->set_network_delegate(&network_delegate_); | 199 context_->set_network_delegate(&network_delegate_); |
| 130 context_->Init(); | 200 context_->Init(); |
| 131 } | 201 } |
| 132 | 202 |
| 133 MockClientSocketFactory socket_factory_; | 203 MockClientSocketFactory socket_factory_; |
| 134 TestNetworkDelegate network_delegate_; | 204 TestNetworkDelegate network_delegate_; |
| 135 std::unique_ptr<TestURLRequestContext> context_; | 205 std::unique_ptr<TestURLRequestContext> context_; |
| 136 }; | 206 }; |
| 137 | 207 |
| 138 const char kSimpleGetMockWrite[] = | |
| 139 "GET / HTTP/1.1\r\n" | |
| 140 "Host: www.example.com\r\n" | |
| 141 "Connection: keep-alive\r\n" | |
| 142 "User-Agent:\r\n" | |
| 143 "Accept-Encoding: gzip, deflate\r\n" | |
| 144 "Accept-Language: en-us,fr\r\n\r\n"; | |
| 145 | |
| 146 TEST_F(URLRequestHttpJobWithMockSocketsTest, | 208 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 147 TestContentLengthSuccessfulRequest) { | 209 TestContentLengthSuccessfulRequest) { |
| 148 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; | 210 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| 149 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | 211 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 150 "Content-Length: 12\r\n\r\n"), | 212 "Content-Length: 12\r\n\r\n"), |
| 151 MockRead("Test Content")}; | 213 MockRead("Test Content")}; |
| 152 | 214 |
| 153 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, | 215 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 154 arraysize(writes)); | 216 arraysize(writes)); |
| 155 socket_factory_.AddSocketDataProvider(&socket_data); | 217 socket_factory_.AddSocketDataProvider(&socket_data); |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 &delegate_)) { | 750 &delegate_)) { |
| 689 } | 751 } |
| 690 | 752 |
| 691 TestDelegate delegate_; | 753 TestDelegate delegate_; |
| 692 std::unique_ptr<URLRequest> req_; | 754 std::unique_ptr<URLRequest> req_; |
| 693 }; | 755 }; |
| 694 | 756 |
| 695 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { | 757 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { |
| 696 public: | 758 public: |
| 697 // GoogleMock does not appear to play nicely with move-only types like | 759 // GoogleMock does not appear to play nicely with move-only types like |
| 698 // scoped_ptr, so this forwarding method acts as a workaround. | 760 // std::unique_ptr, so this forwarding method acts as a workaround. |
| 699 WebSocketHandshakeStreamBase* CreateBasicStream( | 761 WebSocketHandshakeStreamBase* CreateBasicStream( |
| 700 std::unique_ptr<ClientSocketHandle> connection, | 762 std::unique_ptr<ClientSocketHandle> connection, |
| 701 bool using_proxy) override { | 763 bool using_proxy) override { |
| 702 // Discard the arguments since we don't need them anyway. | 764 // Discard the arguments since we don't need them anyway. |
| 703 return CreateBasicStreamMock(); | 765 return CreateBasicStreamMock(); |
| 704 } | 766 } |
| 705 | 767 |
| 706 MOCK_METHOD0(CreateBasicStreamMock, | 768 MOCK_METHOD0(CreateBasicStreamMock, |
| 707 WebSocketHandshakeStreamBase*()); | 769 WebSocketHandshakeStreamBase*()); |
| 708 | 770 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 base::RunLoop().RunUntilIdle(); | 880 base::RunLoop().RunUntilIdle(); |
| 819 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 881 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 820 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 882 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 821 } | 883 } |
| 822 | 884 |
| 823 #endif // defined(ENABLE_WEBSOCKETS) | 885 #endif // defined(ENABLE_WEBSOCKETS) |
| 824 | 886 |
| 825 } // namespace | 887 } // namespace |
| 826 | 888 |
| 827 } // namespace net | 889 } // namespace net |
| OLD | NEW |