Don't include a hash in settings/manageProfile.
[chromium-blink-merge.git] / net / url_request / url_request_simple_job_unittest.cc
blob5ee71b5766fbb6928581fbe09de2e3ece9e9705a
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"
16 namespace net {
18 namespace {
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);
28 class MockSimpleJob : public URLRequestSimpleJob {
29 public:
30 MockSimpleJob(URLRequest* request, NetworkDelegate* network_delegate)
31 : URLRequestSimpleJob(request, network_delegate) {
34 protected:
35 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;
45 private:
46 ~MockSimpleJob() override {}
48 std::string data_;
50 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob);
53 class SimpleJobProtocolHandler :
54 public URLRequestJobFactory::ProtocolHandler {
55 public:
56 URLRequestJob* MaybeCreateJob(
57 URLRequest* request,
58 NetworkDelegate* network_delegate) const override {
59 return new MockSimpleJob(request, network_delegate);
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();
70 request_ = context_.CreateRequest(
71 GURL("data:test"), DEFAULT_PRIORITY, &delegate_, NULL);
74 void StartRequest(const HttpRequestHeaders* headers) {
75 if (headers)
76 request_->SetExtraRequestHeaders(*headers);
77 request_->Start();
79 EXPECT_TRUE(request_->is_pending());
80 base::RunLoop().Run();
81 EXPECT_FALSE(request_->is_pending());
84 protected:
85 URLRequestJobFactoryImpl job_factory_;
86 TestURLRequestContext context_;
87 TestDelegate delegate_;
88 scoped_ptr<URLRequest> request_;
91 } // namespace
93 TEST_F(URLRequestSimpleJobTest, SimpleRequest) {
94 StartRequest(NULL);
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;
103 headers.SetHeader(
104 HttpRequestHeaders::kRange,
105 HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition)
106 .GetHeaderValue());
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",
118 kRangeFirstPosition,
119 middle_pos,
120 middle_pos + 1,
121 kRangeLastPosition);
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());
142 } // namespace net