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

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: rebase 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..deeed7d021efcaad503d0f347866170c91623adc 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,27 @@ scoped_ptr<HttpResponse> HandleDefaultRequest(const GURL& url,
return scoped_ptr<HttpResponse>(new HttpResponse(response));
}
+// Handles |request| by serving a file from under |server_root|.
+scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root,
+ const HttpRequest& request) {
+ // This is a test-only server. Ignore I/O thread restrictions.
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+
+ // Trim the first byte ('/').
+ std::string request_path(request.relative_url.substr(1));
+
+ std::string file_contents;
+ if (!file_util::ReadFileToString(
+ server_root.AppendASCII(request_path), &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 +87,10 @@ EmbeddedTestServer::EmbeddedTestServer(
EmbeddedTestServer::~EmbeddedTestServer() {
DCHECK(thread_checker_.CalledOnValidThread());
+
+ if (Started() && !ShutdownAndWaitUntilComplete()) {
+ LOG(ERROR) << "EmbeddedTestServer failed to shut down.";
+ }
}
bool EmbeddedTestServer::InitializeAndWaitUntilReady() {
@@ -159,6 +188,11 @@ GURL EmbeddedTestServer::GetURL(const std::string& relative_url) const {
return base_url_.Resolve(relative_url);
}
+void EmbeddedTestServer::ServeFilesFromDirectory(
+ const base::FilePath& directory) {
+ RegisterRequestHandler(base::Bind(&HandleFileRequest, directory));
+}
+
void EmbeddedTestServer::RegisterRequestHandler(
const HandleRequestCallback& callback) {
request_handlers_.push_back(callback);
« no previous file with comments | « net/test/embedded_test_server/embedded_test_server.h ('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