Chromium Code Reviews| Index: net/url_request/url_request_http_job_unittest.cc |
| diff --git a/net/url_request/url_request_http_job_unittest.cc b/net/url_request/url_request_http_job_unittest.cc |
| index e820f4fc92dc6af93148195a8b476fb648590e4a..91ab9ede9bfd8629d19bb8f8ad94f3c5101bad1e 100644 |
| --- a/net/url_request/url_request_http_job_unittest.cc |
| +++ b/net/url_request/url_request_http_job_unittest.cc |
| @@ -46,21 +46,86 @@ namespace { |
| using ::testing::Return; |
| +const char kSimpleGetMockWrite[] = |
| + "GET / HTTP/1.1\r\n" |
| + "Host: www.example.com\r\n" |
| + "Connection: keep-alive\r\n" |
| + "User-Agent:\r\n" |
| + "Accept-Encoding: gzip, deflate\r\n" |
| + "Accept-Language: en-us,fr\r\n\r\n"; |
| + |
| // Inherit from URLRequestHttpJob to expose the priority and some |
| // other hidden functions. |
| class TestURLRequestHttpJob : public URLRequestHttpJob { |
| public: |
| explicit TestURLRequestHttpJob(URLRequest* request) |
| - : URLRequestHttpJob(request, request->context()->network_delegate(), |
| - request->context()->http_user_agent_settings()) {} |
| + : TestURLRequestHttpJob(request, false) {} |
| + 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.
|
| + : URLRequestHttpJob(request, |
| + request->context()->network_delegate(), |
| + request->context()->http_user_agent_settings()), |
| + use_null_source_(use_null_source) {} |
| + |
| ~TestURLRequestHttpJob() override {} |
| + // URLRequestJob implementation: |
| + std::unique_ptr<StreamSource> SetupSource() override { |
| + if (use_null_source_) |
| + return nullptr; |
| + return URLRequestHttpJob::SetupSource(); |
| + } |
| + |
| using URLRequestHttpJob::SetPriority; |
| using URLRequestHttpJob::Start; |
| using URLRequestHttpJob::Kill; |
| using URLRequestHttpJob::priority; |
| + |
| + private: |
| + const bool use_null_source_; |
| +}; |
| + |
| +class URLRequestHttpJobSetupSourceTest : public ::testing::Test { |
| + protected: |
| + URLRequestHttpJobSetupSourceTest() : context_(true) { |
| + test_job_interceptor_ = new TestJobInterceptor(); |
| + EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| + url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| + context_.set_job_factory(&test_job_factory_); |
| + context_.set_client_socket_factory(&socket_factory_); |
| + context_.Init(); |
| + } |
| + |
| + MockClientSocketFactory socket_factory_; |
| + // |test_job_interceptor_| is owned by |test_job_factory_|. |
| + TestJobInterceptor* test_job_interceptor_; |
| + URLRequestJobFactoryImpl test_job_factory_; |
| + |
| + TestURLRequestContext context_; |
| + TestDelegate delegate_; |
| }; |
| +// Tests that if SetupSource() returns nullptr, the request fails. |
| +TEST_F(URLRequestHttpJobSetupSourceTest, SetupSourceFails) { |
| + MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| + MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| + "Content-Length: 12\r\n\r\n"), |
| + MockRead("Test Content")}; |
| + |
| + StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| + arraysize(writes)); |
| + socket_factory_.AddSocketDataProvider(&socket_data); |
| + |
| + std::unique_ptr<URLRequest> request = context_.CreateRequest( |
| + GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate_); |
| + test_job_interceptor_->set_main_intercept_job(base::WrapUnique( |
| + new TestURLRequestHttpJob(request.get(), /*use_null_source=*/false))); |
| + request->SetPriority(LOW); |
| + request->Start(); |
| + |
| + base::RunLoop().Run(); |
| + EXPECT_TRUE(request->status().is_success()); |
| +} |
| + |
| class URLRequestHttpJobTest : public ::testing::Test { |
| protected: |
| URLRequestHttpJobTest() : context_(true) { |
| @@ -137,14 +202,6 @@ class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test { |
| std::unique_ptr<TestURLRequestContext> context_; |
| }; |
| -const char kSimpleGetMockWrite[] = |
| - "GET / HTTP/1.1\r\n" |
| - "Host: www.example.com\r\n" |
| - "Connection: keep-alive\r\n" |
| - "User-Agent:\r\n" |
| - "Accept-Encoding: gzip, deflate\r\n" |
| - "Accept-Language: en-us,fr\r\n\r\n"; |
| - |
| TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| TestContentLengthSuccessfulRequest) { |
| MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| @@ -845,7 +902,7 @@ class URLRequestHttpJobWebSocketTest |
| class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { |
| public: |
| // GoogleMock does not appear to play nicely with move-only types like |
| - // scoped_ptr, so this forwarding method acts as a workaround. |
| + // std::unique_ptr, so this forwarding method acts as a workaround. |
| WebSocketHandshakeStreamBase* CreateBasicStream( |
| std::unique_ptr<ClientSocketHandle> connection, |
| bool using_proxy) override { |