1 // Copyright 2013 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.
10 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/format_macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/run_loop.h"
20 #include "base/strings/string_split.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/time/time.h"
24 #include "net/base/address_list.h"
25 #include "net/base/io_buffer.h"
26 #include "net/base/ip_endpoint.h"
27 #include "net/base/net_errors.h"
28 #include "net/base/net_log.h"
29 #include "net/base/net_util.h"
30 #include "net/base/test_completion_callback.h"
31 #include "net/http/http_response_headers.h"
32 #include "net/http/http_util.h"
33 #include "net/server/http_server.h"
34 #include "net/server/http_server_request_info.h"
35 #include "net/socket/tcp_client_socket.h"
36 #include "net/socket/tcp_server_socket.h"
37 #include "net/url_request/url_fetcher.h"
38 #include "net/url_request/url_fetcher_delegate.h"
39 #include "net/url_request/url_request_context.h"
40 #include "net/url_request/url_request_context_getter.h"
41 #include "net/url_request/url_request_test_util.h"
42 #include "testing/gtest/include/gtest/gtest.h"
48 const int kMaxExpectedResponseLength
= 2048;
50 void SetTimedOutAndQuitLoop(const base::WeakPtr
<bool> timed_out
,
51 const base::Closure
& quit_loop_func
) {
58 bool RunLoopWithTimeout(base::RunLoop
* run_loop
) {
59 bool timed_out
= false;
60 base::WeakPtrFactory
<bool> timed_out_weak_factory(&timed_out
);
61 base::MessageLoop::current()->PostDelayedTask(
63 base::Bind(&SetTimedOutAndQuitLoop
,
64 timed_out_weak_factory
.GetWeakPtr(),
65 run_loop
->QuitClosure()),
66 base::TimeDelta::FromSeconds(1));
71 class TestHttpClient
{
73 TestHttpClient() : connect_result_(OK
) {}
75 int ConnectAndWait(const IPEndPoint
& address
) {
76 AddressList
addresses(address
);
77 NetLog::Source source
;
78 socket_
.reset(new TCPClientSocket(addresses
, NULL
, source
));
80 base::RunLoop run_loop
;
81 connect_result_
= socket_
->Connect(base::Bind(&TestHttpClient::OnConnect
,
82 base::Unretained(this),
83 run_loop
.QuitClosure()));
84 if (connect_result_
!= OK
&& connect_result_
!= ERR_IO_PENDING
)
85 return connect_result_
;
87 if (!RunLoopWithTimeout(&run_loop
))
89 return connect_result_
;
92 void Send(const std::string
& data
) {
94 new DrainableIOBuffer(new StringIOBuffer(data
), data
.length());
98 bool Read(std::string
* message
, int expected_bytes
) {
99 int total_bytes_received
= 0;
101 while (total_bytes_received
< expected_bytes
) {
102 net::TestCompletionCallback callback
;
103 ReadInternal(callback
.callback());
104 int bytes_received
= callback
.WaitForResult();
105 if (bytes_received
<= 0)
108 total_bytes_received
+= bytes_received
;
109 message
->append(read_buffer_
->data(), bytes_received
);
114 bool ReadResponse(std::string
* message
) {
115 if (!Read(message
, 1))
117 while (!IsCompleteResponse(*message
)) {
119 if (!Read(&chunk
, 1))
121 message
->append(chunk
);
127 void OnConnect(const base::Closure
& quit_loop
, int result
) {
128 connect_result_
= result
;
133 int result
= socket_
->Write(
135 write_buffer_
->BytesRemaining(),
136 base::Bind(&TestHttpClient::OnWrite
, base::Unretained(this)));
137 if (result
!= ERR_IO_PENDING
)
141 void OnWrite(int result
) {
142 ASSERT_GT(result
, 0);
143 write_buffer_
->DidConsume(result
);
144 if (write_buffer_
->BytesRemaining())
148 void ReadInternal(const net::CompletionCallback
& callback
) {
149 read_buffer_
= new IOBufferWithSize(kMaxExpectedResponseLength
);
151 socket_
->Read(read_buffer_
.get(), kMaxExpectedResponseLength
, callback
);
152 if (result
!= ERR_IO_PENDING
)
153 callback
.Run(result
);
156 bool IsCompleteResponse(const std::string
& response
) {
157 // Check end of headers first.
158 int end_of_headers
= HttpUtil::LocateEndOfHeaders(response
.data(),
160 if (end_of_headers
< 0)
163 // Return true if response has data equal to or more than content length.
164 int64 body_size
= static_cast<int64
>(response
.size()) - end_of_headers
;
165 DCHECK_LE(0, body_size
);
166 scoped_refptr
<HttpResponseHeaders
> headers(new HttpResponseHeaders(
167 HttpUtil::AssembleRawHeaders(response
.data(), end_of_headers
)));
168 return body_size
>= headers
->GetContentLength();
171 scoped_refptr
<IOBufferWithSize
> read_buffer_
;
172 scoped_refptr
<DrainableIOBuffer
> write_buffer_
;
173 scoped_ptr
<TCPClientSocket
> socket_
;
179 class HttpServerTest
: public testing::Test
,
180 public HttpServer::Delegate
{
182 HttpServerTest() : quit_after_request_count_(0) {}
184 virtual void SetUp() OVERRIDE
{
185 scoped_ptr
<ServerSocket
> server_socket(
186 new TCPServerSocket(NULL
, net::NetLog::Source()));
187 server_socket
->ListenWithAddressAndPort("127.0.0.1", 0, 1);
188 server_
.reset(new HttpServer(server_socket
.Pass(), this));
189 ASSERT_EQ(OK
, server_
->GetLocalAddress(&server_address_
));
192 virtual void OnHttpRequest(int connection_id
,
193 const HttpServerRequestInfo
& info
) OVERRIDE
{
194 requests_
.push_back(std::make_pair(info
, connection_id
));
195 if (requests_
.size() == quit_after_request_count_
)
196 run_loop_quit_func_
.Run();
199 virtual void OnWebSocketRequest(int connection_id
,
200 const HttpServerRequestInfo
& info
) OVERRIDE
{
204 virtual void OnWebSocketMessage(int connection_id
,
205 const std::string
& data
) OVERRIDE
{
209 virtual void OnClose(int connection_id
) OVERRIDE
{}
211 bool RunUntilRequestsReceived(size_t count
) {
212 quit_after_request_count_
= count
;
213 if (requests_
.size() == count
)
216 base::RunLoop run_loop
;
217 run_loop_quit_func_
= run_loop
.QuitClosure();
218 bool success
= RunLoopWithTimeout(&run_loop
);
219 run_loop_quit_func_
.Reset();
223 HttpServerRequestInfo
GetRequest(size_t request_index
) {
224 return requests_
[request_index
].first
;
227 int GetConnectionId(size_t request_index
) {
228 return requests_
[request_index
].second
;
231 void HandleAcceptResult(scoped_ptr
<StreamSocket
> socket
) {
232 server_
->accepted_socket_
.reset(socket
.release());
233 server_
->HandleAcceptResult(OK
);
237 scoped_ptr
<HttpServer
> server_
;
238 IPEndPoint server_address_
;
239 base::Closure run_loop_quit_func_
;
240 std::vector
<std::pair
<HttpServerRequestInfo
, int> > requests_
;
243 size_t quit_after_request_count_
;
246 class WebSocketTest
: public HttpServerTest
{
247 virtual void OnHttpRequest(int connection_id
,
248 const HttpServerRequestInfo
& info
) OVERRIDE
{
252 virtual void OnWebSocketRequest(int connection_id
,
253 const HttpServerRequestInfo
& info
) OVERRIDE
{
254 HttpServerTest::OnHttpRequest(connection_id
, info
);
257 virtual void OnWebSocketMessage(int connection_id
,
258 const std::string
& data
) OVERRIDE
{
262 TEST_F(HttpServerTest
, Request
) {
263 TestHttpClient client
;
264 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
265 client
.Send("GET /test HTTP/1.1\r\n\r\n");
266 ASSERT_TRUE(RunUntilRequestsReceived(1));
267 ASSERT_EQ("GET", GetRequest(0).method
);
268 ASSERT_EQ("/test", GetRequest(0).path
);
269 ASSERT_EQ("", GetRequest(0).data
);
270 ASSERT_EQ(0u, GetRequest(0).headers
.size());
271 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer
.ToString(),
276 TEST_F(HttpServerTest
, RequestWithHeaders
) {
277 TestHttpClient client
;
278 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
279 const char* kHeaders
[][3] = {
280 {"Header", ": ", "1"},
281 {"HeaderWithNoWhitespace", ":", "1"},
282 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "},
283 {"HeaderWithColon", ": ", "1:1"},
284 {"EmptyHeader", ":", ""},
285 {"EmptyHeaderWithWhitespace", ": \t ", ""},
286 {"HeaderWithNonASCII", ": ", "\xf7"},
289 for (size_t i
= 0; i
< arraysize(kHeaders
); ++i
) {
291 std::string(kHeaders
[i
][0]) + kHeaders
[i
][1] + kHeaders
[i
][2] + "\r\n";
294 client
.Send("GET /test HTTP/1.1\r\n" + headers
+ "\r\n");
295 ASSERT_TRUE(RunUntilRequestsReceived(1));
296 ASSERT_EQ("", GetRequest(0).data
);
298 for (size_t i
= 0; i
< arraysize(kHeaders
); ++i
) {
299 std::string field
= base::StringToLowerASCII(std::string(kHeaders
[i
][0]));
300 std::string value
= kHeaders
[i
][2];
301 ASSERT_EQ(1u, GetRequest(0).headers
.count(field
)) << field
;
302 ASSERT_EQ(value
, GetRequest(0).headers
[field
]) << kHeaders
[i
][0];
306 TEST_F(HttpServerTest
, RequestWithDuplicateHeaders
) {
307 TestHttpClient client
;
308 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
309 const char* kHeaders
[][3] = {
310 {"FirstHeader", ": ", "1"},
311 {"DuplicateHeader", ": ", "2"},
312 {"MiddleHeader", ": ", "3"},
313 {"DuplicateHeader", ": ", "4"},
314 {"LastHeader", ": ", "5"},
317 for (size_t i
= 0; i
< arraysize(kHeaders
); ++i
) {
319 std::string(kHeaders
[i
][0]) + kHeaders
[i
][1] + kHeaders
[i
][2] + "\r\n";
322 client
.Send("GET /test HTTP/1.1\r\n" + headers
+ "\r\n");
323 ASSERT_TRUE(RunUntilRequestsReceived(1));
324 ASSERT_EQ("", GetRequest(0).data
);
326 for (size_t i
= 0; i
< arraysize(kHeaders
); ++i
) {
327 std::string field
= base::StringToLowerASCII(std::string(kHeaders
[i
][0]));
328 std::string value
= (field
== "duplicateheader") ? "2,4" : kHeaders
[i
][2];
329 ASSERT_EQ(1u, GetRequest(0).headers
.count(field
)) << field
;
330 ASSERT_EQ(value
, GetRequest(0).headers
[field
]) << kHeaders
[i
][0];
334 TEST_F(HttpServerTest
, HasHeaderValueTest
) {
335 TestHttpClient client
;
336 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
337 const char* kHeaders
[] = {
339 "HeaderWithNoWhitespace:E",
340 "HeaderWithWhitespace : \t f \t ",
341 "DuplicateHeader: g",
342 "HeaderWithComma: h, i ,j",
343 "DuplicateHeader: k",
345 "EmptyHeaderWithWhitespace: \t ",
346 "HeaderWithNonASCII: \xf7",
349 for (size_t i
= 0; i
< arraysize(kHeaders
); ++i
) {
350 headers
+= std::string(kHeaders
[i
]) + "\r\n";
353 client
.Send("GET /test HTTP/1.1\r\n" + headers
+ "\r\n");
354 ASSERT_TRUE(RunUntilRequestsReceived(1));
355 ASSERT_EQ("", GetRequest(0).data
);
357 ASSERT_TRUE(GetRequest(0).HasHeaderValue("header", "abcd"));
358 ASSERT_FALSE(GetRequest(0).HasHeaderValue("header", "bc"));
359 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnowhitespace", "e"));
360 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithwhitespace", "f"));
361 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "g"));
362 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "h"));
363 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "i"));
364 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "j"));
365 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "k"));
366 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheader", "x"));
367 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheaderwithwhitespace", "x"));
368 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnonascii", "\xf7"));
371 TEST_F(HttpServerTest
, RequestWithBody
) {
372 TestHttpClient client
;
373 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
374 std::string body
= "a" + std::string(1 << 10, 'b') + "c";
375 client
.Send(base::StringPrintf(
376 "GET /test HTTP/1.1\r\n"
378 "Content-Length: %" PRIuS
"\r\n\r\n%s",
381 ASSERT_TRUE(RunUntilRequestsReceived(1));
382 ASSERT_EQ(2u, GetRequest(0).headers
.size());
383 ASSERT_EQ(body
.length(), GetRequest(0).data
.length());
384 ASSERT_EQ('a', body
[0]);
385 ASSERT_EQ('c', *body
.rbegin());
388 TEST_F(WebSocketTest
, RequestWebSocket
) {
389 TestHttpClient client
;
390 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
392 "GET /test HTTP/1.1\r\n"
393 "Upgrade: WebSocket\r\n"
394 "Connection: SomethingElse, Upgrade\r\n"
395 "Sec-WebSocket-Version: 8\r\n"
396 "Sec-WebSocket-Key: key\r\n"
398 ASSERT_TRUE(RunUntilRequestsReceived(1));
401 TEST_F(HttpServerTest
, RequestWithTooLargeBody
) {
402 class TestURLFetcherDelegate
: public URLFetcherDelegate
{
404 TestURLFetcherDelegate(const base::Closure
& quit_loop_func
)
405 : quit_loop_func_(quit_loop_func
) {}
406 virtual ~TestURLFetcherDelegate() {}
408 virtual void OnURLFetchComplete(const URLFetcher
* source
) OVERRIDE
{
409 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR
, source
->GetResponseCode());
410 quit_loop_func_
.Run();
414 base::Closure quit_loop_func_
;
417 base::RunLoop run_loop
;
418 TestURLFetcherDelegate
delegate(run_loop
.QuitClosure());
420 scoped_refptr
<URLRequestContextGetter
> request_context_getter(
421 new TestURLRequestContextGetter(base::MessageLoopProxy::current()));
422 scoped_ptr
<URLFetcher
> fetcher(
423 URLFetcher::Create(GURL(base::StringPrintf("http://127.0.0.1:%d/test",
424 server_address_
.port())),
427 fetcher
->SetRequestContext(request_context_getter
.get());
428 fetcher
->AddExtraRequestHeader(
429 base::StringPrintf("content-length:%d", 1 << 30));
432 ASSERT_TRUE(RunLoopWithTimeout(&run_loop
));
433 ASSERT_EQ(0u, requests_
.size());
436 TEST_F(HttpServerTest
, Send200
) {
437 TestHttpClient client
;
438 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
439 client
.Send("GET /test HTTP/1.1\r\n\r\n");
440 ASSERT_TRUE(RunUntilRequestsReceived(1));
441 server_
->Send200(GetConnectionId(0), "Response!", "text/plain");
443 std::string response
;
444 ASSERT_TRUE(client
.ReadResponse(&response
));
445 ASSERT_TRUE(StartsWithASCII(response
, "HTTP/1.1 200 OK", true));
446 ASSERT_TRUE(EndsWith(response
, "Response!", true));
449 TEST_F(HttpServerTest
, SendRaw
) {
450 TestHttpClient client
;
451 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
452 client
.Send("GET /test HTTP/1.1\r\n\r\n");
453 ASSERT_TRUE(RunUntilRequestsReceived(1));
454 server_
->SendRaw(GetConnectionId(0), "Raw Data ");
455 server_
->SendRaw(GetConnectionId(0), "More Data");
456 server_
->SendRaw(GetConnectionId(0), "Third Piece of Data");
458 const std::string
expected_response("Raw Data More DataThird Piece of Data");
459 std::string response
;
460 ASSERT_TRUE(client
.Read(&response
, expected_response
.length()));
461 ASSERT_EQ(expected_response
, response
);
466 class MockStreamSocket
: public StreamSocket
{
474 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
{
475 return ERR_NOT_IMPLEMENTED
;
477 virtual void Disconnect() OVERRIDE
{
479 if (!read_callback_
.is_null()) {
482 base::ResetAndReturn(&read_callback_
).Run(ERR_CONNECTION_CLOSED
);
485 virtual bool IsConnected() const OVERRIDE
{ return connected_
; }
486 virtual bool IsConnectedAndIdle() const OVERRIDE
{ return IsConnected(); }
487 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
{
488 return ERR_NOT_IMPLEMENTED
;
490 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
{
491 return ERR_NOT_IMPLEMENTED
;
493 virtual const BoundNetLog
& NetLog() const OVERRIDE
{ return net_log_
; }
494 virtual void SetSubresourceSpeculation() OVERRIDE
{}
495 virtual void SetOmniboxSpeculation() OVERRIDE
{}
496 virtual bool WasEverUsed() const OVERRIDE
{ return true; }
497 virtual bool UsingTCPFastOpen() const OVERRIDE
{ return false; }
498 virtual bool WasNpnNegotiated() const OVERRIDE
{ return false; }
499 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
{
500 return kProtoUnknown
;
502 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
{ return false; }
505 virtual int Read(IOBuffer
* buf
, int buf_len
,
506 const CompletionCallback
& callback
) OVERRIDE
{
508 return ERR_SOCKET_NOT_CONNECTED
;
510 if (pending_read_data_
.empty()) {
512 read_buf_len_
= buf_len
;
513 read_callback_
= callback
;
514 return ERR_IO_PENDING
;
516 DCHECK_GT(buf_len
, 0);
517 int read_len
= std::min(static_cast<int>(pending_read_data_
.size()),
519 memcpy(buf
->data(), pending_read_data_
.data(), read_len
);
520 pending_read_data_
.erase(0, read_len
);
523 virtual int Write(IOBuffer
* buf
, int buf_len
,
524 const CompletionCallback
& callback
) OVERRIDE
{
525 return ERR_NOT_IMPLEMENTED
;
527 virtual int SetReceiveBufferSize(int32 size
) OVERRIDE
{
528 return ERR_NOT_IMPLEMENTED
;
530 virtual int SetSendBufferSize(int32 size
) OVERRIDE
{
531 return ERR_NOT_IMPLEMENTED
;
534 void DidRead(const char* data
, int data_len
) {
535 if (!read_buf_
.get()) {
536 pending_read_data_
.append(data
, data_len
);
539 int read_len
= std::min(data_len
, read_buf_len_
);
540 memcpy(read_buf_
->data(), data
, read_len
);
541 pending_read_data_
.assign(data
+ read_len
, data_len
- read_len
);
544 base::ResetAndReturn(&read_callback_
).Run(read_len
);
548 virtual ~MockStreamSocket() {}
551 scoped_refptr
<IOBuffer
> read_buf_
;
553 CompletionCallback read_callback_
;
554 std::string pending_read_data_
;
555 BoundNetLog net_log_
;
557 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket
);
562 TEST_F(HttpServerTest
, RequestWithBodySplitAcrossPackets
) {
563 MockStreamSocket
* socket
= new MockStreamSocket();
564 HandleAcceptResult(make_scoped_ptr
<StreamSocket
>(socket
));
565 std::string
body("body");
566 std::string request_text
= base::StringPrintf(
567 "GET /test HTTP/1.1\r\n"
569 "Content-Length: %" PRIuS
"\r\n\r\n%s",
572 socket
->DidRead(request_text
.c_str(), request_text
.length() - 2);
573 ASSERT_EQ(0u, requests_
.size());
574 socket
->DidRead(request_text
.c_str() + request_text
.length() - 2, 2);
575 ASSERT_EQ(1u, requests_
.size());
576 ASSERT_EQ(body
, GetRequest(0).data
);
579 TEST_F(HttpServerTest
, MultipleRequestsOnSameConnection
) {
580 // The idea behind this test is that requests with or without bodies should
581 // not break parsing of the next request.
582 TestHttpClient client
;
583 ASSERT_EQ(OK
, client
.ConnectAndWait(server_address_
));
584 std::string body
= "body";
585 client
.Send(base::StringPrintf(
586 "GET /test HTTP/1.1\r\n"
587 "Content-Length: %" PRIuS
"\r\n\r\n%s",
590 ASSERT_TRUE(RunUntilRequestsReceived(1));
591 ASSERT_EQ(body
, GetRequest(0).data
);
593 int client_connection_id
= GetConnectionId(0);
594 server_
->Send200(client_connection_id
, "Content for /test", "text/plain");
595 std::string response1
;
596 ASSERT_TRUE(client
.ReadResponse(&response1
));
597 ASSERT_TRUE(StartsWithASCII(response1
, "HTTP/1.1 200 OK", true));
598 ASSERT_TRUE(EndsWith(response1
, "Content for /test", true));
600 client
.Send("GET /test2 HTTP/1.1\r\n\r\n");
601 ASSERT_TRUE(RunUntilRequestsReceived(2));
602 ASSERT_EQ("/test2", GetRequest(1).path
);
604 ASSERT_EQ(client_connection_id
, GetConnectionId(1));
605 server_
->Send404(client_connection_id
);
606 std::string response2
;
607 ASSERT_TRUE(client
.ReadResponse(&response2
));
608 ASSERT_TRUE(StartsWithASCII(response2
, "HTTP/1.1 404 Not Found", true));
610 client
.Send("GET /test3 HTTP/1.1\r\n\r\n");
611 ASSERT_TRUE(RunUntilRequestsReceived(3));
612 ASSERT_EQ("/test3", GetRequest(2).path
);
614 ASSERT_EQ(client_connection_id
, GetConnectionId(2));
615 server_
->Send200(client_connection_id
, "Content for /test3", "text/plain");
616 std::string response3
;
617 ASSERT_TRUE(client
.ReadResponse(&response3
));
618 ASSERT_TRUE(StartsWithASCII(response3
, "HTTP/1.1 200 OK", true));
619 ASSERT_TRUE(EndsWith(response3
, "Content for /test3", true));