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

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

Issue 14914010: GTTF: Convert WebContentsImplBrowserTest to use EmbeddedTestServer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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"
9 #include "base/file_util.h"
10 #include "base/path_service.h"
8 #include "base/run_loop.h" 11 #include "base/run_loop.h"
9 #include "base/stl_util.h" 12 #include "base/stl_util.h"
10 #include "base/string_util.h" 13 #include "base/string_util.h"
11 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "base/threading/thread_restrictions.h"
12 #include "net/test/embedded_test_server/http_connection.h" 16 #include "net/test/embedded_test_server/http_connection.h"
13 #include "net/test/embedded_test_server/http_request.h" 17 #include "net/test/embedded_test_server/http_request.h"
14 #include "net/test/embedded_test_server/http_response.h" 18 #include "net/test/embedded_test_server/http_response.h"
15 #include "net/tools/fetch/http_listen_socket.h" 19 #include "net/tools/fetch/http_listen_socket.h"
16 20
17 namespace net { 21 namespace net {
18 namespace test_server { 22 namespace test_server {
19 23
20 namespace { 24 namespace {
21 25
22 const int kPort = 8040; 26 const int kPort = 8040;
23 const char kIp[] = "127.0.0.1"; 27 const char kIp[] = "127.0.0.1";
24 const int kRetries = 10; 28 const int kRetries = 10;
25 29
26 // Callback to handle requests with default predefined response for requests 30 // Callback to handle requests with default predefined response for requests
27 // matching the address |url|. 31 // matching the address |url|.
28 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url, 32 scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
29 const HttpResponse& response, 33 const HttpResponse& response,
30 const HttpRequest& request) { 34 const HttpRequest& request) {
31 const GURL request_url = url.Resolve(request.relative_url); 35 const GURL request_url = url.Resolve(request.relative_url);
32 if (url.path() != request_url.path()) 36 if (url.path() != request_url.path())
33 return scoped_ptr<HttpResponse>(NULL); 37 return scoped_ptr<HttpResponse>(NULL);
34 return scoped_ptr<HttpResponse>(new HttpResponse(response)); 38 return scoped_ptr<HttpResponse>(new HttpResponse(response));
35 } 39 }
36 40
41 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& file_path,
satorux1 2013/05/16 04:57:59 function comment is missing.
satorux1 2013/05/16 04:57:59 file_path -> directory_path ?
42 const HttpRequest& request) {
43 // This is a test-only server. Ignore I/O thread restrictions.
44 base::ThreadRestrictions::ScopedAllowIO allow_io;
45
46 std::string file_contents;
satorux1 2013/05/16 04:57:59 Maybe: // Trim the first byte ('/').
47 if (!file_util::ReadFileToString(
48 file_path.AppendASCII(request.relative_url.substr(1)),
49 &file_contents)) {
50 return scoped_ptr<HttpResponse>(NULL);
51 }
52
53 scoped_ptr<HttpResponse> http_response(new HttpResponse);
54 http_response->set_code(net::test_server::SUCCESS);
55 http_response->set_content(file_contents);
56 return http_response.Pass();
57 }
58
37 } // namespace 59 } // namespace
38 60
39 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor, 61 HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
40 StreamListenSocket::Delegate* delegate) 62 StreamListenSocket::Delegate* delegate)
41 : TCPListenSocket(socket_descriptor, delegate) { 63 : TCPListenSocket(socket_descriptor, delegate) {
42 DCHECK(thread_checker_.CalledOnValidThread()); 64 DCHECK(thread_checker_.CalledOnValidThread());
43 } 65 }
44 66
45 void HttpListenSocket::Listen() { 67 void HttpListenSocket::Listen() {
46 DCHECK(thread_checker_.CalledOnValidThread()); 68 DCHECK(thread_checker_.CalledOnValidThread());
47 TCPListenSocket::Listen(); 69 TCPListenSocket::Listen();
48 } 70 }
49 71
50 HttpListenSocket::~HttpListenSocket() { 72 HttpListenSocket::~HttpListenSocket() {
51 DCHECK(thread_checker_.CalledOnValidThread()); 73 DCHECK(thread_checker_.CalledOnValidThread());
52 } 74 }
53 75
54 EmbeddedTestServer::EmbeddedTestServer( 76 EmbeddedTestServer::EmbeddedTestServer(
55 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread) 77 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread)
56 : io_thread_(io_thread), 78 : io_thread_(io_thread),
57 port_(-1), 79 port_(-1),
58 weak_factory_(this) { 80 weak_factory_(this) {
59 DCHECK(io_thread_); 81 DCHECK(io_thread_);
60 DCHECK(thread_checker_.CalledOnValidThread()); 82 DCHECK(thread_checker_.CalledOnValidThread());
61 } 83 }
62 84
63 EmbeddedTestServer::~EmbeddedTestServer() { 85 EmbeddedTestServer::~EmbeddedTestServer() {
64 DCHECK(thread_checker_.CalledOnValidThread()); 86 DCHECK(thread_checker_.CalledOnValidThread());
87
88 if (Started() && !ShutdownAndWaitUntilComplete()) {
89 LOG(ERROR) << "EmbeddedTestServer failed to shut down.";
90 }
65 } 91 }
66 92
67 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { 93 bool EmbeddedTestServer::InitializeAndWaitUntilReady() {
68 DCHECK(thread_checker_.CalledOnValidThread()); 94 DCHECK(thread_checker_.CalledOnValidThread());
69 95
70 base::RunLoop run_loop; 96 base::RunLoop run_loop;
71 if (!io_thread_->PostTaskAndReply( 97 if (!io_thread_->PostTaskAndReply(
72 FROM_HERE, 98 FROM_HERE,
73 base::Bind(&EmbeddedTestServer::InitializeOnIOThread, 99 base::Bind(&EmbeddedTestServer::InitializeOnIOThread,
74 base::Unretained(this)), 100 base::Unretained(this)),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 connections_.erase(connection->socket_.get()); 178 connections_.erase(connection->socket_.get());
153 delete connection; 179 delete connection;
154 } 180 }
155 181
156 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const { 182 GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const {
157 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */)) 183 DCHECK(StartsWithASCII(relative_url, "/", true /* case_sensitive */))
158 << relative_url; 184 << relative_url;
159 return base_url_.Resolve(relative_url); 185 return base_url_.Resolve(relative_url);
160 } 186 }
161 187
188 bool EmbeddedTestServer::ServeFilesFromDirectory(
189 int root_key, const base::FilePath& directory) {
190 base::FilePath root_path;
191 if (!PathService::Get(root_key, &root_path))
192 return false;
193 RegisterRequestHandler(base::Bind(&HandleFileRequest,
194 root_path.Append(directory)));
195 return true;
196 }
197
162 void EmbeddedTestServer::RegisterRequestHandler( 198 void EmbeddedTestServer::RegisterRequestHandler(
163 const HandleRequestCallback& callback) { 199 const HandleRequestCallback& callback) {
164 request_handlers_.push_back(callback); 200 request_handlers_.push_back(callback);
165 } 201 }
166 202
167 void EmbeddedTestServer::DidAccept(StreamListenSocket* server, 203 void EmbeddedTestServer::DidAccept(StreamListenSocket* server,
168 StreamListenSocket* connection) { 204 StreamListenSocket* connection) {
169 DCHECK(io_thread_->BelongsToCurrentThread()); 205 DCHECK(io_thread_->BelongsToCurrentThread());
170 206
171 HttpConnection* http_connection = new HttpConnection( 207 HttpConnection* http_connection = new HttpConnection(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 std::map<StreamListenSocket*, HttpConnection*>::iterator it = 243 std::map<StreamListenSocket*, HttpConnection*>::iterator it =
208 connections_.find(socket); 244 connections_.find(socket);
209 if (it == connections_.end()) { 245 if (it == connections_.end()) {
210 return NULL; 246 return NULL;
211 } 247 }
212 return it->second; 248 return it->second;
213 } 249 }
214 250
215 } // namespace test_server 251 } // namespace test_server
216 } // namespace net 252 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698