OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "net/http/http_response_headers.h" | 9 #include "net/http/http_response_headers.h" |
10 #include "net/test/embedded_test_server/http_request.h" | 10 #include "net/test/embedded_test_server/http_request.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 num_responses_expected_ = num_responses; | 78 num_responses_expected_ = num_responses; |
79 // Will be terminated in OnURLFetchComplete(). | 79 // Will be terminated in OnURLFetchComplete(). |
80 base::MessageLoop::current()->Run(); | 80 base::MessageLoop::current()->Run(); |
81 } | 81 } |
82 | 82 |
83 // Handles |request| sent to |path| and returns the response per |content|, | 83 // Handles |request| sent to |path| and returns the response per |content|, |
84 // |content type|, and |code|. Saves the request URL for verification. | 84 // |content type|, and |code|. Saves the request URL for verification. |
85 scoped_ptr<HttpResponse> HandleRequest(const std::string& path, | 85 scoped_ptr<HttpResponse> HandleRequest(const std::string& path, |
86 const std::string& content, | 86 const std::string& content, |
87 const std::string& content_type, | 87 const std::string& content_type, |
88 ResponseCode code, | 88 HttpStatusCode code, |
89 const HttpRequest& request) { | 89 const HttpRequest& request) { |
90 request_relative_url_ = request.relative_url; | 90 request_relative_url_ = request.relative_url; |
91 | 91 |
92 GURL absolute_url = server_->GetURL(request.relative_url); | 92 GURL absolute_url = server_->GetURL(request.relative_url); |
93 if (absolute_url.path() == path) { | 93 if (absolute_url.path() == path) { |
94 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | 94 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); |
95 http_response->set_code(code); | 95 http_response->set_code(code); |
96 http_response->set_content(content); | 96 http_response->set_content(content); |
97 http_response->set_content_type(content_type); | 97 http_response->set_content_type(content_type); |
98 return http_response.PassAs<HttpResponse>(); | 98 return http_response.PassAs<HttpResponse>(); |
(...skipping 22 matching lines...) Expand all Loading... |
121 server_->GetURL("/path?query=foo").spec()); | 121 server_->GetURL("/path?query=foo").spec()); |
122 } | 122 } |
123 | 123 |
124 TEST_F(EmbeddedTestServerTest, RegisterRequestHandler) { | 124 TEST_F(EmbeddedTestServerTest, RegisterRequestHandler) { |
125 server_->RegisterRequestHandler( | 125 server_->RegisterRequestHandler( |
126 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 126 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
127 base::Unretained(this), | 127 base::Unretained(this), |
128 "/test", | 128 "/test", |
129 "<b>Worked!</b>", | 129 "<b>Worked!</b>", |
130 "text/html", | 130 "text/html", |
131 SUCCESS)); | 131 HTTP_OK)); |
132 | 132 |
133 scoped_ptr<URLFetcher> fetcher( | 133 scoped_ptr<URLFetcher> fetcher( |
134 URLFetcher::Create(server_->GetURL("/test?q=foo"), | 134 URLFetcher::Create(server_->GetURL("/test?q=foo"), |
135 URLFetcher::GET, | 135 URLFetcher::GET, |
136 this)); | 136 this)); |
137 fetcher->SetRequestContext(request_context_getter_.get()); | 137 fetcher->SetRequestContext(request_context_getter_.get()); |
138 fetcher->Start(); | 138 fetcher->Start(); |
139 WaitForResponses(1); | 139 WaitForResponses(1); |
140 | 140 |
141 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 141 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
142 EXPECT_EQ(SUCCESS, fetcher->GetResponseCode()); | 142 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); |
143 EXPECT_EQ("<b>Worked!</b>", GetContentFromFetcher(*fetcher)); | 143 EXPECT_EQ("<b>Worked!</b>", GetContentFromFetcher(*fetcher)); |
144 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher)); | 144 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher)); |
145 | 145 |
146 EXPECT_EQ("/test?q=foo", request_relative_url_); | 146 EXPECT_EQ("/test?q=foo", request_relative_url_); |
147 } | 147 } |
148 | 148 |
149 TEST_F(EmbeddedTestServerTest, ServeFilesFromDirectory) { | 149 TEST_F(EmbeddedTestServerTest, ServeFilesFromDirectory) { |
150 base::FilePath src_dir; | 150 base::FilePath src_dir; |
151 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | 151 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
152 server_->ServeFilesFromDirectory( | 152 server_->ServeFilesFromDirectory( |
153 src_dir.AppendASCII("net").AppendASCII("data")); | 153 src_dir.AppendASCII("net").AppendASCII("data")); |
154 | 154 |
155 scoped_ptr<URLFetcher> fetcher( | 155 scoped_ptr<URLFetcher> fetcher( |
156 URLFetcher::Create(server_->GetURL("/test.html"), | 156 URLFetcher::Create(server_->GetURL("/test.html"), |
157 URLFetcher::GET, | 157 URLFetcher::GET, |
158 this)); | 158 this)); |
159 fetcher->SetRequestContext(request_context_getter_.get()); | 159 fetcher->SetRequestContext(request_context_getter_.get()); |
160 fetcher->Start(); | 160 fetcher->Start(); |
161 WaitForResponses(1); | 161 WaitForResponses(1); |
162 | 162 |
163 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 163 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
164 EXPECT_EQ(SUCCESS, fetcher->GetResponseCode()); | 164 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); |
165 EXPECT_EQ("<p>Hello World!</p>", GetContentFromFetcher(*fetcher)); | 165 EXPECT_EQ("<p>Hello World!</p>", GetContentFromFetcher(*fetcher)); |
166 EXPECT_EQ("", GetContentTypeFromFetcher(*fetcher)); | 166 EXPECT_EQ("", GetContentTypeFromFetcher(*fetcher)); |
167 } | 167 } |
168 | 168 |
169 TEST_F(EmbeddedTestServerTest, DefaultNotFoundResponse) { | 169 TEST_F(EmbeddedTestServerTest, DefaultNotFoundResponse) { |
170 scoped_ptr<URLFetcher> fetcher( | 170 scoped_ptr<URLFetcher> fetcher( |
171 URLFetcher::Create(server_->GetURL("/non-existent"), | 171 URLFetcher::Create(server_->GetURL("/non-existent"), |
172 URLFetcher::GET, | 172 URLFetcher::GET, |
173 this)); | 173 this)); |
174 fetcher->SetRequestContext(request_context_getter_.get()); | 174 fetcher->SetRequestContext(request_context_getter_.get()); |
175 | 175 |
176 fetcher->Start(); | 176 fetcher->Start(); |
177 WaitForResponses(1); | 177 WaitForResponses(1); |
178 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 178 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
179 EXPECT_EQ(NOT_FOUND, fetcher->GetResponseCode()); | 179 EXPECT_EQ(HTTP_NOT_FOUND, fetcher->GetResponseCode()); |
180 } | 180 } |
181 | 181 |
182 TEST_F(EmbeddedTestServerTest, ConcurrentFetches) { | 182 TEST_F(EmbeddedTestServerTest, ConcurrentFetches) { |
183 server_->RegisterRequestHandler( | 183 server_->RegisterRequestHandler( |
184 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 184 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
185 base::Unretained(this), | 185 base::Unretained(this), |
186 "/test1", | 186 "/test1", |
187 "Raspberry chocolate", | 187 "Raspberry chocolate", |
188 "text/html", | 188 "text/html", |
189 SUCCESS)); | 189 HTTP_OK)); |
190 server_->RegisterRequestHandler( | 190 server_->RegisterRequestHandler( |
191 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 191 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
192 base::Unretained(this), | 192 base::Unretained(this), |
193 "/test2", | 193 "/test2", |
194 "Vanilla chocolate", | 194 "Vanilla chocolate", |
195 "text/html", | 195 "text/html", |
196 SUCCESS)); | 196 HTTP_OK)); |
197 server_->RegisterRequestHandler( | 197 server_->RegisterRequestHandler( |
198 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 198 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
199 base::Unretained(this), | 199 base::Unretained(this), |
200 "/test3", | 200 "/test3", |
201 "No chocolates", | 201 "No chocolates", |
202 "text/plain", | 202 "text/plain", |
203 NOT_FOUND)); | 203 HTTP_NOT_FOUND)); |
204 | 204 |
205 scoped_ptr<URLFetcher> fetcher1 = scoped_ptr<URLFetcher>( | 205 scoped_ptr<URLFetcher> fetcher1 = scoped_ptr<URLFetcher>( |
206 URLFetcher::Create(server_->GetURL("/test1"), | 206 URLFetcher::Create(server_->GetURL("/test1"), |
207 URLFetcher::GET, | 207 URLFetcher::GET, |
208 this)); | 208 this)); |
209 fetcher1->SetRequestContext(request_context_getter_.get()); | 209 fetcher1->SetRequestContext(request_context_getter_.get()); |
210 scoped_ptr<URLFetcher> fetcher2 = scoped_ptr<URLFetcher>( | 210 scoped_ptr<URLFetcher> fetcher2 = scoped_ptr<URLFetcher>( |
211 URLFetcher::Create(server_->GetURL("/test2"), | 211 URLFetcher::Create(server_->GetURL("/test2"), |
212 URLFetcher::GET, | 212 URLFetcher::GET, |
213 this)); | 213 this)); |
214 fetcher2->SetRequestContext(request_context_getter_.get()); | 214 fetcher2->SetRequestContext(request_context_getter_.get()); |
215 scoped_ptr<URLFetcher> fetcher3 = scoped_ptr<URLFetcher>( | 215 scoped_ptr<URLFetcher> fetcher3 = scoped_ptr<URLFetcher>( |
216 URLFetcher::Create(server_->GetURL("/test3"), | 216 URLFetcher::Create(server_->GetURL("/test3"), |
217 URLFetcher::GET, | 217 URLFetcher::GET, |
218 this)); | 218 this)); |
219 fetcher3->SetRequestContext(request_context_getter_.get()); | 219 fetcher3->SetRequestContext(request_context_getter_.get()); |
220 | 220 |
221 // Fetch the three URLs concurrently. | 221 // Fetch the three URLs concurrently. |
222 fetcher1->Start(); | 222 fetcher1->Start(); |
223 fetcher2->Start(); | 223 fetcher2->Start(); |
224 fetcher3->Start(); | 224 fetcher3->Start(); |
225 WaitForResponses(3); | 225 WaitForResponses(3); |
226 | 226 |
227 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); | 227 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
228 EXPECT_EQ(SUCCESS, fetcher1->GetResponseCode()); | 228 EXPECT_EQ(HTTP_OK, fetcher1->GetResponseCode()); |
229 EXPECT_EQ("Raspberry chocolate", GetContentFromFetcher(*fetcher1)); | 229 EXPECT_EQ("Raspberry chocolate", GetContentFromFetcher(*fetcher1)); |
230 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher1)); | 230 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher1)); |
231 | 231 |
232 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher2->GetStatus().status()); | 232 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher2->GetStatus().status()); |
233 EXPECT_EQ(SUCCESS, fetcher2->GetResponseCode()); | 233 EXPECT_EQ(HTTP_OK, fetcher2->GetResponseCode()); |
234 EXPECT_EQ("Vanilla chocolate", GetContentFromFetcher(*fetcher2)); | 234 EXPECT_EQ("Vanilla chocolate", GetContentFromFetcher(*fetcher2)); |
235 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); | 235 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); |
236 | 236 |
237 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); | 237 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); |
238 EXPECT_EQ(NOT_FOUND, fetcher3->GetResponseCode()); | 238 EXPECT_EQ(HTTP_NOT_FOUND, fetcher3->GetResponseCode()); |
239 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); | 239 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); |
240 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); | 240 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); |
241 } | 241 } |
242 | 242 |
243 } // namespace test_server | 243 } // namespace test_server |
244 } // namespace net | 244 } // namespace net |
OLD | NEW |