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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 TestURLRequestContext context_; | 156 TestURLRequestContext context_; |
157 URLRequestJobFactoryImpl job_factory_; | 157 URLRequestJobFactoryImpl job_factory_; |
158 TestDelegate delegate_; | 158 TestDelegate delegate_; |
159 std::unique_ptr<URLRequest> request_; | 159 std::unique_ptr<URLRequest> request_; |
160 }; | 160 }; |
161 | 161 |
162 } // namespace | 162 } // namespace |
163 | 163 |
164 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 164 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
165 StartRequest(NULL); | 165 StartRequest(NULL); |
166 ASSERT_TRUE(request_->status().is_success()); | 166 EXPECT_EQ(OK, delegate_.request_status()); |
167 EXPECT_EQ(kTestData, delegate_.data_received()); | 167 EXPECT_EQ(kTestData, delegate_.data_received()); |
168 } | 168 } |
169 | 169 |
170 TEST_F(URLRequestSimpleJobTest, RangeRequest) { | 170 TEST_F(URLRequestSimpleJobTest, RangeRequest) { |
171 const std::string kExpectedBody = std::string( | 171 const std::string kExpectedBody = std::string( |
172 kTestData + kRangeFirstPosition, kTestData + kRangeLastPosition + 1); | 172 kTestData + kRangeFirstPosition, kTestData + kRangeLastPosition + 1); |
173 HttpRequestHeaders headers; | 173 HttpRequestHeaders headers; |
174 headers.SetHeader( | 174 headers.SetHeader( |
175 HttpRequestHeaders::kRange, | 175 HttpRequestHeaders::kRange, |
176 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition) | 176 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition) |
177 .GetHeaderValue()); | 177 .GetHeaderValue()); |
178 | 178 |
179 StartRequest(&headers); | 179 StartRequest(&headers); |
180 | 180 |
181 ASSERT_TRUE(request_->status().is_success()); | 181 EXPECT_EQ(OK, delegate_.request_status()); |
182 EXPECT_EQ(kExpectedBody, delegate_.data_received()); | 182 EXPECT_EQ(kExpectedBody, delegate_.data_received()); |
183 } | 183 } |
184 | 184 |
185 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { | 185 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { |
186 HttpRequestHeaders headers; | 186 HttpRequestHeaders headers; |
187 int middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2; | 187 int middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2; |
188 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", | 188 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", |
189 kRangeFirstPosition, | 189 kRangeFirstPosition, |
190 middle_pos, | 190 middle_pos, |
191 middle_pos + 1, | 191 middle_pos + 1, |
192 kRangeLastPosition); | 192 kRangeLastPosition); |
193 headers.SetHeader(HttpRequestHeaders::kRange, range); | 193 headers.SetHeader(HttpRequestHeaders::kRange, range); |
194 | 194 |
195 StartRequest(&headers); | 195 StartRequest(&headers); |
196 | 196 |
197 EXPECT_TRUE(delegate_.request_failed()); | 197 EXPECT_TRUE(delegate_.request_failed()); |
198 EXPECT_THAT(request_->status().error(), | 198 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, delegate_.request_status()); |
199 IsError(ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
200 } | 199 } |
201 | 200 |
202 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { | 201 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { |
203 HttpRequestHeaders headers; | 202 HttpRequestHeaders headers; |
204 std::string range = base::StringPrintf( | 203 std::string range = base::StringPrintf( |
205 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); | 204 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); |
206 headers.SetHeader(HttpRequestHeaders::kRange, range); | 205 headers.SetHeader(HttpRequestHeaders::kRange, range); |
207 | 206 |
208 StartRequest(&headers); | 207 StartRequest(&headers); |
209 | 208 |
210 ASSERT_TRUE(request_->status().is_success()); | 209 EXPECT_EQ(OK, delegate_.request_status()); |
211 EXPECT_EQ(kTestData, delegate_.data_received()); | 210 EXPECT_EQ(kTestData, delegate_.data_received()); |
212 } | 211 } |
213 | 212 |
214 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 213 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
215 request_ = | 214 request_ = |
216 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); | 215 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); |
217 StartRequest(nullptr); | 216 StartRequest(nullptr); |
218 ASSERT_TRUE(request_->status().is_success()); | 217 EXPECT_EQ(OK, delegate_.request_status()); |
219 EXPECT_EQ("", delegate_.data_received()); | 218 EXPECT_EQ("", delegate_.data_received()); |
220 } | 219 } |
221 | 220 |
222 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { | 221 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { |
223 request_ = | 222 request_ = |
224 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); | 223 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); |
225 request_->Start(); | 224 request_->Start(); |
226 request_->Cancel(); | 225 request_->Cancel(); |
227 | 226 |
228 base::RunLoop().RunUntilIdle(); | 227 base::RunLoop().RunUntilIdle(); |
229 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | 228 EXPECT_EQ(ERR_ABORTED, delegate_.request_status()); |
230 EXPECT_EQ(1, delegate_.response_started_count()); | 229 EXPECT_EQ(1, delegate_.response_started_count()); |
231 } | 230 } |
232 | 231 |
233 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 232 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
234 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 233 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
235 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 234 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
236 &cancel_delegate); | 235 &cancel_delegate); |
237 request_->Start(); | 236 request_->Start(); |
238 cancel_delegate.WaitUntilHeadersReceived(); | 237 cancel_delegate.WaitUntilHeadersReceived(); |
239 | 238 |
240 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 239 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
241 // callbacks which are invoked in ReadRawData have completed safely. | 240 // callbacks which are invoked in ReadRawData have completed safely. |
242 base::RunLoop run_loop; | 241 base::RunLoop run_loop; |
243 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 242 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
244 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 243 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
245 run_loop.Run(); | 244 run_loop.Run(); |
246 | 245 |
247 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | |
248 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 246 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
249 EXPECT_EQ("", cancel_delegate.data_received()); | 247 EXPECT_EQ("", cancel_delegate.data_received()); |
250 // Destroy the request so it doesn't outlive its delegate. | 248 // Destroy the request so it doesn't outlive its delegate. |
251 request_.reset(); | 249 request_.reset(); |
252 } | 250 } |
253 | 251 |
254 } // namespace net | 252 } // namespace net |
OLD | NEW |