OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 return ERR_TIMED_OUT; | 83 return ERR_TIMED_OUT; |
84 return connect_result_; | 84 return connect_result_; |
85 } | 85 } |
86 | 86 |
87 void Send(const std::string& data) { | 87 void Send(const std::string& data) { |
88 write_buffer_ = | 88 write_buffer_ = |
89 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); | 89 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); |
90 Write(); | 90 Write(); |
91 } | 91 } |
92 | 92 |
93 bool Read(std::string* message) { | 93 bool Read(std::string* message) { return Read(message, 1); } |
94 return Read(message, 1); | |
95 } | |
96 | 94 |
97 bool Read(std::string* message, int expected_bytes) { | 95 bool Read(std::string* message, int expected_bytes) { |
98 int total_bytes_received = 0; | 96 int total_bytes_received = 0; |
99 message->clear(); | 97 message->clear(); |
100 while (total_bytes_received < expected_bytes) { | 98 while (total_bytes_received < expected_bytes) { |
101 net::TestCompletionCallback callback; | 99 net::TestCompletionCallback callback; |
102 ReadInternal(callback.callback()); | 100 ReadInternal(callback.callback()); |
103 int bytes_received = callback.WaitForResult(); | 101 int bytes_received = callback.WaitForResult(); |
104 if (bytes_received <= 0) | 102 if (bytes_received <= 0) |
105 return false; | 103 return false; |
(...skipping 21 matching lines...) Expand all Loading... |
127 | 125 |
128 void OnWrite(int result) { | 126 void OnWrite(int result) { |
129 ASSERT_GT(result, 0); | 127 ASSERT_GT(result, 0); |
130 write_buffer_->DidConsume(result); | 128 write_buffer_->DidConsume(result); |
131 if (write_buffer_->BytesRemaining()) | 129 if (write_buffer_->BytesRemaining()) |
132 Write(); | 130 Write(); |
133 } | 131 } |
134 | 132 |
135 void ReadInternal(const net::CompletionCallback& callback) { | 133 void ReadInternal(const net::CompletionCallback& callback) { |
136 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); | 134 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); |
137 int result = socket_->Read(read_buffer_, | 135 int result = |
138 kMaxExpectedResponseLength, | 136 socket_->Read(read_buffer_, kMaxExpectedResponseLength, callback); |
139 callback); | |
140 if (result != ERR_IO_PENDING) | 137 if (result != ERR_IO_PENDING) |
141 callback.Run(result); | 138 callback.Run(result); |
142 } | 139 } |
143 | 140 |
144 scoped_refptr<IOBufferWithSize> read_buffer_; | 141 scoped_refptr<IOBufferWithSize> read_buffer_; |
145 scoped_refptr<DrainableIOBuffer> write_buffer_; | 142 scoped_refptr<DrainableIOBuffer> write_buffer_; |
146 scoped_ptr<TCPClientSocket> socket_; | 143 scoped_ptr<TCPClientSocket> socket_; |
147 int connect_result_; | 144 int connect_result_; |
148 }; | 145 }; |
149 | 146 |
150 } // namespace | 147 } // namespace |
151 | 148 |
152 class HttpServerTest : public testing::Test, | 149 class HttpServerTest : public testing::Test, public HttpServer::Delegate { |
153 public HttpServer::Delegate { | |
154 public: | 150 public: |
155 HttpServerTest() : quit_after_request_count_(0) {} | 151 HttpServerTest() : quit_after_request_count_(0) {} |
156 | 152 |
157 virtual void SetUp() OVERRIDE { | 153 virtual void SetUp() OVERRIDE { |
158 TCPListenSocketFactory socket_factory("127.0.0.1", 0); | 154 TCPListenSocketFactory socket_factory("127.0.0.1", 0); |
159 server_ = new HttpServer(socket_factory, this); | 155 server_ = new HttpServer(socket_factory, this); |
160 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 156 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
161 } | 157 } |
162 | 158 |
163 virtual void OnHttpRequest(int connection_id, | 159 virtual void OnHttpRequest(int connection_id, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 207 |
212 TEST_F(HttpServerTest, Request) { | 208 TEST_F(HttpServerTest, Request) { |
213 TestHttpClient client; | 209 TestHttpClient client; |
214 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 210 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
215 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 211 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
216 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 212 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
217 ASSERT_EQ("GET", GetRequest(0).method); | 213 ASSERT_EQ("GET", GetRequest(0).method); |
218 ASSERT_EQ("/test", GetRequest(0).path); | 214 ASSERT_EQ("/test", GetRequest(0).path); |
219 ASSERT_EQ("", GetRequest(0).data); | 215 ASSERT_EQ("", GetRequest(0).data); |
220 ASSERT_EQ(0u, GetRequest(0).headers.size()); | 216 ASSERT_EQ(0u, GetRequest(0).headers.size()); |
221 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), | 217 ASSERT_TRUE( |
222 "127.0.0.1", | 218 StartsWithASCII(GetRequest(0).peer.ToString(), "127.0.0.1", true)); |
223 true)); | |
224 } | 219 } |
225 | 220 |
226 TEST_F(HttpServerTest, RequestWithHeaders) { | 221 TEST_F(HttpServerTest, RequestWithHeaders) { |
227 TestHttpClient client; | 222 TestHttpClient client; |
228 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 223 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
229 const char* kHeaders[][3] = { | 224 const char* kHeaders[][3] = { |
230 {"Header", ": ", "1"}, | 225 {"Header", ": ", "1"}, |
231 {"HeaderWithNoWhitespace", ":", "1"}, | 226 {"HeaderWithNoWhitespace", ":", "1"}, |
232 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, | 227 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, |
233 {"HeaderWithColon", ": ", "1:1"}, | 228 {"HeaderWithColon", ": ", "1:1"}, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 337 |
343 virtual void Accept() OVERRIDE { NOTREACHED(); } | 338 virtual void Accept() OVERRIDE { NOTREACHED(); } |
344 | 339 |
345 private: | 340 private: |
346 virtual ~MockStreamListenSocket() {} | 341 virtual ~MockStreamListenSocket() {} |
347 }; | 342 }; |
348 | 343 |
349 } // namespace | 344 } // namespace |
350 | 345 |
351 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 346 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
352 StreamListenSocket* socket = | 347 StreamListenSocket* socket = new MockStreamListenSocket(server_.get()); |
353 new MockStreamListenSocket(server_.get()); | |
354 server_->DidAccept(NULL, make_scoped_ptr(socket)); | 348 server_->DidAccept(NULL, make_scoped_ptr(socket)); |
355 std::string body("body"); | 349 std::string body("body"); |
356 std::string request_text = base::StringPrintf( | 350 std::string request_text = base::StringPrintf( |
357 "GET /test HTTP/1.1\r\n" | 351 "GET /test HTTP/1.1\r\n" |
358 "SomeHeader: 1\r\n" | 352 "SomeHeader: 1\r\n" |
359 "Content-Length: %" PRIuS "\r\n\r\n%s", | 353 "Content-Length: %" PRIuS "\r\n\r\n%s", |
360 body.length(), | 354 body.length(), |
361 body.c_str()); | 355 body.c_str()); |
362 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); | 356 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); |
363 ASSERT_EQ(0u, requests_.size()); | 357 ASSERT_EQ(0u, requests_.size()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 | 397 |
404 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 398 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
405 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 399 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
406 std::string response3; | 400 std::string response3; |
407 ASSERT_TRUE(client.Read(&response3)); | 401 ASSERT_TRUE(client.Read(&response3)); |
408 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 402 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
409 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 403 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
410 } | 404 } |
411 | 405 |
412 } // namespace net | 406 } // namespace net |
OLD | NEW |