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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 protected: | 202 protected: |
203 scoped_refptr<HttpServer> server_; | 203 scoped_refptr<HttpServer> server_; |
204 IPEndPoint server_address_; | 204 IPEndPoint server_address_; |
205 base::Closure run_loop_quit_func_; | 205 base::Closure run_loop_quit_func_; |
206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
207 | 207 |
208 private: | 208 private: |
209 size_t quit_after_request_count_; | 209 size_t quit_after_request_count_; |
210 }; | 210 }; |
211 | 211 |
| 212 class WebSocketTest : public HttpServerTest { |
| 213 virtual void OnHttpRequest(int connection_id, |
| 214 const HttpServerRequestInfo& info) OVERRIDE { |
| 215 NOTREACHED(); |
| 216 } |
| 217 |
| 218 virtual void OnWebSocketRequest(int connection_id, |
| 219 const HttpServerRequestInfo& info) OVERRIDE { |
| 220 HttpServerTest::OnHttpRequest(connection_id, info); |
| 221 } |
| 222 |
| 223 virtual void OnWebSocketMessage(int connection_id, |
| 224 const std::string& data) OVERRIDE { |
| 225 } |
| 226 }; |
| 227 |
212 TEST_F(HttpServerTest, Request) { | 228 TEST_F(HttpServerTest, Request) { |
213 TestHttpClient client; | 229 TestHttpClient client; |
214 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 230 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
215 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 231 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
216 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 232 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
217 ASSERT_EQ("GET", GetRequest(0).method); | 233 ASSERT_EQ("GET", GetRequest(0).method); |
218 ASSERT_EQ("/test", GetRequest(0).path); | 234 ASSERT_EQ("/test", GetRequest(0).path); |
219 ASSERT_EQ("", GetRequest(0).data); | 235 ASSERT_EQ("", GetRequest(0).data); |
220 ASSERT_EQ(0u, GetRequest(0).headers.size()); | 236 ASSERT_EQ(0u, GetRequest(0).headers.size()); |
221 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), | 237 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), |
(...skipping 24 matching lines...) Expand all Loading... |
246 ASSERT_EQ("", GetRequest(0).data); | 262 ASSERT_EQ("", GetRequest(0).data); |
247 | 263 |
248 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 264 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
249 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); | 265 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); |
250 std::string value = kHeaders[i][2]; | 266 std::string value = kHeaders[i][2]; |
251 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; | 267 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
252 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; | 268 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
253 } | 269 } |
254 } | 270 } |
255 | 271 |
| 272 TEST_F(HttpServerTest, RequestWithDuplicateHeaders) { |
| 273 TestHttpClient client; |
| 274 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 275 const char* kHeaders[][3] = { |
| 276 {"FirstHeader", ": ", "1"}, |
| 277 {"DuplicateHeader", ": ", "2"}, |
| 278 {"MiddleHeader", ": ", "3"}, |
| 279 {"DuplicateHeader", ": ", "4"}, |
| 280 {"LastHeader", ": ", "5"}, |
| 281 }; |
| 282 std::string headers; |
| 283 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 284 headers += |
| 285 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; |
| 286 } |
| 287 |
| 288 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 289 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 290 ASSERT_EQ("", GetRequest(0).data); |
| 291 |
| 292 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 293 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); |
| 294 std::string value = (field == "duplicateheader") ? "2,4" : kHeaders[i][2]; |
| 295 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
| 296 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
| 297 } |
| 298 } |
| 299 |
| 300 TEST_F(HttpServerTest, HasHeaderValueTest) { |
| 301 TestHttpClient client; |
| 302 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 303 const char* kHeaders[] = { |
| 304 "Header: Abcd", |
| 305 "HeaderWithNoWhitespace:E", |
| 306 "HeaderWithWhitespace : \t f \t ", |
| 307 "DuplicateHeader: g", |
| 308 "HeaderWithComma: h, i ,j", |
| 309 "DuplicateHeader: k", |
| 310 "EmptyHeader:", |
| 311 "EmptyHeaderWithWhitespace: \t ", |
| 312 "HeaderWithNonASCII: \xf7", |
| 313 }; |
| 314 std::string headers; |
| 315 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 316 headers += std::string(kHeaders[i]) + "\r\n"; |
| 317 } |
| 318 |
| 319 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 320 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 321 ASSERT_EQ("", GetRequest(0).data); |
| 322 |
| 323 ASSERT_TRUE(GetRequest(0).HasHeaderValue("header", "abcd")); |
| 324 ASSERT_FALSE(GetRequest(0).HasHeaderValue("header", "bc")); |
| 325 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnowhitespace", "e")); |
| 326 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithwhitespace", "f")); |
| 327 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "g")); |
| 328 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "h")); |
| 329 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "i")); |
| 330 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "j")); |
| 331 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "k")); |
| 332 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheader", "x")); |
| 333 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheaderwithwhitespace", "x")); |
| 334 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnonascii", "\xf7")); |
| 335 } |
| 336 |
256 TEST_F(HttpServerTest, RequestWithBody) { | 337 TEST_F(HttpServerTest, RequestWithBody) { |
257 TestHttpClient client; | 338 TestHttpClient client; |
258 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 339 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
259 std::string body = "a" + std::string(1 << 10, 'b') + "c"; | 340 std::string body = "a" + std::string(1 << 10, 'b') + "c"; |
260 client.Send(base::StringPrintf( | 341 client.Send(base::StringPrintf( |
261 "GET /test HTTP/1.1\r\n" | 342 "GET /test HTTP/1.1\r\n" |
262 "SomeHeader: 1\r\n" | 343 "SomeHeader: 1\r\n" |
263 "Content-Length: %" PRIuS "\r\n\r\n%s", | 344 "Content-Length: %" PRIuS "\r\n\r\n%s", |
264 body.length(), | 345 body.length(), |
265 body.c_str())); | 346 body.c_str())); |
266 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 347 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
267 ASSERT_EQ(2u, GetRequest(0).headers.size()); | 348 ASSERT_EQ(2u, GetRequest(0).headers.size()); |
268 ASSERT_EQ(body.length(), GetRequest(0).data.length()); | 349 ASSERT_EQ(body.length(), GetRequest(0).data.length()); |
269 ASSERT_EQ('a', body[0]); | 350 ASSERT_EQ('a', body[0]); |
270 ASSERT_EQ('c', *body.rbegin()); | 351 ASSERT_EQ('c', *body.rbegin()); |
271 } | 352 } |
272 | 353 |
| 354 TEST_F(WebSocketTest, RequestWebSocket) { |
| 355 TestHttpClient client; |
| 356 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 357 client.Send( |
| 358 "GET /test HTTP/1.1\r\n" |
| 359 "Upgrade: WebSocket\r\n" |
| 360 "Connection: SomethingElse, Upgrade\r\n" |
| 361 "Sec-WebSocket-Version: 8\r\n" |
| 362 "Sec-WebSocket-Key: key\r\n" |
| 363 "\r\n"); |
| 364 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 365 } |
| 366 |
273 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 367 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
274 class TestURLFetcherDelegate : public URLFetcherDelegate { | 368 class TestURLFetcherDelegate : public URLFetcherDelegate { |
275 public: | 369 public: |
276 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 370 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
277 : quit_loop_func_(quit_loop_func) {} | 371 : quit_loop_func_(quit_loop_func) {} |
278 virtual ~TestURLFetcherDelegate() {} | 372 virtual ~TestURLFetcherDelegate() {} |
279 | 373 |
280 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 374 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { |
281 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); | 375 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); |
282 quit_loop_func_.Run(); | 376 quit_loop_func_.Run(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 | 497 |
404 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 498 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
405 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 499 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
406 std::string response3; | 500 std::string response3; |
407 ASSERT_TRUE(client.Read(&response3)); | 501 ASSERT_TRUE(client.Read(&response3)); |
408 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 502 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
409 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 503 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
410 } | 504 } |
411 | 505 |
412 } // namespace net | 506 } // namespace net |
OLD | NEW |