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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
7 #include "net/base/request_priority.h" | 7 #include "net/base/request_priority.h" |
8 #include "net/url_request/url_request_job.h" | 8 #include "net/url_request/url_request_job.h" |
9 #include "net/url_request/url_request_job_factory.h" | 9 #include "net/url_request/url_request_job_factory.h" |
10 #include "net/url_request/url_request_job_factory_impl.h" | 10 #include "net/url_request/url_request_job_factory_impl.h" |
11 #include "net/url_request/url_request_simple_job.h" | 11 #include "net/url_request/url_request_simple_job.h" |
12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const char kTestData[] = "Huge data array"; | 19 const char kTestData[] = "Huge data array"; |
20 const int kRangeFirstPosition = 5; | 20 const int kRangeFirstPosition = 5; |
21 const int kRangeLastPosition = 8; | 21 const int kRangeLastPosition = 8; |
22 COMPILE_ASSERT(kRangeFirstPosition > 0 && | 22 COMPILE_ASSERT(kRangeFirstPosition > 0 && |
23 kRangeFirstPosition < kRangeLastPosition && | 23 kRangeFirstPosition < |
24 kRangeLastPosition < static_cast<int>(arraysize(kTestData) - 1), | 24 kRangeLastPosition&& kRangeLastPosition < |
25 invalid_range); | 25 static_cast<int>(arraysize(kTestData) - 1), |
| 26 invalid_range); |
26 | 27 |
27 class MockSimpleJob : public URLRequestSimpleJob { | 28 class MockSimpleJob : public URLRequestSimpleJob { |
28 public: | 29 public: |
29 MockSimpleJob(URLRequest* request, NetworkDelegate* network_delegate) | 30 MockSimpleJob(URLRequest* request, NetworkDelegate* network_delegate) |
30 : URLRequestSimpleJob(request, network_delegate) { | 31 : URLRequestSimpleJob(request, network_delegate) {} |
31 } | |
32 | 32 |
33 protected: | 33 protected: |
34 virtual int GetData(std::string* mime_type, | 34 virtual int GetData(std::string* mime_type, |
35 std::string* charset, | 35 std::string* charset, |
36 std::string* data, | 36 std::string* data, |
37 const CompletionCallback& callback) const OVERRIDE { | 37 const CompletionCallback& callback) const OVERRIDE { |
38 mime_type->assign("text/plain"); | 38 mime_type->assign("text/plain"); |
39 charset->assign("US-ASCII"); | 39 charset->assign("US-ASCII"); |
40 data->assign(kTestData); | 40 data->assign(kTestData); |
41 return OK; | 41 return OK; |
42 } | 42 } |
43 | 43 |
44 private: | 44 private: |
45 virtual ~MockSimpleJob() {} | 45 virtual ~MockSimpleJob() {} |
46 | 46 |
47 std::string data_; | 47 std::string data_; |
48 | 48 |
49 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); | 49 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); |
50 }; | 50 }; |
51 | 51 |
52 class SimpleJobProtocolHandler : | 52 class SimpleJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
53 public URLRequestJobFactory::ProtocolHandler { | |
54 public: | 53 public: |
55 virtual URLRequestJob* MaybeCreateJob( | 54 virtual URLRequestJob* MaybeCreateJob( |
56 URLRequest* request, | 55 URLRequest* request, |
57 NetworkDelegate* network_delegate) const OVERRIDE { | 56 NetworkDelegate* network_delegate) const OVERRIDE { |
58 return new MockSimpleJob(request, network_delegate); | 57 return new MockSimpleJob(request, network_delegate); |
59 } | 58 } |
60 }; | 59 }; |
61 | 60 |
62 class URLRequestSimpleJobTest : public ::testing::Test { | 61 class URLRequestSimpleJobTest : public ::testing::Test { |
63 public: | 62 public: |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 .GetHeaderValue()); | 104 .GetHeaderValue()); |
106 | 105 |
107 StartRequest(&headers); | 106 StartRequest(&headers); |
108 | 107 |
109 ASSERT_TRUE(request_->status().is_success()); | 108 ASSERT_TRUE(request_->status().is_success()); |
110 EXPECT_EQ(kExpectedBody, delegate_.data_received()); | 109 EXPECT_EQ(kExpectedBody, delegate_.data_received()); |
111 } | 110 } |
112 | 111 |
113 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { | 112 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) { |
114 HttpRequestHeaders headers; | 113 HttpRequestHeaders headers; |
115 int middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2; | 114 int middle_pos = (kRangeFirstPosition + kRangeLastPosition) / 2; |
116 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", | 115 std::string range = base::StringPrintf("bytes=%d-%d,%d-%d", |
117 kRangeFirstPosition, | 116 kRangeFirstPosition, |
118 middle_pos, | 117 middle_pos, |
119 middle_pos + 1, | 118 middle_pos + 1, |
120 kRangeLastPosition); | 119 kRangeLastPosition); |
121 headers.SetHeader(HttpRequestHeaders::kRange, range); | 120 headers.SetHeader(HttpRequestHeaders::kRange, range); |
122 | 121 |
123 StartRequest(&headers); | 122 StartRequest(&headers); |
124 | 123 |
125 EXPECT_TRUE(delegate_.request_failed()); | 124 EXPECT_TRUE(delegate_.request_failed()); |
126 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, request_->status().error()); | 125 EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, request_->status().error()); |
127 } | 126 } |
128 | 127 |
129 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { | 128 TEST_F(URLRequestSimpleJobTest, InvalidRangeRequest) { |
130 HttpRequestHeaders headers; | 129 HttpRequestHeaders headers; |
131 std::string range = base::StringPrintf( | 130 std::string range = base::StringPrintf( |
132 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); | 131 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); |
133 headers.SetHeader(HttpRequestHeaders::kRange, range); | 132 headers.SetHeader(HttpRequestHeaders::kRange, range); |
134 | 133 |
135 StartRequest(&headers); | 134 StartRequest(&headers); |
136 | 135 |
137 ASSERT_TRUE(request_->status().is_success()); | 136 ASSERT_TRUE(request_->status().is_success()); |
138 EXPECT_EQ(kTestData, delegate_.data_received()); | 137 EXPECT_EQ(kTestData, delegate_.data_received()); |
139 } | 138 } |
140 | 139 |
141 } // namespace net | 140 } // namespace net |
OLD | NEW |