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 virtual 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 virtual void SetUp() {
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 virtual void TearDown() {
67 void TestSuccessRequest(const GURL
& url
,
68 const std::string
& expected_response
) {
69 TestRequest("GET", url
, net::HttpRequestHeaders(), 200, expected_response
);
72 void TestRequest(const std::string
& method
,
74 const net::HttpRequestHeaders
& extra_headers
,
75 int expected_status_code
,
76 const std::string
& expected_response
) {
77 net::TestDelegate delegate
;
78 request_
= url_request_context_
.CreateRequest(
79 url
, net::DEFAULT_PRIORITY
, &delegate
, NULL
);
80 request_
->set_method(method
);
81 if (!extra_headers
.IsEmpty())
82 request_
->SetExtraRequestHeaders(extra_headers
);
85 base::MessageLoop::current()->RunUntilIdle();
88 EXPECT_TRUE(request_
->status().is_success());
89 ASSERT_TRUE(request_
->response_headers());
90 EXPECT_EQ(expected_status_code
,
91 request_
->response_headers()->response_code());
92 EXPECT_EQ(expected_response
, delegate
.data_received());
96 base::MessageLoopForIO message_loop_
;
97 scoped_ptr
<StreamRegistry
> registry_
;
99 net::URLRequestContext url_request_context_
;
100 net::URLRequestJobFactoryImpl url_request_job_factory_
;
101 scoped_ptr
<net::URLRequest
> request_
;
104 TEST_F(StreamURLRequestJobTest
, TestGetSimpleDataRequest
) {
105 scoped_refptr
<Stream
> stream(
106 new Stream(registry_
.get(), NULL
, kStreamURL
));
108 scoped_refptr
<net::StringIOBuffer
> buffer(
109 new net::StringIOBuffer(kTestData1
));
111 stream
->AddData(buffer
, buffer
->size());
114 TestSuccessRequest(kStreamURL
, kTestData1
);
117 TEST_F(StreamURLRequestJobTest
, TestGetLargeStreamRequest
) {
118 scoped_refptr
<Stream
> stream(
119 new Stream(registry_
.get(), NULL
, kStreamURL
));
121 std::string large_data
;
122 large_data
.reserve(kBufferSize
* 5);
123 for (int i
= 0; i
< kBufferSize
* 5; ++i
)
124 large_data
.append(1, static_cast<char>(i
% 256));
126 scoped_refptr
<net::StringIOBuffer
> buffer(
127 new net::StringIOBuffer(large_data
));
129 stream
->AddData(buffer
, buffer
->size());
131 TestSuccessRequest(kStreamURL
, large_data
);
134 TEST_F(StreamURLRequestJobTest
, TestGetNonExistentStreamRequest
) {
135 net::TestDelegate delegate
;
136 request_
= url_request_context_
.CreateRequest(
137 kStreamURL
, net::DEFAULT_PRIORITY
, &delegate
, NULL
);
138 request_
->set_method("GET");
141 base::MessageLoop::current()->RunUntilIdle();
144 EXPECT_FALSE(request_
->status().is_success());
147 TEST_F(StreamURLRequestJobTest
, TestRangeDataRequest
) {
148 scoped_refptr
<Stream
> stream(
149 new Stream(registry_
.get(), NULL
, kStreamURL
));
151 scoped_refptr
<net::StringIOBuffer
> buffer(
152 new net::StringIOBuffer(kTestData2
));
154 stream
->AddData(buffer
, buffer
->size());
157 net::HttpRequestHeaders extra_headers
;
158 extra_headers
.SetHeader(net::HttpRequestHeaders::kRange
,
159 net::HttpByteRange::Bounded(0, 3).GetHeaderValue());
160 TestRequest("GET", kStreamURL
, extra_headers
,
161 200, std::string(kTestData2
, 4));
164 TEST_F(StreamURLRequestJobTest
, TestInvalidRangeDataRequest
) {
165 scoped_refptr
<Stream
> stream(
166 new Stream(registry_
.get(), NULL
, kStreamURL
));
168 scoped_refptr
<net::StringIOBuffer
> buffer(
169 new net::StringIOBuffer(kTestData2
));
171 stream
->AddData(buffer
, buffer
->size());
174 net::HttpRequestHeaders extra_headers
;
175 extra_headers
.SetHeader(net::HttpRequestHeaders::kRange
,
176 net::HttpByteRange::Bounded(1, 3).GetHeaderValue());
177 TestRequest("GET", kStreamURL
, extra_headers
, 405, std::string());
180 } // namespace content