QUIC - enable persisting of QUICServerInfo (server config) to disk
[chromium-blink-merge.git] / net / url_request / url_request_simple_job_unittest.cc
blobdfd985acd4c3206467e7930c8f9d5dc67dd14086
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"
15 namespace net {
17 namespace {
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);
27 class MockSimpleJob : public URLRequestSimpleJob {
28 public:
29 MockSimpleJob(URLRequest* request, NetworkDelegate* network_delegate)
30 : URLRequestSimpleJob(request, network_delegate) {
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;
44 private:
45 virtual ~MockSimpleJob() {}
47 std::string data_;
49 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob);
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);
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();
69 request_.reset(new URLRequest(
70 GURL("data:test"), DEFAULT_PRIORITY, &delegate_, &context_));
73 void StartRequest(const HttpRequestHeaders* headers) {
74 if (headers)
75 request_->SetExtraRequestHeaders(*headers);
76 request_->Start();
78 EXPECT_TRUE(request_->is_pending());
79 base::RunLoop().Run();
80 EXPECT_FALSE(request_->is_pending());
83 protected:
84 URLRequestJobFactoryImpl job_factory_;
85 TestURLRequestContext context_;
86 TestDelegate delegate_;
87 scoped_ptr<URLRequest> request_;
90 } // namespace
92 TEST_F(URLRequestSimpleJobTest, SimpleRequest) {
93 StartRequest(NULL);
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;
102 headers.SetHeader(
103 HttpRequestHeaders::kRange,
104 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition)
105 .GetHeaderValue());
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",
117 kRangeFirstPosition,
118 middle_pos,
119 middle_pos + 1,
120 kRangeLastPosition);
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());
141 } // namespace net