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/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "base/strings/stringprintf.h"
8 #include "net/base/request_priority.h"
9 #include "net/url_request/url_request_job.h"
10 #include "net/url_request/url_request_job_factory.h"
11 #include "net/url_request/url_request_job_factory_impl.h"
12 #include "net/url_request/url_request_simple_job.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 const char kTestData
[] = "Huge data array";
21 const int kRangeFirstPosition
= 5;
22 const int kRangeLastPosition
= 8;
23 COMPILE_ASSERT(kRangeFirstPosition
> 0 &&
24 kRangeFirstPosition
< kRangeLastPosition
&&
25 kRangeLastPosition
< static_cast<int>(arraysize(kTestData
) - 1),
28 class MockSimpleJob
: public URLRequestSimpleJob
{
30 MockSimpleJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
31 : URLRequestSimpleJob(request
, network_delegate
) {
35 int GetData(std::string
* mime_type
,
38 const CompletionCallback
& callback
) const override
{
39 mime_type
->assign("text/plain");
40 charset
->assign("US-ASCII");
41 data
->assign(kTestData
);
46 ~MockSimpleJob() override
{}
50 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob
);
53 class SimpleJobProtocolHandler
:
54 public URLRequestJobFactory::ProtocolHandler
{
56 URLRequestJob
* MaybeCreateJob(
58 NetworkDelegate
* network_delegate
) const override
{
59 return new MockSimpleJob(request
, network_delegate
);
63 class URLRequestSimpleJobTest
: public ::testing::Test
{
65 URLRequestSimpleJobTest() : context_(true) {
66 job_factory_
.SetProtocolHandler("data", new SimpleJobProtocolHandler());
67 context_
.set_job_factory(&job_factory_
);
70 request_
= context_
.CreateRequest(
71 GURL("data:test"), DEFAULT_PRIORITY
, &delegate_
, NULL
);
74 void StartRequest(const HttpRequestHeaders
* headers
) {
76 request_
->SetExtraRequestHeaders(*headers
);
79 EXPECT_TRUE(request_
->is_pending());
80 base::RunLoop().Run();
81 EXPECT_FALSE(request_
->is_pending());
85 URLRequestJobFactoryImpl job_factory_
;
86 TestURLRequestContext context_
;
87 TestDelegate delegate_
;
88 scoped_ptr
<URLRequest
> request_
;
93 TEST_F(URLRequestSimpleJobTest
, SimpleRequest
) {
95 ASSERT_TRUE(request_
->status().is_success());
96 EXPECT_EQ(kTestData
, delegate_
.data_received());
99 TEST_F(URLRequestSimpleJobTest
, RangeRequest
) {
100 const std::string kExpectedBody
= std::string(
101 kTestData
+ kRangeFirstPosition
, kTestData
+ kRangeLastPosition
+ 1);
102 HttpRequestHeaders headers
;
104 HttpRequestHeaders::kRange
,
105 HttpByteRange::Bounded(kRangeFirstPosition
, kRangeLastPosition
)
108 StartRequest(&headers
);
110 ASSERT_TRUE(request_
->status().is_success());
111 EXPECT_EQ(kExpectedBody
, delegate_
.data_received());
114 TEST_F(URLRequestSimpleJobTest
, MultipleRangeRequest
) {
115 HttpRequestHeaders headers
;
116 int middle_pos
= (kRangeFirstPosition
+ kRangeLastPosition
)/2;
117 std::string range
= base::StringPrintf("bytes=%d-%d,%d-%d",
122 headers
.SetHeader(HttpRequestHeaders::kRange
, range
);
124 StartRequest(&headers
);
126 EXPECT_TRUE(delegate_
.request_failed());
127 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE
, request_
->status().error());
130 TEST_F(URLRequestSimpleJobTest
, InvalidRangeRequest
) {
131 HttpRequestHeaders headers
;
132 std::string range
= base::StringPrintf(
133 "bytes=%d-%d", kRangeLastPosition
, kRangeFirstPosition
);
134 headers
.SetHeader(HttpRequestHeaders::kRange
, range
);
136 StartRequest(&headers
);
138 ASSERT_TRUE(request_
->status().is_success());
139 EXPECT_EQ(kTestData
, delegate_
.data_received());