Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/google_apis/test_server/http_request.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace drive { | |
| 10 namespace test_server { | |
| 11 | |
| 12 TEST(HttpRequestTest, ParseRequest) { | |
| 13 HttpRequestParser parser; | |
| 14 | |
| 15 // Process request in chunks to check if the parser deals with border cases. | |
| 16 // Also, check multi-line headers as well as multiple requests in the same | |
| 17 // chunk. This basically should cover all the simplest border cases. | |
| 18 parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n"); | |
| 19 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 20 parser.ProcessChunk("Host: localhost:1234\r\n"); | |
| 21 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 22 parser.ProcessChunk("Multi-line-header: abcd\r\n"); | |
| 23 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 24 parser.ProcessChunk(" efgh\r\n"); | |
| 25 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 26 parser.ProcessChunk(" ijkl\r\n"); | |
| 27 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 28 parser.ProcessChunk("Content-Length: 10\r\n\r\n"); | |
| 29 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 30 // Content data and another request in the same chunk. | |
| 31 parser.ProcessChunk("1234567890GET /another.html HTTP/1.1\r\n\r\n"); | |
| 32 ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); | |
| 33 | |
| 34 // Fetch the first request and validate it. | |
| 35 { | |
| 36 scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
| 37 EXPECT_EQ("http://localhost/foobar.html", request->uri.spec()); | |
| 38 EXPECT_EQ(POST, request->method); | |
| 39 EXPECT_EQ("1234567890", request->content); | |
| 40 ASSERT_EQ(3u, request->headers.size()); | |
| 41 | |
| 42 EXPECT_EQ(1u, request->headers.count("Host")); | |
| 43 EXPECT_EQ(1u, request->headers.count("Multi-line-header")); | |
| 44 EXPECT_EQ(1u, request->headers.count("Content-Length")); | |
| 45 | |
| 46 EXPECT_EQ("localhost:1234", request->headers["Host"]); | |
| 47 EXPECT_EQ("abcd efgh ijkl", request->headers["Multi-line-header"]); | |
| 48 EXPECT_EQ("10", request->headers["Content-Length"]); | |
| 49 } | |
| 50 | |
| 51 // Fetch the second request and roughly check it. | |
| 52 { | |
| 53 ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); | |
| 54 scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
| 55 EXPECT_EQ("http://localhost/another.html", request->uri.spec()); | |
| 56 EXPECT_EQ(GET, request->method); | |
| 57 EXPECT_EQ("", request->content); | |
| 58 EXPECT_EQ(0u, request->headers.size()); | |
| 59 } | |
| 60 | |
| 61 // No other request available yet. | |
| 62 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
| 63 } | |
| 64 | |
| 65 TEST(HttpRequestTest, ParseRequestWithEmptyBody) { | |
| 66 HttpRequestParser parser; | |
| 67 | |
| 68 parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n"); | |
| 69 parser.ProcessChunk("Content-Length: 0\r\n\r\n"); | |
| 70 ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); | |
| 71 | |
| 72 scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
| 73 EXPECT_EQ("", request->content); | |
| 74 EXPECT_EQ(1u, request->headers.count("Content-Length")); | |
| 75 EXPECT_EQ("0", request->headers["Content-Length"]); | |
|
satorux1
2012/11/14 01:44:39
Thank you for adding the test.
I think we should
satorux1
2012/11/14 01:55:20
Or just has_content member, as HttpRequest is a st
mtomasz
2012/11/14 03:23:35
Is the method fine? Done.
mtomasz
2012/11/14 03:25:09
Changed to member.
| |
| 76 } | |
| 77 | |
| 78 } // namespace test_server | |
| 79 } // namespace drive | |
| OLD | NEW |