Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: net/url_request/url_request_simple_job_unittest.cc

Issue 104513002: Fixed playing video in component extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added URLRequestSimpleJobTest. More clear ignoring incorrect range headers. Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reservedelegate_.
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 size_t kRangeFirstPosition = 5;
21 const size_t kRangeLastPosition = 8;
22
23 class MockSimpleJob : public net::URLRequestSimpleJob {
mmenke 2014/01/07 18:51:39 nit: "net::" prefix not needed anywhere in this f
kirr 2014/01/09 07:33:21 Done.
24 public:
25 MockSimpleJob(net::URLRequest* request,
26 net::NetworkDelegate* network_delegate)
mmenke 2014/01/07 18:51:39 nit: 4 less indent.
kirr 2014/01/09 07:33:21 Done.
27 : net::URLRequestSimpleJob(request, network_delegate) {
28 }
29
30 protected:
31 virtual int GetData(std::string* mime_type,
32 std::string* charset,
33 std::string* data,
34 const net::CompletionCallback& callback) const OVERRIDE {
35 mime_type->assign("text/plain");
36 charset->assign("US-ASCII");
37 data->assign(kTestData);
38 return net::OK;
39 }
40
41 private:
42 virtual ~MockSimpleJob() {}
43
44 std::string data_;
45 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob);
mmenke 2014/01/07 18:51:39 nit: Blank line before DISALLOW_COPY_AND_ASSIGN.
kirr 2014/01/09 07:33:21 Done.
46 };
47
48 class URLRequestSimpleJobTest : public ::testing::Test {
49 public:
50 class SimpleJobProtocolHandler :
51 public net::URLRequestJobFactory::ProtocolHandler {
mmenke 2014/01/07 18:51:39 optional: Suggest moving this class outside of th
kirr 2014/01/09 07:33:21 Done.
52 public:
53 virtual net::URLRequestJob* MaybeCreateJob(
54 net::URLRequest* request,
55 net::NetworkDelegate* network_delegate) const OVERRIDE {
56 return new MockSimpleJob(request, network_delegate);
57 }
58 };
59
60 URLRequestSimpleJobTest() : context_(true) {
61 CHECK(kRangeFirstPosition > 0 &&
62 kRangeFirstPosition < kRangeLastPosition &&
63 kRangeLastPosition < strlen(kTestData));
mmenke 2014/01/07 18:51:39 Instead of a CHECK, you could put things like the
kirr 2014/01/09 07:33:21 Done.
64
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 net::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
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 data_part = std::string(kTestData + kRangeFirstPosition,
mmenke 2014/01/07 18:51:39 nit: This should use kConstantNamingScheme. I al
kirr 2014/01/09 07:33:21 Done.
101 kTestData + kRangeLastPosition + 1);
mmenke 2014/01/07 18:51:39 nit: This should line up with the "kTestData" in
kirr 2014/01/09 07:33:21 Done.
102 HttpRequestHeaders headers;
103 headers.SetHeader(
104 HttpRequestHeaders::kRange,
105 net::HttpByteRange::Bounded(kRangeFirstPosition, kRangeLastPosition)
106 .GetHeaderValue());
107
108 StartRequest(&headers);
109
110 ASSERT_TRUE(request_->status().is_success());
111 EXPECT_EQ(data_part, delegate_.data_received());
112 }
113
114 TEST_F(URLRequestSimpleJobTest, MultipleRangeRequest) {
115 HttpRequestHeaders headers;
116 size_t middle_pos = (kRangeFirstPosition + kRangeLastPosition)/2;
117 std::string range = base::StringPrintf("bytes=%zu-%zu,%zu-%zu",
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(net::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=%zu-%zu", 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());
mmenke 2014/01/07 18:51:39 Wonder if we should be returning an error or not i
kirr 2014/01/09 07:33:21 As I understand we ignore invalid ranges just like
mmenke 2014/01/09 15:49:23 It just seems a little weird to me that if we see
140 }
141
142 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698