Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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. |
| 4 | |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 5 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 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), |
| 26 | invalid_range); |
| 27 | |
| 28 | class MockSimpleJob : public URLRequestSimpleJob { |
| 29 | public: |
| 30 | MockSimpleJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 31 | : URLRequestSimpleJob(request, network_delegate) { |
| 32 | } |
| 33 | |
| 34 | protected: |
| 35 | virtual int GetData(std::string* mime_type, |
| 36 | std::string* charset, |
| 37 | std::string* data, |
| 38 | const CompletionCallback& callback) const OVERRIDE { |
| 39 | mime_type->assign("text/plain"); |
| 40 | charset->assign("US-ASCII"); |
| 41 | data->assign(kTestData); |
| 42 | return OK; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | virtual ~MockSimpleJob() {} |
| 47 | |
| 48 | std::string data_; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); |
| 51 | }; |
| 52 | |
| 53 | class SimpleJobProtocolHandler : |
| 54 | public URLRequestJobFactory::ProtocolHandler { |
| 55 | public: |
| 56 | virtual URLRequestJob* MaybeCreateJob( |
| 57 | URLRequest* request, |
| 58 | NetworkDelegate* network_delegate) const OVERRIDE { |
| 59 | return new MockSimpleJob(request, network_delegate); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | class URLRequestSimpleJobTest : public ::testing::Test { |
| 64 | public: |
| 65 | URLRequestSimpleJobTest() : context_(true) { |
| 66 | job_factory_.SetProtocolHandler("data", new SimpleJobProtocolHandler()); |
| 67 | context_.set_job_factory(&job_factory_); |
| 68 | context_.Init(); |
| 69 | |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 70 | request_ = context_.CreateRequest( |
| 71 | GURL("data:test"), DEFAULT_PRIORITY, &delegate_, NULL); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void StartRequest(const HttpRequestHeaders* headers) { |
| 75 | if (headers) |
| 76 | request_->SetExtraRequestHeaders(*headers); |
| 77 | request_->Start(); |
| 78 | |
| 79 | EXPECT_TRUE(request_->is_pending()); |
| 80 | base::RunLoop().Run(); |
| 81 | EXPECT_FALSE(request_->is_pending()); |
| 82 | } |
| 83 | |
| 84 | protected: |
| 85 | URLRequestJobFactoryImpl job_factory_; |
| 86 | TestURLRequestContext context_; |
| 87 | TestDelegate delegate_; |
| 88 | scoped_ptr<URLRequest> request_; |
| 89 | }; |
| 90 | |
| 91 | } // namespace |
| 92 | |
| 93 | TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
| 94 | StartRequest(NULL); |
| 95 | ASSERT_TRUE(request_->status().is_success()); |
| 96 | EXPECT_EQ(kTestData, delegate_.data_received()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(URLRequestSimpleJobTest, RangeRequest) { |
| 100 | const std::string kExpectedBody = std::string( |
| 101 | kTestData + kRangeFirstPosition, kTestData + kRangeLastPosition + 1); |
| 102 | HttpRequestHeaders headers; |
| 103 | headers.SetHeader( |
| 104 | HttpRequestHeaders::kRange, |
| 105 | HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition) |
| 106 | .GetHeaderValue()); |
| 107 | |
| 108 | StartRequest(&headers); |
| 109 | |
| 110 | ASSERT_TRUE(request_->status().is_success()); |
| 111 | EXPECT_EQ(kExpectedBody, delegate_.data_received()); |
| 112 | } |
| 113 | |
| 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", |
| 118 | kRangeFirstPosition, |
| 119 | middle_pos, |
| 120 | middle_pos + 1, |
| 121 | kRangeLastPosition); |
| 122 | headers.SetHeader(HttpRequestHeaders::kRange, range); |
| 123 | |
| 124 | StartRequest(&headers); |
| 125 | |
| 126 | EXPECT_TRUE(delegate_.request_failed()); |
| 127 | EXPECT_EQ(ERR_REQUEST_RANGE_NOT_SATISFIABLE, request_->status().error()); |
| 128 | } |
| 129 | |
| 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); |
| 135 | |
| 136 | StartRequest(&headers); |
| 137 | |
| 138 | ASSERT_TRUE(request_->status().is_success()); |
| 139 | EXPECT_EQ(kTestData, delegate_.data_received()); |
| 140 | } |
| 141 | |
| 142 | } // namespace net |