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 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 ~CancelAfterFirstReadURLRequestDelegate() override {} | 81 ~CancelAfterFirstReadURLRequestDelegate() override {} |
82 | 82 |
83 void OnResponseStarted(URLRequest* request, int net_error) override { | 83 void OnResponseStarted(URLRequest* request, int net_error) override { |
84 DCHECK_NE(ERR_IO_PENDING, net_error); | 84 DCHECK_NE(ERR_IO_PENDING, net_error); |
85 // net::TestDelegate will start the first read. | 85 // net::TestDelegate will start the first read. |
86 TestDelegate::OnResponseStarted(request, net_error); | 86 TestDelegate::OnResponseStarted(request, net_error); |
87 request->Cancel(); | 87 request->Cancel(); |
88 run_loop_->Quit(); | 88 run_loop_->Quit(); |
89 } | 89 } |
90 | 90 |
| 91 void OnReadCompleted(URLRequest* request, int bytes_read) override { |
| 92 // Read should have been cancelled. |
| 93 EXPECT_EQ(ERR_ABORTED, bytes_read); |
| 94 } |
| 95 |
91 void WaitUntilHeadersReceived() const { run_loop_->Run(); } | 96 void WaitUntilHeadersReceived() const { run_loop_->Run(); } |
92 | 97 |
93 private: | 98 private: |
94 std::unique_ptr<base::RunLoop> run_loop_; | 99 std::unique_ptr<base::RunLoop> run_loop_; |
95 | 100 |
96 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); | 101 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); |
97 }; | 102 }; |
98 | 103 |
99 class SimpleJobProtocolHandler : | 104 class SimpleJobProtocolHandler : |
100 public URLRequestJobFactory::ProtocolHandler { | 105 public URLRequestJobFactory::ProtocolHandler { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 TestURLRequestContext context_; | 156 TestURLRequestContext context_; |
152 URLRequestJobFactoryImpl job_factory_; | 157 URLRequestJobFactoryImpl job_factory_; |
153 TestDelegate delegate_; | 158 TestDelegate delegate_; |
154 std::unique_ptr<URLRequest> request_; | 159 std::unique_ptr<URLRequest> request_; |
155 }; | 160 }; |
156 | 161 |
157 } // namespace | 162 } // namespace |
158 | 163 |
159 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 164 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
160 StartRequest(NULL); | 165 StartRequest(NULL); |
161 EXPECT_THAT(delegate_.request_status(), IsOk()); | 166 ASSERT_TRUE(request_->status().is_success()); |
162 EXPECT_EQ(kTestData, delegate_.data_received()); | 167 EXPECT_EQ(kTestData, delegate_.data_received()); |
163 } | 168 } |
164 | 169 |
165 TEST_F(URLRequestSimpleJobTest, RangeRequest) { | 170 TEST_F(URLRequestSimpleJobTest, RangeRequest) { |
166 const std::string kExpectedBody = std::string( | 171 const std::string kExpectedBody = std::string( |
167 kTestData + kRangeFirstPosition, kTestData + kRangeLastPosition + 1); | 172 kTestData + kRangeFirstPosition, kTestData + kRangeLastPosition + 1); |
168 HttpRequestHeaders headers; | 173 HttpRequestHeaders headers; |
169 headers.SetHeader( | 174 headers.SetHeader( |
170 HttpRequestHeaders::kRange, | 175 HttpRequestHeaders::kRange, |
171 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition) | 176 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition) |
172 .GetHeaderValue()); | 177 .GetHeaderValue()); |
173 | 178 |
174 StartRequest(&headers); | 179 StartRequest(&headers); |
175 | 180 |
176 EXPECT_THAT(delegate_.request_status(), IsOk()); | 181 ASSERT_TRUE(request_->status().is_success()); |
177 EXPECT_EQ(kExpectedBody, delegate_.data_received()); | 182 EXPECT_EQ(kExpectedBody, delegate_.data_received()); |
178 } | 183 } |
179 | 184 |
180 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { | 185 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { |
181 HttpRequestHeaders headers; | 186 HttpRequestHeaders headers; |
182 int middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2; | 187 int middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2; |
183 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", | 188 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", |
184 kRangeFirstPosition, | 189 kRangeFirstPosition, |
185 middle_pos, | 190 middle_pos, |
186 middle_pos + 1, | 191 middle_pos + 1, |
187 kRangeLastPosition); | 192 kRangeLastPosition); |
188 headers.SetHeader(HttpRequestHeaders::kRange, range); | 193 headers.SetHeader(HttpRequestHeaders::kRange, range); |
189 | 194 |
190 StartRequest(&headers); | 195 StartRequest(&headers); |
191 | 196 |
192 EXPECT_TRUE(delegate_.request_failed()); | 197 EXPECT_TRUE(delegate_.request_failed()); |
193 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, delegate_.request_status()); | 198 EXPECT_THAT(request_->status().error(), |
| 199 IsError(ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
194 } | 200 } |
195 | 201 |
196 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { | 202 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { |
197 HttpRequestHeaders headers; | 203 HttpRequestHeaders headers; |
198 std::string range = base::StringPrintf( | 204 std::string range = base::StringPrintf( |
199 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); | 205 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); |
200 headers.SetHeader(HttpRequestHeaders::kRange, range); | 206 headers.SetHeader(HttpRequestHeaders::kRange, range); |
201 | 207 |
202 StartRequest(&headers); | 208 StartRequest(&headers); |
203 | 209 |
204 EXPECT_THAT(delegate_.request_status(), IsOk()); | 210 ASSERT_TRUE(request_->status().is_success()); |
205 EXPECT_EQ(kTestData, delegate_.data_received()); | 211 EXPECT_EQ(kTestData, delegate_.data_received()); |
206 } | 212 } |
207 | 213 |
208 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 214 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
209 request_ = | 215 request_ = |
210 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); | 216 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); |
211 StartRequest(nullptr); | 217 StartRequest(nullptr); |
212 EXPECT_THAT(delegate_.request_status(), IsOk()); | 218 ASSERT_TRUE(request_->status().is_success()); |
213 EXPECT_EQ("", delegate_.data_received()); | 219 EXPECT_EQ("", delegate_.data_received()); |
214 } | 220 } |
215 | 221 |
216 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { | 222 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { |
217 request_ = | 223 request_ = |
218 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); | 224 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); |
219 request_->Start(); | 225 request_->Start(); |
220 request_->Cancel(); | 226 request_->Cancel(); |
221 | 227 |
222 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
223 EXPECT_THAT(delegate_.request_status(), IsError(ERR_ABORTED)); | 229 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); |
224 EXPECT_EQ(1, delegate_.response_started_count()); | 230 EXPECT_EQ(1, delegate_.response_started_count()); |
225 } | 231 } |
226 | 232 |
227 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 233 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
228 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 234 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
229 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 235 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
230 &cancel_delegate); | 236 &cancel_delegate); |
231 request_->Start(); | 237 request_->Start(); |
232 cancel_delegate.WaitUntilHeadersReceived(); | 238 cancel_delegate.WaitUntilHeadersReceived(); |
233 | 239 |
234 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 240 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
235 // callbacks which are invoked in ReadRawData have completed safely. | 241 // callbacks which are invoked in ReadRawData have completed safely. |
236 base::RunLoop run_loop; | 242 base::RunLoop run_loop; |
237 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 243 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
238 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 244 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
239 run_loop.Run(); | 245 run_loop.Run(); |
240 | 246 |
241 EXPECT_THAT(cancel_delegate.request_status(), IsError(ERR_ABORTED)); | 247 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); |
242 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 248 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
243 EXPECT_EQ("", cancel_delegate.data_received()); | 249 EXPECT_EQ("", cancel_delegate.data_received()); |
244 // Destroy the request so it doesn't outlive its delegate. | 250 // Destroy the request so it doesn't outlive its delegate. |
245 request_.reset(); | 251 request_.reset(); |
246 } | 252 } |
247 | 253 |
248 } // namespace net | 254 } // namespace net |
OLD | NEW |