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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: net/test/embedded_test_server/embedded_test_server.cc
diff --git a/net/test/embedded_test_server/embedded_test_server.cc b/net/test/embedded_test_server/embedded_test_server.cc
index c42ff639f241cfe176416f9954fef2976030ff12..d21127cb7e92ed186441bed2d0a6d883aabca4d5 100644
--- a/net/test/embedded_test_server/embedded_test_server.cc
+++ b/net/test/embedded_test_server/embedded_test_server.cc
@@ -5,10 +5,14 @@
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "base/bind.h"
+#include "base/files/file_path.h"
+#include "base/file_util.h"
+#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
+#include "base/threading/thread_restrictions.h"
#include "net/test/embedded_test_server/http_connection.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
@@ -34,6 +38,24 @@ scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
return scoped_ptr<HttpResponse>(new HttpResponse(response));
}
+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 ?
+ const HttpRequest& request) {
+ // This is a test-only server. Ignore I/O thread restrictions.
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+
+ std::string file_contents;
satorux1 2013/05/16 04:57:59 Maybe: // Trim the first byte ('/').
+ if (!file_util::ReadFileToString(
+ file_path.AppendASCII(request.relative_url.substr(1)),
+ &file_contents)) {
+ return scoped_ptr<HttpResponse>(NULL);
+ }
+
+ scoped_ptr<HttpResponse> http_response(new HttpResponse);
+ http_response->set_code(net::test_server::SUCCESS);
+ http_response->set_content(file_contents);
+ return http_response.Pass();
+}
+
} // namespace
HttpListenSocket::HttpListenSocket(const SocketDescriptor socket_descriptor,
@@ -62,6 +84,10 @@ EmbeddedTestServer::EmbeddedTestServer(
EmbeddedTestServer::~EmbeddedTestServer() {
DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (Started() && !ShutdownAndWaitUntilComplete()) {
+ LOG(ERROR) << "EmbeddedTestServer failed to shut down.";
+ }
}
bool EmbeddedTestServer::InitializeAndWaitUntilReady() {
@@ -159,6 +185,16 @@ GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const {
return base_url_.Resolve(relative_url);
}
+bool EmbeddedTestServer::ServeFilesFromDirectory(
+ int root_key, const base::FilePath& directory) {
+ base::FilePath root_path;
+ if (!PathService::Get(root_key, &root_path))
+ return false;
+ RegisterRequestHandler(base::Bind(&HandleFileRequest,
+ root_path.Append(directory)));
+ return true;
+}
+
void EmbeddedTestServer::RegisterRequestHandler(
const HandleRequestCallback& callback) {
request_handlers_.push_back(callback);

Powered by Google App Engine
This is Rietveld 408576698