Chromium Code Reviews| Index: chrome/browser/chromeos/drive/test_servers/http_test_server_unittest.cc |
| diff --git a/chrome/browser/chromeos/drive/test_servers/http_test_server_unittest.cc b/chrome/browser/chromeos/drive/test_servers/http_test_server_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd91ea48f4dab6b5dd1a809cc6edec3a7b96d621 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/drive/test_servers/http_test_server_unittest.cc |
| @@ -0,0 +1,221 @@ |
| +// 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/chromeos/drive/test_servers/http_test_server.h" |
| + |
| +#include "base/threading/thread.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| + |
| +namespace drive { |
| +namespace test_servers { |
| + |
| +namespace { |
| + |
| +// Helper function. Sends chunk of a http request to the |parser|. |
| +void SendChunkToParser(HttpRequestParser* parser, |
| + const std::string& data) { |
| + parser->ProcessChunk(data.data(), data.length()); |
| +} |
| + |
| +// Helper function to receive content of a response returned after invoking |
| +// |fetcher|. |
| +std::string GetFetcherResponseContent(const net::URLFetcher* fetcher) { |
| + std::string result; |
| + DCHECK(fetcher->GetResponseAsString(&result)); |
| + return result; |
| +} |
| + |
| +} // namespace |
| + |
| +class HttpTestServerTest : public testing::Test, |
| + public net::URLFetcherDelegate { |
| + public: |
| + HttpTestServerTest() |
| + : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| + io_thread_(content::BrowserThread::IO) { |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + io_thread_.StartIOThread(); |
| + |
| + request_context_getter_ = new TestURLRequestContextGetter( |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::IO)); |
| + } |
| + |
| + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| + MessageLoop::current()->Quit(); |
| + } |
| + |
| + protected: |
| + MessageLoopForUI message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| + content::TestBrowserThread io_thread_; |
| + scoped_refptr<TestURLRequestContextGetter> request_context_getter_; |
| +}; |
| + |
| +TEST_F(HttpTestServerTest, 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. |
| + SendChunkToParser(&parser, "POST /foobar.html HTTP/1.1\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + SendChunkToParser(&parser, "Host: localhost:1234\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + SendChunkToParser(&parser, "Multi-line-header: abcd\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + SendChunkToParser(&parser, " efgh\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + SendChunkToParser(&parser, " ijkl\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + SendChunkToParser(&parser, "Content-Length: 10\r\n\r\n"); |
| + EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); |
| + // Content data and another request in the same chunk. |
| + SendChunkToParser(&parser, "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\nefgh\nijkl", 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_F(HttpTestServerTest, GenerateResponse) { |
| + HttpResponse response; |
| + response.code = SUCCESS; |
| + response.content = "Sample content - Hello world!"; |
| + response.content_type = "text/plain"; |
| + response.custom_headers["Simple-Header"] = "Simple value."; |
| + response.custom_headers["Multi-Line-Header"] = "Multi\nLine\nValue."; |
| + std::string response_string = response.ToResponseString(); |
| + |
| + std::string request_response_string = |
| + "HTTP/1.1 200 OK\r\n" |
| + "Content-Length: 29\r\n" |
| + "Content-Type: text/plain\r\n" |
| + "Multi-Line-Header: Multi\r\n" |
| + " Line\r\n" |
| + " Value.\r\n" |
| + "Simple-Header: Simple value.\r\n\r\n" |
| + "Sample content - Hello world!"; |
| + |
| + EXPECT_EQ(request_response_string, |
| + response_string); |
| +} |
| + |
| +TEST_F(HttpTestServerTest, TextRequest) { |
| + scoped_ptr<HttpTestServer> server_ = |
| + HttpTestServer::CreateForTesting(); |
| + DCHECK(server_.get()); |
| + |
| + // The simplest text response with an auto generated url. |
| + GURL url1 = server_->RegisterTextResponse("Raspberry chocolate", |
|
mtomasz
2012/11/08 13:30:00
If this dummy response text is inappropriate, I'll
|
| + "text/html"); |
| + DCHECK(url1.spec() != ""); |
| + |
| + GURL url2 = server_->RegisterTextResponse("Vanilla chocolate", |
| + "text/html"); |
| + DCHECK(url2.spec() != ""); |
| + |
| + // Response with a specified url and response code. |
| + GURL url3 = server_->RegisterTextResponse( |
| + "chocolate/bar.html", // URL |
| + "No chocolates", // Dummy response text. |
| + "text/plain", // Content type. |
| + NOT_FOUND); // Response code (404 here). |
| + DCHECK(url3.spec() != ""); |
| + |
| + // Set up fetchers. |
| + scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>( |
| + net::URLFetcher::Create(url1, |
| + net::URLFetcher::GET, |
| + this)); |
| + fetcher1->SetRequestContext(request_context_getter_.get()); |
| + scoped_ptr<net::URLFetcher> fetcher2 = scoped_ptr<net::URLFetcher>( |
| + net::URLFetcher::Create(url2, |
| + net::URLFetcher::GET, |
| + this)); |
| + fetcher2->SetRequestContext(request_context_getter_.get()); |
| + scoped_ptr<net::URLFetcher> fetcher3 = scoped_ptr<net::URLFetcher>( |
| + net::URLFetcher::Create(url3, |
| + net::URLFetcher::GET, |
| + this)); |
| + fetcher3->SetRequestContext(request_context_getter_.get()); |
| + |
| + // Test. |
| + fetcher1->Start(); |
| + MessageLoop::current()->Run(); |
| + EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| + EXPECT_EQ(200, fetcher1->GetResponseCode()); |
| + EXPECT_EQ("Raspberry chocolate", GetFetcherResponseContent(fetcher1.get())); |
| + |
| + fetcher2->Start(); |
| + MessageLoop::current()->Run(); |
| + EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| + EXPECT_EQ(200, fetcher2->GetResponseCode()); |
| + EXPECT_EQ("Vanilla chocolate", GetFetcherResponseContent(fetcher2.get())); |
| + |
| + fetcher3->Start(); |
| + MessageLoop::current()->Run(); |
| + EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| + EXPECT_EQ(404, fetcher3->GetResponseCode()); |
| + EXPECT_EQ("No chocolates", GetFetcherResponseContent(fetcher3.get())); |
| +} |
| + |
| +TEST_F(HttpTestServerTest, DefaultNotFoundResponse) { |
| + scoped_ptr<HttpTestServer> server_ = |
| + HttpTestServer::CreateForTesting(); |
| + DCHECK(server_.get()); |
| + DCHECK(server_->GetBaseURL().spec() != ""); |
| + |
| + scoped_ptr<net::URLFetcher> fetcher = scoped_ptr<net::URLFetcher>( |
| + net::URLFetcher::Create(server_->GetBaseURL(), |
| + net::URLFetcher::GET, |
| + this)); |
| + fetcher->SetRequestContext(request_context_getter_.get()); |
| + |
| + fetcher->Start(); |
| + MessageLoop::current()->Run(); |
| + EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
| + EXPECT_EQ(404, fetcher->GetResponseCode()); |
| +} |
| + |
| + |
| +// TODO(mtomasz): Write a test for a file response. |
| + |
| +} // namespace test_servers |
| +} // namespace drive |