Chromium Code Reviews| Index: chrome/browser/google_apis/test_server/http_request_unittest.cc |
| diff --git a/chrome/browser/google_apis/test_server/http_request_unittest.cc b/chrome/browser/google_apis/test_server/http_request_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f0e973bff4eb89a4b99c2e77e9ff76309e98bb18 |
| --- /dev/null |
| +++ b/chrome/browser/google_apis/test_server/http_request_unittest.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/google_apis/test_server/http_request.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace drive { |
| +namespace test_server { |
| + |
| +TEST(HttpRequestTest, ParseRequest) { |
| + HttpRequestParser parser; |
| + |
| + // Process request in chunks to check if the parser deals with border cases. |
| + // Also, check multi-line headers as well as multiple requests in the same |
| + // chunk. This basically should cover all the simplest border cases. |
| + parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + parser.ProcessChunk("Host: localhost:1234\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + parser.ProcessChunk("Multi-line-header: abcd\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + parser.ProcessChunk(" efgh\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + parser.ProcessChunk(" ijkl\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + parser.ProcessChunk("Content-Length: 10\r\n\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + // Content data and another request in the same chunk. |
| + parser.ProcessChunk("1234567890GET /another.html HTTP/1.1\r\n\r\n"); |
| + ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); |
| + |
| + // Fetch the first request and validate it. |
| + { |
| + scoped_ptr<HttpRequest> request = parser.GetRequest(); |
| + EXPECT_EQ("http://localhost/foobar.html", request->uri.spec()); |
| + EXPECT_EQ(POST, request->method); |
| + EXPECT_EQ("1234567890", request->content); |
| + ASSERT_EQ(3u, request->headers.size()); |
| + |
| + EXPECT_EQ(1u, request->headers.count("Host")); |
| + EXPECT_EQ(1u, request->headers.count("Multi-line-header")); |
| + EXPECT_EQ(1u, request->headers.count("Content-Length")); |
| + |
| + EXPECT_EQ("localhost:1234", request->headers["Host"]); |
| + EXPECT_EQ("abcd efgh ijkl", request->headers["Multi-line-header"]); |
| + EXPECT_EQ("10", request->headers["Content-Length"]); |
| + } |
| + |
| + // Fetch the second request and roughly check it. |
| + { |
| + ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); |
| + scoped_ptr<HttpRequest> request = parser.GetRequest(); |
| + EXPECT_EQ("http://localhost/another.html", request->uri.spec()); |
| + EXPECT_EQ(GET, request->method); |
| + EXPECT_EQ("", request->content); |
| + EXPECT_EQ(0u, request->headers.size()); |
| + } |
| + |
| + // No other request available yet. |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| +} |
| + |
| +TEST(HttpRequestTest, ParseRequestWithEmptyBody) { |
| + HttpRequestParser parser; |
| + |
| + parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n"); |
| + parser.ProcessChunk("Content-Length: 0\r\n\r\n"); |
| + ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); |
| + |
| + scoped_ptr<HttpRequest> request = parser.GetRequest(); |
| + EXPECT_EQ("", request->content); |
| + EXPECT_EQ(1u, request->headers.count("Content-Length")); |
| + 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.
|
| +} |
| + |
| +} // namespace test_server |
| +} // namespace drive |