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
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "base/strings/stringprintf.h"
7 #include "net/base/request_priority.h"
8 #include "net/url_request/url_request_job.h"
9 #include "net/url_request/url_request_job_factory.h"
10 #include "net/url_request/url_request_job_factory_impl.h"
11 #include "net/url_request/url_request_simple_job.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 const char kTestData
[] = "Huge data array";
20 const int kRangeFirstPosition
= 5;
21 const int kRangeLastPosition
= 8;
22 COMPILE_ASSERT(kRangeFirstPosition
> 0 &&
23 kRangeFirstPosition
< kRangeLastPosition
&&
24 kRangeLastPosition
< static_cast<int>(arraysize(kTestData
) - 1),
27 class MockSimpleJob
: public URLRequestSimpleJob
{
29 MockSimpleJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
30 : URLRequestSimpleJob(request
, network_delegate
) {
34 virtual int GetData(std::string
* mime_type
,
37 const CompletionCallback
& callback
) const OVERRIDE
{
38 mime_type
->assign("text/plain");
39 charset
->assign("US-ASCII");
40 data
->assign(kTestData
);
45 virtual ~MockSimpleJob() {}
49 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob
);
52 class SimpleJobProtocolHandler
:
53 public URLRequestJobFactory::ProtocolHandler
{
55 virtual URLRequestJob
* MaybeCreateJob(
57 NetworkDelegate
* network_delegate
) const OVERRIDE
{
58 return new MockSimpleJob(request
, network_delegate
);
62 class URLRequestSimpleJobTest
: public ::testing::Test
{
64 URLRequestSimpleJobTest() : context_(true) {
65 job_factory_
.SetProtocolHandler("data", new SimpleJobProtocolHandler());
66 context_
.set_job_factory(&job_factory_
);
69 request_
.reset(new URLRequest(
70 GURL("data:test"), DEFAULT_PRIORITY
, &delegate_
, &context_
));
73 void StartRequest(const HttpRequestHeaders
* headers
) {
75 request_
->SetExtraRequestHeaders(*headers
);
78 EXPECT_TRUE(request_
->is_pending());
79 base::RunLoop().Run();
80 EXPECT_FALSE(request_
->is_pending());
84 URLRequestJobFactoryImpl job_factory_
;
85 TestURLRequestContext context_
;
86 TestDelegate delegate_
;
87 scoped_ptr
<URLRequest
> request_
;
92 TEST_F(URLRequestSimpleJobTest
, SimpleRequest
) {
94 ASSERT_TRUE(request_
->status().is_success());
95 EXPECT_EQ(kTestData
, delegate_
.data_received());
98 TEST_F(URLRequestSimpleJobTest
, RangeRequest
) {
99 const std::string kExpectedBody
= std::string(
100 kTestData
+ kRangeFirstPosition
, kTestData
+ kRangeLastPosition
+ 1);
101 HttpRequestHeaders headers
;
103 HttpRequestHeaders::kRange
,
104 HttpByteRange::Bounded(kRangeFirstPosition
, kRangeLastPosition
)
107 StartRequest(&headers
);
109 ASSERT_TRUE(request_
->status().is_success());
110 EXPECT_EQ(kExpectedBody
, delegate_
.data_received());
113 TEST_F(URLRequestSimpleJobTest
, MultipleRangeRequest
) {
114 HttpRequestHeaders headers
;
115 int middle_pos
= (kRangeFirstPosition
+ kRangeLastPosition
)/2;
116 std::string range
= base::StringPrintf("bytes=%d-%d,%d-%d",
121 headers
.SetHeader(HttpRequestHeaders::kRange
, range
);
123 StartRequest(&headers
);
125 EXPECT_TRUE(delegate_
.request_failed());
126 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE
, request_
->status().error());
129 TEST_F(URLRequestSimpleJobTest
, InvalidRangeRequest
) {
130 HttpRequestHeaders headers
;
131 std::string range
= base::StringPrintf(
132 "bytes=%d-%d", kRangeLastPosition
, kRangeFirstPosition
);
133 headers
.SetHeader(HttpRequestHeaders::kRange
, range
);
135 StartRequest(&headers
);
137 ASSERT_TRUE(request_
->status().is_success());
138 EXPECT_EQ(kTestData
, delegate_
.data_received());