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/chromeos/drive/test_servers/http_test_server.h" | |
6 | |
7 #include "base/threading/thread.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "content/public/test/test_browser_thread.h" | |
10 #include "net/url_request/url_fetcher.h" | |
11 #include "net/url_request/url_fetcher_delegate.h" | |
12 #include "net/url_request/url_request_test_util.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 | |
16 namespace drive { | |
17 namespace test_servers { | |
18 | |
19 namespace { | |
20 | |
21 // Helper function. Sends chunk of a http request to the |parser|. | |
22 void SendChunkToParser(HttpRequestParser* parser, | |
23 const std::string& data) { | |
24 parser->ProcessChunk(data.data(), data.length()); | |
25 } | |
26 | |
27 // Helper function to receive content of a response returned after invoking | |
28 // |fetcher|. | |
29 std::string GetFetcherResponseContent(const net::URLFetcher* fetcher) { | |
30 std::string result; | |
31 DCHECK(fetcher->GetResponseAsString(&result)); | |
32 return result; | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 class HttpTestServerTest : public testing::Test, | |
38 public net::URLFetcherDelegate { | |
39 public: | |
40 HttpTestServerTest() | |
41 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
42 io_thread_(content::BrowserThread::IO) { | |
43 } | |
44 | |
45 virtual void SetUp() OVERRIDE { | |
46 io_thread_.StartIOThread(); | |
47 | |
48 request_context_getter_ = new TestURLRequestContextGetter( | |
49 content::BrowserThread::GetMessageLoopProxyForThread( | |
50 content::BrowserThread::IO)); | |
51 } | |
52 | |
53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | |
54 MessageLoop::current()->Quit(); | |
55 } | |
56 | |
57 protected: | |
58 MessageLoopForUI message_loop_; | |
59 content::TestBrowserThread ui_thread_; | |
60 content::TestBrowserThread io_thread_; | |
61 scoped_refptr<TestURLRequestContextGetter> request_context_getter_; | |
62 }; | |
63 | |
64 TEST_F(HttpTestServerTest, ParseRequest) { | |
65 HttpRequestParser parser; | |
66 | |
67 // Process request in chunks to check if the parser deals with border cases. | |
68 // Also, check multi-line headers as well as multiple requests in the same | |
69 // chunk. This basically should cover all the simplest border cases. | |
70 SendChunkToParser(&parser, "POST /foobar.html HTTP/1.1\r\n"); | |
71 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
72 SendChunkToParser(&parser, "Host: localhost:1234\r\n"); | |
73 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
74 SendChunkToParser(&parser, "Multi-line-header: abcd\r\n"); | |
75 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
76 SendChunkToParser(&parser, " efgh\r\n"); | |
77 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
78 SendChunkToParser(&parser, " ijkl\r\n"); | |
79 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
80 SendChunkToParser(&parser, "Content-Length: 10\r\n\r\n"); | |
81 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
82 // Content data and another request in the same chunk. | |
83 SendChunkToParser(&parser, "1234567890GET /another.html HTTP/1.1\r\n\r\n"); | |
84 ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); | |
85 | |
86 // Fetch the first request and validate it. | |
87 { | |
88 scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
89 EXPECT_EQ("http://localhost/foobar.html", request->uri.spec()); | |
90 EXPECT_EQ(POST, request->method); | |
91 EXPECT_EQ("1234567890", request->content); | |
92 ASSERT_EQ(3u, request->headers.size()); | |
93 | |
94 EXPECT_EQ(1u, request->headers.count("Host")); | |
95 EXPECT_EQ(1u, request->headers.count("Multi-line-header")); | |
96 EXPECT_EQ(1u, request->headers.count("Content-Length")); | |
97 | |
98 EXPECT_EQ("localhost:1234", request->headers["Host"]); | |
99 EXPECT_EQ("abcd\nefgh\nijkl", request->headers["Multi-line-header"]); | |
100 EXPECT_EQ("10", request->headers["Content-Length"]); | |
101 } | |
102 | |
103 // Fetch the second request and roughly check it. | |
104 { | |
105 ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest()); | |
106 scoped_ptr<HttpRequest> request = parser.GetRequest(); | |
107 EXPECT_EQ("http://localhost/another.html", request->uri.spec()); | |
108 EXPECT_EQ(GET, request->method); | |
109 EXPECT_EQ("", request->content); | |
110 EXPECT_EQ(0u, request->headers.size()); | |
111 } | |
112 | |
113 // No other request available yet. | |
114 EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest()); | |
115 } | |
116 | |
117 TEST_F(HttpTestServerTest, GenerateResponse) { | |
118 HttpResponse response; | |
119 response.code = SUCCESS; | |
120 response.content = "Sample content - Hello world!"; | |
121 response.content_type = "text/plain"; | |
122 response.custom_headers["Simple-Header"] = "Simple value."; | |
123 response.custom_headers["Multi-Line-Header"] = "Multi\nLine\nValue."; | |
124 std::string response_string = response.ToResponseString(); | |
125 | |
126 std::string request_response_string = | |
127 "HTTP/1.1 200 OK\r\n" | |
128 "Content-Length: 29\r\n" | |
129 "Content-Type: text/plain\r\n" | |
130 "Multi-Line-Header: Multi\r\n" | |
131 " Line\r\n" | |
132 " Value.\r\n" | |
133 "Simple-Header: Simple value.\r\n\r\n" | |
134 "Sample content - Hello world!"; | |
135 | |
136 EXPECT_EQ(request_response_string, | |
137 response_string); | |
138 } | |
139 | |
140 TEST_F(HttpTestServerTest, TextRequest) { | |
141 scoped_ptr<HttpTestServer> server_ = | |
142 HttpTestServer::CreateForTesting(); | |
143 DCHECK(server_.get()); | |
144 | |
145 // The simplest text response with an auto generated url. | |
146 GURL url1 = server_->RegisterTextResponse("Raspberry chocolate", | |
mtomasz
2012/11/08 13:30:00
If this dummy response text is inappropriate, I'll
| |
147 "text/html"); | |
148 DCHECK(url1.spec() != ""); | |
149 | |
150 GURL url2 = server_->RegisterTextResponse("Vanilla chocolate", | |
151 "text/html"); | |
152 DCHECK(url2.spec() != ""); | |
153 | |
154 // Response with a specified url and response code. | |
155 GURL url3 = server_->RegisterTextResponse( | |
156 "chocolate/bar.html", // URL | |
157 "No chocolates", // Dummy response text. | |
158 "text/plain", // Content type. | |
159 NOT_FOUND); // Response code (404 here). | |
160 DCHECK(url3.spec() != ""); | |
161 | |
162 // Set up fetchers. | |
163 scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>( | |
164 net::URLFetcher::Create(url1, | |
165 net::URLFetcher::GET, | |
166 this)); | |
167 fetcher1->SetRequestContext(request_context_getter_.get()); | |
168 scoped_ptr<net::URLFetcher> fetcher2 = scoped_ptr<net::URLFetcher>( | |
169 net::URLFetcher::Create(url2, | |
170 net::URLFetcher::GET, | |
171 this)); | |
172 fetcher2->SetRequestContext(request_context_getter_.get()); | |
173 scoped_ptr<net::URLFetcher> fetcher3 = scoped_ptr<net::URLFetcher>( | |
174 net::URLFetcher::Create(url3, | |
175 net::URLFetcher::GET, | |
176 this)); | |
177 fetcher3->SetRequestContext(request_context_getter_.get()); | |
178 | |
179 // Test. | |
180 fetcher1->Start(); | |
181 MessageLoop::current()->Run(); | |
182 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); | |
183 EXPECT_EQ(200, fetcher1->GetResponseCode()); | |
184 EXPECT_EQ("Raspberry chocolate", GetFetcherResponseContent(fetcher1.get())); | |
185 | |
186 fetcher2->Start(); | |
187 MessageLoop::current()->Run(); | |
188 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); | |
189 EXPECT_EQ(200, fetcher2->GetResponseCode()); | |
190 EXPECT_EQ("Vanilla chocolate", GetFetcherResponseContent(fetcher2.get())); | |
191 | |
192 fetcher3->Start(); | |
193 MessageLoop::current()->Run(); | |
194 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); | |
195 EXPECT_EQ(404, fetcher3->GetResponseCode()); | |
196 EXPECT_EQ("No chocolates", GetFetcherResponseContent(fetcher3.get())); | |
197 } | |
198 | |
199 TEST_F(HttpTestServerTest, DefaultNotFoundResponse) { | |
200 scoped_ptr<HttpTestServer> server_ = | |
201 HttpTestServer::CreateForTesting(); | |
202 DCHECK(server_.get()); | |
203 DCHECK(server_->GetBaseURL().spec() != ""); | |
204 | |
205 scoped_ptr<net::URLFetcher> fetcher = scoped_ptr<net::URLFetcher>( | |
206 net::URLFetcher::Create(server_->GetBaseURL(), | |
207 net::URLFetcher::GET, | |
208 this)); | |
209 fetcher->SetRequestContext(request_context_getter_.get()); | |
210 | |
211 fetcher->Start(); | |
212 MessageLoop::current()->Run(); | |
213 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | |
214 EXPECT_EQ(404, fetcher->GetResponseCode()); | |
215 } | |
216 | |
217 | |
218 // TODO(mtomasz): Write a test for a file response. | |
219 | |
220 } // namespace test_servers | |
221 } // namespace drive | |
OLD | NEW |