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