1 // Copyright (c) 2012 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/message_loop/message_loop.h"
6 #include "base/test/test_simple_task_runner.h"
7 #include "content/browser/streams/stream.h"
8 #include "content/browser/streams/stream_registry.h"
9 #include "content/browser/streams/stream_url_request_job.h"
10 #include "content/browser/streams/stream_write_observer.h"
11 #include "net/base/request_priority.h"
12 #include "net/http/http_byte_range.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_job_factory_impl.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 const int kBufferSize
= 1024;
25 const char kTestData1
[] = "Hello";
26 const char kTestData2
[] = "Here it is data.";
28 const GURL
kStreamURL("blob://stream");
32 class StreamURLRequestJobTest
: public testing::Test
{
34 // A simple ProtocolHandler implementation to create StreamURLRequestJob.
35 class MockProtocolHandler
:
36 public net::URLRequestJobFactory::ProtocolHandler
{
38 MockProtocolHandler(StreamRegistry
* registry
) : registry_(registry
) {}
40 // net::URLRequestJobFactory::ProtocolHandler override.
41 net::URLRequestJob
* MaybeCreateJob(
42 net::URLRequest
* request
,
43 net::NetworkDelegate
* network_delegate
) const override
{
44 scoped_refptr
<Stream
> stream
= registry_
->GetStream(request
->url());
46 return new StreamURLRequestJob(request
, network_delegate
, stream
);
51 StreamRegistry
* registry_
;
54 StreamURLRequestJobTest() {}
56 void SetUp() override
{
57 registry_
.reset(new StreamRegistry());
59 url_request_job_factory_
.SetProtocolHandler(
60 "blob", new MockProtocolHandler(registry_
.get()));
61 url_request_context_
.set_job_factory(&url_request_job_factory_
);
64 void TearDown() override
{}
66 void TestSuccessRequest(const GURL
& url
,
67 const std::string
& expected_response
) {
68 TestRequest("GET", url
, net::HttpRequestHeaders(), 200, expected_response
);
71 void TestRequest(const std::string
& method
,
73 const net::HttpRequestHeaders
& extra_headers
,
74 int expected_status_code
,
75 const std::string
& expected_response
) {
76 net::TestDelegate delegate
;
77 request_
= url_request_context_
.CreateRequest(
78 url
, net::DEFAULT_PRIORITY
, &delegate
);
79 request_
->set_method(method
);
80 if (!extra_headers
.IsEmpty())
81 request_
->SetExtraRequestHeaders(extra_headers
);
84 base::MessageLoop::current()->RunUntilIdle();
87 EXPECT_TRUE(request_
->status().is_success());
88 ASSERT_TRUE(request_
->response_headers());
89 EXPECT_EQ(expected_status_code
,
90 request_
->response_headers()->response_code());
91 EXPECT_EQ(expected_response
, delegate
.data_received());
95 base::MessageLoopForIO message_loop_
;
96 scoped_ptr
<StreamRegistry
> registry_
;
98 net::URLRequestContext url_request_context_
;
99 net::URLRequestJobFactoryImpl url_request_job_factory_
;
100 scoped_ptr
<net::URLRequest
> request_
;
103 TEST_F(StreamURLRequestJobTest
, TestGetSimpleDataRequest
) {
104 scoped_refptr
<Stream
> stream(
105 new Stream(registry_
.get(), NULL
, kStreamURL
));
107 scoped_refptr
<net::StringIOBuffer
> buffer(
108 new net::StringIOBuffer(kTestData1
));
110 stream
->AddData(buffer
, buffer
->size());
113 TestSuccessRequest(kStreamURL
, kTestData1
);
116 TEST_F(StreamURLRequestJobTest
, TestGetLargeStreamRequest
) {
117 scoped_refptr
<Stream
> stream(
118 new Stream(registry_
.get(), NULL
, kStreamURL
));
120 std::string large_data
;
121 large_data
.reserve(kBufferSize
* 5);
122 for (int i
= 0; i
< kBufferSize
* 5; ++i
)
123 large_data
.append(1, static_cast<char>(i
% 256));
125 scoped_refptr
<net::StringIOBuffer
> buffer(
126 new net::StringIOBuffer(large_data
));
128 stream
->AddData(buffer
, buffer
->size());
130 TestSuccessRequest(kStreamURL
, large_data
);
133 TEST_F(StreamURLRequestJobTest
, TestGetNonExistentStreamRequest
) {
134 net::TestDelegate delegate
;
135 request_
= url_request_context_
.CreateRequest(
136 kStreamURL
, net::DEFAULT_PRIORITY
, &delegate
);
137 request_
->set_method("GET");
140 base::MessageLoop::current()->RunUntilIdle();
143 EXPECT_FALSE(request_
->status().is_success());
146 TEST_F(StreamURLRequestJobTest
, TestRangeDataRequest
) {
147 scoped_refptr
<Stream
> stream(
148 new Stream(registry_
.get(), NULL
, kStreamURL
));
150 scoped_refptr
<net::StringIOBuffer
> buffer(
151 new net::StringIOBuffer(kTestData2
));
153 stream
->AddData(buffer
, buffer
->size());
156 net::HttpRequestHeaders extra_headers
;
157 extra_headers
.SetHeader(net::HttpRequestHeaders::kRange
,
158 net::HttpByteRange::Bounded(0, 3).GetHeaderValue());
159 TestRequest("GET", kStreamURL
, extra_headers
,
160 200, std::string(kTestData2
, 4));
163 TEST_F(StreamURLRequestJobTest
, TestInvalidRangeDataRequest
) {
164 scoped_refptr
<Stream
> stream(
165 new Stream(registry_
.get(), NULL
, kStreamURL
));
167 scoped_refptr
<net::StringIOBuffer
> buffer(
168 new net::StringIOBuffer(kTestData2
));
170 stream
->AddData(buffer
, buffer
->size());
173 net::HttpRequestHeaders extra_headers
;
174 extra_headers
.SetHeader(net::HttpRequestHeaders::kRange
,
175 net::HttpByteRange::Bounded(1, 3).GetHeaderValue());
176 TestRequest("GET", kStreamURL
, extra_headers
, 405, std::string());
179 } // namespace content