OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_simple_job.h" | 5 #include "net/url_request/url_request_simple_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
15 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
18 #include "base/test/scoped_task_scheduler.h" | 17 #include "base/test/scoped_task_environment.h" |
19 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
20 #include "net/test/gtest_util.h" | 19 #include "net/test/gtest_util.h" |
20 #include "net/test/net_test_suite.h" | |
21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
22 #include "net/url_request/url_request_job.h" | 22 #include "net/url_request/url_request_job.h" |
23 #include "net/url_request/url_request_job_factory.h" | 23 #include "net/url_request/url_request_job_factory.h" |
24 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
25 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
29 using net::test::IsError; | 29 using net::test::IsError; |
30 using net::test::IsOk; | 30 using net::test::IsOk; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 } | 105 } |
106 | 106 |
107 ~SimpleJobProtocolHandler() override {} | 107 ~SimpleJobProtocolHandler() override {} |
108 | 108 |
109 private: | 109 private: |
110 DISALLOW_COPY_AND_ASSIGN(SimpleJobProtocolHandler); | 110 DISALLOW_COPY_AND_ASSIGN(SimpleJobProtocolHandler); |
111 }; | 111 }; |
112 | 112 |
113 class URLRequestSimpleJobTest : public ::testing::Test { | 113 class URLRequestSimpleJobTest : public ::testing::Test { |
114 public: | 114 public: |
115 URLRequestSimpleJobTest() | 115 URLRequestSimpleJobTest() : context_(true) { |
116 : context_(true), scoped_task_scheduler_(base::MessageLoop::current()) { | |
117 job_factory_.SetProtocolHandler( | 116 job_factory_.SetProtocolHandler( |
118 "data", base::MakeUnique<SimpleJobProtocolHandler>()); | 117 "data", base::MakeUnique<SimpleJobProtocolHandler>()); |
119 context_.set_job_factory(&job_factory_); | 118 context_.set_job_factory(&job_factory_); |
120 context_.Init(); | 119 context_.Init(); |
121 | 120 |
122 request_ = context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, | 121 request_ = context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, |
123 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); | 122 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
124 } | 123 } |
125 | 124 |
126 void StartRequest(const HttpRequestHeaders* headers) { | 125 void StartRequest(const HttpRequestHeaders* headers) { |
127 if (headers) | 126 if (headers) |
128 request_->SetExtraRequestHeaders(*headers); | 127 request_->SetExtraRequestHeaders(*headers); |
129 request_->Start(); | 128 request_->Start(); |
130 | 129 |
131 EXPECT_TRUE(request_->is_pending()); | 130 EXPECT_TRUE(request_->is_pending()); |
132 base::RunLoop().Run(); | 131 base::RunLoop().Run(); |
133 EXPECT_FALSE(request_->is_pending()); | 132 EXPECT_FALSE(request_->is_pending()); |
134 } | 133 } |
135 | 134 |
136 protected: | 135 protected: |
137 TestURLRequestContext context_; | 136 TestURLRequestContext context_; |
138 URLRequestJobFactoryImpl job_factory_; | 137 URLRequestJobFactoryImpl job_factory_; |
139 TestDelegate delegate_; | 138 TestDelegate delegate_; |
140 std::unique_ptr<URLRequest> request_; | 139 std::unique_ptr<URLRequest> request_; |
141 | 140 |
142 private: | 141 private: |
143 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(URLRequestSimpleJobTest); | 142 DISALLOW_COPY_AND_ASSIGN(URLRequestSimpleJobTest); |
146 }; | 143 }; |
147 | 144 |
148 } // namespace | 145 } // namespace |
149 | 146 |
150 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 147 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
151 StartRequest(NULL); | 148 StartRequest(NULL); |
152 EXPECT_THAT(delegate_.request_status(), IsOk()); | 149 EXPECT_THAT(delegate_.request_status(), IsOk()); |
153 EXPECT_EQ(kTestData, delegate_.data_received()); | 150 EXPECT_EQ(kTestData, delegate_.data_received()); |
154 } | 151 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 } | 213 } |
217 | 214 |
218 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 215 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
219 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 216 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
220 request_ = | 217 request_ = |
221 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 218 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
222 &cancel_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); | 219 &cancel_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
223 request_->Start(); | 220 request_->Start(); |
224 cancel_delegate.WaitUntilHeadersReceived(); | 221 cancel_delegate.WaitUntilHeadersReceived(); |
225 | 222 |
226 // Run ScopedTaskScheduler tasks. | 223 // Run TaskScheduler tasks and their reply on the main thread. |
227 base::RunLoop().RunUntilIdle(); | 224 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle(); |
mmenke
2017/05/10 15:43:44
Why do only some RunUntilIdle calls need to be swi
fdoray
2017/05/10 16:16:12
This CL removes ScopedTaskScheduler from individua
| |
228 | 225 |
229 EXPECT_THAT(cancel_delegate.request_status(), IsError(ERR_ABORTED)); | 226 EXPECT_THAT(cancel_delegate.request_status(), IsError(ERR_ABORTED)); |
230 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 227 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
231 EXPECT_EQ("", cancel_delegate.data_received()); | 228 EXPECT_EQ("", cancel_delegate.data_received()); |
232 // Destroy the request so it doesn't outlive its delegate. | 229 // Destroy the request so it doesn't outlive its delegate. |
233 request_.reset(); | 230 request_.reset(); |
234 } | 231 } |
235 | 232 |
236 } // namespace net | 233 } // namespace net |
OLD | NEW |