Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: net/test/embedded_test_server/embedded_test_server.cc

Issue 16268017: GTTF: convert some tests in chrome to use EmbeddedTestServer patch nr 1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
16 #include "net/base/ip_endpoint.h" 16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/test/embedded_test_server/http_connection.h" 18 #include "net/test/embedded_test_server/http_connection.h"
19 #include "net/test/embedded_test_server/http_request.h" 19 #include "net/test/embedded_test_server/http_request.h"
20 #include "net/test/embedded_test_server/http_response.h" 20 #include "net/test/embedded_test_server/http_response.h"
21 #include "net/tools/fetch/http_listen_socket.h" 21 #include "net/tools/fetch/http_listen_socket.h"
22 22
23 namespace net { 23 namespace net {
24 namespace test_server { 24 namespace test_server {
25 25
26 namespace { 26 namespace {
27 27
28 class CustomHttpResponse : public HttpResponse {
29 public:
30 CustomHttpResponse(const std::string& headers, const std::string& contents)
31 : headers_(headers), contents_(contents) {
32 }
33
34 virtual std::string ToResponseString() const OVERRIDE {
35 return headers_ + "\r\n" + contents_;
36 }
37
38 private:
39 std::string headers_;
40 std::string contents_;
41
42 DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse);
43 };
44
28 // Handles |request| by serving a file from under |server_root|. 45 // Handles |request| by serving a file from under |server_root|.
29 scoped_ptr<HttpResponse> HandleFileRequest( 46 scoped_ptr<HttpResponse> HandleFileRequest(
30 const base::FilePath& server_root, 47 const base::FilePath& server_root,
31 const HttpRequest& request) { 48 const HttpRequest& request) {
32 // This is a test-only server. Ignore I/O thread restrictions. 49 // This is a test-only server. Ignore I/O thread restrictions.
33 base::ThreadRestrictions::ScopedAllowIO allow_io; 50 base::ThreadRestrictions::ScopedAllowIO allow_io;
34 51
35 // Trim the first byte ('/'). 52 // Trim the first byte ('/').
36 std::string request_path(request.relative_url.substr(1)); 53 std::string request_path(request.relative_url.substr(1));
37 54
38 // Remove the query string if present. 55 // Remove the query string if present.
39 size_t query_pos = request_path.find('?'); 56 size_t query_pos = request_path.find('?');
40 if (query_pos != std::string::npos) 57 if (query_pos != std::string::npos)
41 request_path = request_path.substr(0, query_pos); 58 request_path = request_path.substr(0, query_pos);
42 59
60 base::FilePath file_path(server_root.AppendASCII(request_path));
43 std::string file_contents; 61 std::string file_contents;
44 if (!file_util::ReadFileToString( 62 if (!file_util::ReadFileToString(file_path, &file_contents))
45 server_root.AppendASCII(request_path), &file_contents)) {
46 return scoped_ptr<HttpResponse>(); 63 return scoped_ptr<HttpResponse>();
64
65 base::FilePath headers_path(
66 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers")));
67
68 if (file_util::PathExists(headers_path)) {
69 std::string headers_contents;
70 if (!file_util::ReadFileToString(headers_path, &headers_contents))
71 return scoped_ptr<HttpResponse>(NULL);
72
73 scoped_ptr<CustomHttpResponse> http_response(
74 new CustomHttpResponse(headers_contents, file_contents));
75 return http_response.PassAs<HttpResponse>();
47 } 76 }
48 77
49 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); 78 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
50 http_response->set_code(net::test_server::SUCCESS); 79 http_response->set_code(HTTP_OK);
51 http_response->set_content(file_contents); 80 http_response->set_content(file_contents);
52 return http_response.PassAs<HttpResponse>(); 81 return http_response.PassAs<HttpResponse>();
53 } 82 }
54 83
55 } // namespace 84 } // namespace
56 85
57 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, 86 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
58 StreamListenSocket::Delegate* delegate) 87 StreamListenSocket::Delegate* delegate)
59 : TCPListenSocket(socket_descriptor, delegate) { 88 : TCPListenSocket(socket_descriptor, delegate) {
60 DCHECK(thread_checker_.CalledOnValidThread()); 89 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 connection->SendResponse(response.Pass()); 190 connection->SendResponse(response.Pass());
162 request_handled = true; 191 request_handled = true;
163 break; 192 break;
164 } 193 }
165 } 194 }
166 195
167 if (!request_handled) { 196 if (!request_handled) {
168 LOG(WARNING) << "Request not handled. Returning 404: " 197 LOG(WARNING) << "Request not handled. Returning 404: "
169 << request->relative_url; 198 << request->relative_url;
170 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse); 199 scoped_ptr<BasicHttpResponse> not_found_response(new BasicHttpResponse);
171 not_found_response->set_code(NOT_FOUND); 200 not_found_response->set_code(HTTP_NOT_FOUND);
172 connection->SendResponse( 201 connection->SendResponse(
173 not_found_response.PassAs<HttpResponse>()); 202 not_found_response.PassAs<HttpResponse>());
174 } 203 }
175 204
176 // Drop the connection, since we do not support multiple requests per 205 // Drop the connection, since we do not support multiple requests per
177 // connection. 206 // connection.
178 connections_.erase(connection->socket_.get()); 207 connections_.erase(connection->socket_.get());
179 delete connection; 208 delete connection;
180 } 209 }
181 210
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 std::map<StreamListenSocket*, HttpConnection*>::iterator it = 267 std::map<StreamListenSocket*, HttpConnection*>::iterator it =
239 connections_.find(socket); 268 connections_.find(socket);
240 if (it == connections_.end()) { 269 if (it == connections_.end()) {
241 return NULL; 270 return NULL;
242 } 271 }
243 return it->second; 272 return it->second;
244 } 273 }
245 274
246 } // namespace test_server 275 } // namespace test_server
247 } // namespace net 276 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_status_code.cc ('k') | net/test/embedded_test_server/embedded_test_server_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698