Fix build break
[chromium-blink-merge.git] / chrome / browser / google_apis / test_server / http_server_unittest.cc
blobd837baf629b37dc5074c7f7666905aafa54ba4c4
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.
5 #include "chrome/browser/google_apis/test_server/http_server.h"
7 #include "base/stringprintf.h"
8 #include "base/threading/thread.h"
9 #include "chrome/browser/google_apis/test_server/http_request.h"
10 #include "chrome/browser/google_apis/test_server/http_response.h"
11 #include "chrome/browser/google_apis/test_util.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_fetcher_delegate.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace google_apis {
21 namespace test_server {
23 namespace {
25 // Gets the content from the given URLFetcher.
26 std::string GetContentFromFetcher(const net::URLFetcher& fetcher) {
27 std::string result;
28 const bool success = fetcher.GetResponseAsString(&result);
29 EXPECT_TRUE(success);
30 return result;
33 // Gets the content type from the given URLFetcher.
34 std::string GetContentTypeFromFetcher(const net::URLFetcher& fetcher) {
35 const net::HttpResponseHeaders* headers = fetcher.GetResponseHeaders();
36 if (headers) {
37 std::string content_type;
38 if (headers->GetMimeType(&content_type))
39 return content_type;
41 return std::string();
44 } // namespace
46 class HttpServerTest : public testing::Test,
47 public net::URLFetcherDelegate {
48 public:
49 HttpServerTest()
50 : num_responses_received_(0),
51 num_responses_expected_(0),
52 ui_thread_(content::BrowserThread::UI, &message_loop_),
53 io_thread_(content::BrowserThread::IO) {
56 virtual void SetUp() OVERRIDE {
57 io_thread_.StartIOThread();
59 request_context_getter_ = new net::TestURLRequestContextGetter(
60 content::BrowserThread::GetMessageLoopProxyForThread(
61 content::BrowserThread::IO));
63 ASSERT_TRUE(server_.InitializeAndWaitUntilReady());
66 virtual void TearDown() OVERRIDE {
67 server_.ShutdownAndWaitUntilComplete();
70 // net::URLFetcherDelegate override.
71 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE {
72 ++num_responses_received_;
73 if (num_responses_received_ == num_responses_expected_)
74 message_loop_.Quit();
77 // Waits until the specified number of responses are received.
78 void WaitForResponses(int num_responses) {
79 num_responses_received_ = 0;
80 num_responses_expected_ = num_responses;
81 message_loop_.Run(); // Will be terminated in OnURLFetchComplete().
84 // Handles |request| sent to |path| and returns the response per |content|,
85 // |content type|, and |code|. Saves the request URL for verification.
86 scoped_ptr<HttpResponse> HandleRequest(const std::string& path,
87 const std::string& content,
88 const std::string& content_type,
89 ResponseCode code,
90 const HttpRequest& request) {
91 request_relative_url_ = request.relative_url;
93 GURL absolute_url = server_.GetURL(request.relative_url);
94 if (absolute_url.path() == path) {
95 scoped_ptr<HttpResponse> http_response(new HttpResponse);
96 http_response->set_code(code);
97 http_response->set_content(content);
98 http_response->set_content_type(content_type);
99 return http_response.Pass();
102 return scoped_ptr<HttpResponse>();
105 protected:
106 int num_responses_received_;
107 int num_responses_expected_;
108 std::string request_relative_url_;
109 MessageLoopForUI message_loop_;
110 content::TestBrowserThread ui_thread_;
111 content::TestBrowserThread io_thread_;
112 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
113 HttpServer server_;
116 TEST_F(HttpServerTest, GetBaseURL) {
117 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%d/", server_.port()),
118 server_.base_url().spec());
121 TEST_F(HttpServerTest, GetURL) {
122 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%d/path?query=foo",
123 server_.port()),
124 server_.GetURL("/path?query=foo").spec());
127 TEST_F(HttpServerTest, RegisterRequestHandler) {
128 server_.RegisterRequestHandler(base::Bind(&HttpServerTest::HandleRequest,
129 base::Unretained(this),
130 "/test",
131 "<b>Worked!</b>",
132 "text/html",
133 SUCCESS));
135 scoped_ptr<net::URLFetcher> fetcher(
136 net::URLFetcher::Create(server_.GetURL("/test?q=foo"),
137 net::URLFetcher::GET,
138 this));
139 fetcher->SetRequestContext(request_context_getter_.get());
140 fetcher->Start();
141 WaitForResponses(1);
143 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status());
144 EXPECT_EQ(SUCCESS, fetcher->GetResponseCode());
145 EXPECT_EQ("<b>Worked!</b>", GetContentFromFetcher(*fetcher));
146 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher));
148 EXPECT_EQ("/test?q=foo", request_relative_url_);
151 TEST_F(HttpServerTest, DefaultNotFoundResponse) {
152 scoped_ptr<net::URLFetcher> fetcher(
153 net::URLFetcher::Create(server_.GetURL("/non-existent"),
154 net::URLFetcher::GET,
155 this));
156 fetcher->SetRequestContext(request_context_getter_.get());
158 fetcher->Start();
159 WaitForResponses(1);
160 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status());
161 EXPECT_EQ(NOT_FOUND, fetcher->GetResponseCode());
164 TEST_F(HttpServerTest, ConcurrentFetches) {
165 server_.RegisterRequestHandler(
166 base::Bind(&HttpServerTest::HandleRequest,
167 base::Unretained(this),
168 "/test1",
169 "Raspberry chocolate",
170 "text/html",
171 SUCCESS));
172 server_.RegisterRequestHandler(
173 base::Bind(&HttpServerTest::HandleRequest,
174 base::Unretained(this),
175 "/test2",
176 "Vanilla chocolate",
177 "text/html",
178 SUCCESS));
179 server_.RegisterRequestHandler(
180 base::Bind(&HttpServerTest::HandleRequest,
181 base::Unretained(this),
182 "/test3",
183 "No chocolates",
184 "text/plain",
185 NOT_FOUND));
187 scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>(
188 net::URLFetcher::Create(server_.GetURL("/test1"),
189 net::URLFetcher::GET,
190 this));
191 fetcher1->SetRequestContext(request_context_getter_.get());
192 scoped_ptr<net::URLFetcher> fetcher2 = scoped_ptr<net::URLFetcher>(
193 net::URLFetcher::Create(server_.GetURL("/test2"),
194 net::URLFetcher::GET,
195 this));
196 fetcher2->SetRequestContext(request_context_getter_.get());
197 scoped_ptr<net::URLFetcher> fetcher3 = scoped_ptr<net::URLFetcher>(
198 net::URLFetcher::Create(server_.GetURL("/test3"),
199 net::URLFetcher::GET,
200 this));
201 fetcher3->SetRequestContext(request_context_getter_.get());
203 // Fetch the three URLs concurrently.
204 fetcher1->Start();
205 fetcher2->Start();
206 fetcher3->Start();
207 WaitForResponses(3);
209 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status());
210 EXPECT_EQ(SUCCESS, fetcher1->GetResponseCode());
211 EXPECT_EQ("Raspberry chocolate", GetContentFromFetcher(*fetcher1));
212 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher1));
214 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher2->GetStatus().status());
215 EXPECT_EQ(SUCCESS, fetcher2->GetResponseCode());
216 EXPECT_EQ("Vanilla chocolate", GetContentFromFetcher(*fetcher2));
217 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2));
219 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher3->GetStatus().status());
220 EXPECT_EQ(NOT_FOUND, fetcher3->GetResponseCode());
221 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3));
222 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3));
225 } // namespace test_server
226 } // namespace google_apis