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" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/test/scoped_task_scheduler.h" | 18 #include "base/test/scoped_task_scheduler.h" |
19 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
20 #include "net/test/gtest_util.h" | 20 #include "net/test/gtest_util.h" |
| 21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
21 #include "net/url_request/url_request_job.h" | 22 #include "net/url_request/url_request_job.h" |
22 #include "net/url_request/url_request_job_factory.h" | 23 #include "net/url_request/url_request_job_factory.h" |
23 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 using net::test::IsError; | 29 using net::test::IsError; |
29 using net::test::IsOk; | 30 using net::test::IsOk; |
30 | 31 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 112 |
112 class URLRequestSimpleJobTest : public ::testing::Test { | 113 class URLRequestSimpleJobTest : public ::testing::Test { |
113 public: | 114 public: |
114 URLRequestSimpleJobTest() | 115 URLRequestSimpleJobTest() |
115 : context_(true), scoped_task_scheduler_(base::MessageLoop::current()) { | 116 : context_(true), scoped_task_scheduler_(base::MessageLoop::current()) { |
116 job_factory_.SetProtocolHandler( | 117 job_factory_.SetProtocolHandler( |
117 "data", base::MakeUnique<SimpleJobProtocolHandler>()); | 118 "data", base::MakeUnique<SimpleJobProtocolHandler>()); |
118 context_.set_job_factory(&job_factory_); | 119 context_.set_job_factory(&job_factory_); |
119 context_.Init(); | 120 context_.Init(); |
120 | 121 |
121 request_ = | 122 request_ = context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, |
122 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); | 123 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
123 } | 124 } |
124 | 125 |
125 void StartRequest(const HttpRequestHeaders* headers) { | 126 void StartRequest(const HttpRequestHeaders* headers) { |
126 if (headers) | 127 if (headers) |
127 request_->SetExtraRequestHeaders(*headers); | 128 request_->SetExtraRequestHeaders(*headers); |
128 request_->Start(); | 129 request_->Start(); |
129 | 130 |
130 EXPECT_TRUE(request_->is_pending()); | 131 EXPECT_TRUE(request_->is_pending()); |
131 base::RunLoop().Run(); | 132 base::RunLoop().Run(); |
132 EXPECT_FALSE(request_->is_pending()); | 133 EXPECT_FALSE(request_->is_pending()); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); | 190 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); |
190 headers.SetHeader(HttpRequestHeaders::kRange, range); | 191 headers.SetHeader(HttpRequestHeaders::kRange, range); |
191 | 192 |
192 StartRequest(&headers); | 193 StartRequest(&headers); |
193 | 194 |
194 EXPECT_THAT(delegate_.request_status(), IsOk()); | 195 EXPECT_THAT(delegate_.request_status(), IsOk()); |
195 EXPECT_EQ(kTestData, delegate_.data_received()); | 196 EXPECT_EQ(kTestData, delegate_.data_received()); |
196 } | 197 } |
197 | 198 |
198 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 199 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
199 request_ = | 200 request_ = context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, |
200 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); | 201 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
201 StartRequest(nullptr); | 202 StartRequest(nullptr); |
202 EXPECT_THAT(delegate_.request_status(), IsOk()); | 203 EXPECT_THAT(delegate_.request_status(), IsOk()); |
203 EXPECT_EQ("", delegate_.data_received()); | 204 EXPECT_EQ("", delegate_.data_received()); |
204 } | 205 } |
205 | 206 |
206 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { | 207 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { |
207 request_ = | 208 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
208 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); | 209 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
209 request_->Start(); | 210 request_->Start(); |
210 request_->Cancel(); | 211 request_->Cancel(); |
211 | 212 |
212 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
213 EXPECT_THAT(delegate_.request_status(), IsError(ERR_ABORTED)); | 214 EXPECT_THAT(delegate_.request_status(), IsError(ERR_ABORTED)); |
214 EXPECT_EQ(1, delegate_.response_started_count()); | 215 EXPECT_EQ(1, delegate_.response_started_count()); |
215 } | 216 } |
216 | 217 |
217 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 218 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
218 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 219 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
219 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 220 request_ = |
220 &cancel_delegate); | 221 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
| 222 &cancel_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
221 request_->Start(); | 223 request_->Start(); |
222 cancel_delegate.WaitUntilHeadersReceived(); | 224 cancel_delegate.WaitUntilHeadersReceived(); |
223 | 225 |
224 // Run ScopedTaskScheduler tasks. | 226 // Run ScopedTaskScheduler tasks. |
225 base::RunLoop().RunUntilIdle(); | 227 base::RunLoop().RunUntilIdle(); |
226 | 228 |
227 EXPECT_THAT(cancel_delegate.request_status(), IsError(ERR_ABORTED)); | 229 EXPECT_THAT(cancel_delegate.request_status(), IsError(ERR_ABORTED)); |
228 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 230 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
229 EXPECT_EQ("", cancel_delegate.data_received()); | 231 EXPECT_EQ("", cancel_delegate.data_received()); |
230 // Destroy the request so it doesn't outlive its delegate. | 232 // Destroy the request so it doesn't outlive its delegate. |
231 request_.reset(); | 233 request_.reset(); |
232 } | 234 } |
233 | 235 |
234 } // namespace net | 236 } // namespace net |
OLD | NEW |