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/task_scheduler/task_scheduler.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" |
21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 20 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
22 #include "net/url_request/url_request_job.h" | 21 #include "net/url_request/url_request_job.h" |
23 #include "net/url_request/url_request_job_factory.h" | 22 #include "net/url_request/url_request_job_factory.h" |
24 #include "net/url_request/url_request_job_factory_impl.h" | 23 #include "net/url_request/url_request_job_factory_impl.h" |
25 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
28 | 27 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 104 } |
106 | 105 |
107 ~SimpleJobProtocolHandler() override {} | 106 ~SimpleJobProtocolHandler() override {} |
108 | 107 |
109 private: | 108 private: |
110 DISALLOW_COPY_AND_ASSIGN(SimpleJobProtocolHandler); | 109 DISALLOW_COPY_AND_ASSIGN(SimpleJobProtocolHandler); |
111 }; | 110 }; |
112 | 111 |
113 class URLRequestSimpleJobTest : public ::testing::Test { | 112 class URLRequestSimpleJobTest : public ::testing::Test { |
114 public: | 113 public: |
115 URLRequestSimpleJobTest() | 114 URLRequestSimpleJobTest() : context_(true) { |
116 : context_(true), scoped_task_scheduler_(base::MessageLoop::current()) { | |
117 job_factory_.SetProtocolHandler( | 115 job_factory_.SetProtocolHandler( |
118 "data", base::MakeUnique<SimpleJobProtocolHandler>()); | 116 "data", base::MakeUnique<SimpleJobProtocolHandler>()); |
119 context_.set_job_factory(&job_factory_); | 117 context_.set_job_factory(&job_factory_); |
120 context_.Init(); | 118 context_.Init(); |
121 | 119 |
122 request_ = context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, | 120 request_ = context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, |
123 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); | 121 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
124 } | 122 } |
125 | 123 |
126 void StartRequest(const HttpRequestHeaders* headers) { | 124 void StartRequest(const HttpRequestHeaders* headers) { |
127 if (headers) | 125 if (headers) |
128 request_->SetExtraRequestHeaders(*headers); | 126 request_->SetExtraRequestHeaders(*headers); |
129 request_->Start(); | 127 request_->Start(); |
130 | 128 |
131 EXPECT_TRUE(request_->is_pending()); | 129 EXPECT_TRUE(request_->is_pending()); |
132 base::RunLoop().Run(); | 130 base::RunLoop().Run(); |
133 EXPECT_FALSE(request_->is_pending()); | 131 EXPECT_FALSE(request_->is_pending()); |
134 } | 132 } |
135 | 133 |
136 protected: | 134 protected: |
137 TestURLRequestContext context_; | 135 TestURLRequestContext context_; |
138 URLRequestJobFactoryImpl job_factory_; | 136 URLRequestJobFactoryImpl job_factory_; |
139 TestDelegate delegate_; | 137 TestDelegate delegate_; |
140 std::unique_ptr<URLRequest> request_; | 138 std::unique_ptr<URLRequest> request_; |
141 | 139 |
142 private: | 140 private: |
143 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(URLRequestSimpleJobTest); | 141 DISALLOW_COPY_AND_ASSIGN(URLRequestSimpleJobTest); |
146 }; | 142 }; |
147 | 143 |
148 } // namespace | 144 } // namespace |
149 | 145 |
150 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 146 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
151 StartRequest(NULL); | 147 StartRequest(NULL); |
152 EXPECT_THAT(delegate_.request_status(), IsOk()); | 148 EXPECT_THAT(delegate_.request_status(), IsOk()); |
153 EXPECT_EQ(kTestData, delegate_.data_received()); | 149 EXPECT_EQ(kTestData, delegate_.data_received()); |
154 } | 150 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 212 } |
217 | 213 |
218 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 214 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
219 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 215 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
220 request_ = | 216 request_ = |
221 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 217 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
222 &cancel_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); | 218 &cancel_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
223 request_->Start(); | 219 request_->Start(); |
224 cancel_delegate.WaitUntilHeadersReceived(); | 220 cancel_delegate.WaitUntilHeadersReceived(); |
225 | 221 |
226 // Run ScopedTaskScheduler tasks. | 222 // Run TaskScheduler tasks and their reply on the main thread. |
| 223 base::TaskScheduler::GetInstance()->FlushForTesting(); |
227 base::RunLoop().RunUntilIdle(); | 224 base::RunLoop().RunUntilIdle(); |
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 |