Commit 3bfa72c7 authored by satorux@chromium.org's avatar satorux@chromium.org

google_apis: Remove some functions from test_server::HttpServer

These functions are not used. We can add them back if needed.
Less code to maintain is preferable.

BUG=none
TEST=none

Review URL: https://codereview.chromium.org/11419283

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170843 0039d316-1c4b-4281-b951-d872f2087c98
parent 3dc50265
...@@ -164,60 +164,6 @@ void HttpServer::RegisterRequestHandler( ...@@ -164,60 +164,6 @@ void HttpServer::RegisterRequestHandler(
request_handlers_.push_back(callback); request_handlers_.push_back(callback);
} }
void HttpServer::RegisterDefaultResponse(
const std::string& relative_path,
const HttpResponse& default_response) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(StartsWithASCII(relative_path, "/", true /* case_sensitive */))
<< relative_path;
const HandleRequestCallback callback =
base::Bind(&HandleDefaultRequest,
GetURL(relative_path),
default_response);
request_handlers_.push_back(callback);
}
void HttpServer::RegisterTextResponse(
const std::string& relative_path,
const std::string& content,
const std::string& content_type,
const ResponseCode response_code) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(StartsWithASCII(relative_path, "/", true /* case_sensitive */))
<< relative_path;
HttpResponse default_response;
default_response.set_content(content);
default_response.set_content_type(content_type);
default_response.set_code(response_code);
RegisterDefaultResponse(relative_path, default_response);
}
void HttpServer::RegisterFileResponse(
const std::string& relative_path,
const FilePath& file_path,
const std::string& content_type,
const ResponseCode response_code) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(StartsWithASCII(relative_path, "/", true /* case_sensitive */))
<< relative_path;
HttpResponse default_response;
std::string content;
const bool success = file_util::ReadFileToString(
file_path, &content);
default_response.set_content(content);
DCHECK(success) << "Failed to open the file: " << file_path.value();
default_response.set_content_type(content_type);
default_response.set_code(response_code);
RegisterDefaultResponse(relative_path, default_response);
}
void HttpServer::DidAccept(net::StreamListenSocket* server, void HttpServer::DidAccept(net::StreamListenSocket* server,
net::StreamListenSocket* connection) { net::StreamListenSocket* connection) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
......
...@@ -30,7 +30,7 @@ class HttpListenSocket: public net::TCPListenSocket { ...@@ -30,7 +30,7 @@ class HttpListenSocket: public net::TCPListenSocket {
virtual ~HttpListenSocket(); virtual ~HttpListenSocket();
}; };
// Class providing a HTTP server for testing purpose. This is a basic server // Class providing an HTTP server for testing purpose. This is a basic server
// providing only an essential subset of HTTP/1.1 protocol. Especially, // providing only an essential subset of HTTP/1.1 protocol. Especially,
// it assumes that the request syntax is correct. It *does not* support // it assumes that the request syntax is correct. It *does not* support
// a Chunked Transfer Encoding. // a Chunked Transfer Encoding.
...@@ -38,22 +38,27 @@ class HttpListenSocket: public net::TCPListenSocket { ...@@ -38,22 +38,27 @@ class HttpListenSocket: public net::TCPListenSocket {
// The common use case is below: // The common use case is below:
// //
// scoped_ptr<HttpServer> test_server_; // scoped_ptr<HttpServer> test_server_;
// GURL hello_world_url_; //
// GURL file_url_;
// (...)
// void SetUp() { // void SetUp() {
// test_server_.reset(new HttpServer()); // test_server_.reset(new HttpServer());
// DCHECK(test_server_.InitializeAndWaitUntilReady()); // DCHECK(test_server_.InitializeAndWaitUntilReady());
// hello_world_url = test_server->RegisterTextResponse( // test_server->RegisterRequestHandler(
// "/abc", // base::Bind(&FooTest::HandleRequest, base::Unretained(this)));
// "<b>Hello world!</b>", // }
// "text/html"); //
// metadata_url = test_server->RegisterFileResponse( // void HandleRequest(const HttpRequest& request) {
// "metadata/file.doc") // GURL absolute_url = test_server_.GetURL(request.relative_url);
// "testing/data/metadata.xml", // if (absolute_url.path() != "/test")
// "text/xml", // return scoped_ptr<test_server::HttpResponse>();
// 200); //
// scoped_ptr<test_server::HttpResponse> http_response(
// new test_server::HttpResponse);
// http_response->set_code(test_server::SUCCESS);
// http_response->set_content("hello");
// http_response->set_content_type("text/plain");
// return http_response.Pass();
// } // }
//
class HttpServer : private net::StreamListenSocket::Delegate { class HttpServer : private net::StreamListenSocket::Delegate {
public: public:
typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
...@@ -93,27 +98,6 @@ class HttpServer : private net::StreamListenSocket::Delegate { ...@@ -93,27 +98,6 @@ class HttpServer : private net::StreamListenSocket::Delegate {
// on UI thread. // on UI thread.
void RegisterRequestHandler(const HandleRequestCallback& callback); void RegisterRequestHandler(const HandleRequestCallback& callback);
// Used to provide the same predefined response for the requests matching
// the |relative_path|, which should start with '/'. Should be used if any
// custom data, such as additional headers should be sent from the server.
void RegisterDefaultResponse(
const std::string& relative_path,
const HttpResponse& default_response);
// Registers a simple text response.
void RegisterTextResponse(
const std::string& relative_path,
const std::string& content,
const std::string& content_type,
const ResponseCode response_code);
// Registers a simple file response. The file is loaded into memory.
void RegisterFileResponse(
const std::string& relative_path,
const FilePath& file_path,
const std::string& content_type,
const ResponseCode response_code);
private: private:
// Initializes and starts the server. If initialization succeeds, Starts() // Initializes and starts the server. If initialization succeeds, Starts()
// will return true. // will return true.
......
...@@ -79,16 +79,25 @@ class HttpServerTest : public testing::Test, ...@@ -79,16 +79,25 @@ class HttpServerTest : public testing::Test,
message_loop_.Run(); // Will be terminated in OnURLFetchComplete(). message_loop_.Run(); // Will be terminated in OnURLFetchComplete().
} }
// Handles the request and returns a simple text content. Saves the // Handles |request| sent to |path| and returns the response per |content|,
// request URL for verification. // |content type|, and |code|. Saves the request URL for verification.
scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) { scoped_ptr<HttpResponse> HandleRequest(const std::string& path,
const std::string& content,
const std::string& content_type,
ResponseCode code,
const HttpRequest& request) {
request_relative_url_ = request.relative_url; request_relative_url_ = request.relative_url;
scoped_ptr<HttpResponse> http_response(new HttpResponse); GURL absolute_url = server_.GetURL(request.relative_url);
http_response->set_code(SUCCESS); if (absolute_url.path() == path) {
http_response->set_content("<b>Worked!</b>"); scoped_ptr<HttpResponse> http_response(new HttpResponse);
http_response->set_content_type("text/html"); http_response->set_code(code);
return http_response.Pass(); http_response->set_content(content);
http_response->set_content_type(content_type);
return http_response.Pass();
}
return scoped_ptr<HttpResponse>();
} }
protected: protected:
...@@ -115,7 +124,11 @@ TEST_F(HttpServerTest, GetURL) { ...@@ -115,7 +124,11 @@ TEST_F(HttpServerTest, GetURL) {
TEST_F(HttpServerTest, RegisterRequestHandler) { TEST_F(HttpServerTest, RegisterRequestHandler) {
server_.RegisterRequestHandler(base::Bind(&HttpServerTest::HandleRequest, server_.RegisterRequestHandler(base::Bind(&HttpServerTest::HandleRequest,
base::Unretained(this))); base::Unretained(this),
"/test",
"<b>Worked!</b>",
"text/html",
SUCCESS));
scoped_ptr<net::URLFetcher> fetcher( scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/test?q=foo"), net::URLFetcher::Create(server_.GetURL("/test?q=foo"),
...@@ -133,82 +146,6 @@ TEST_F(HttpServerTest, RegisterRequestHandler) { ...@@ -133,82 +146,6 @@ TEST_F(HttpServerTest, RegisterRequestHandler) {
EXPECT_EQ("/test?q=foo", request_relative_url_); EXPECT_EQ("/test?q=foo", request_relative_url_);
} }
TEST_F(HttpServerTest, RegisterDefaultResponse) {
HttpResponse http_response;
// MOVED is chosen here, as it's rather an unusual code.
http_response.set_code(MOVED);
http_response.set_content("<b>Moved!</b>");
http_response.set_content_type("text/html");
http_response.AddCustomHeader("Server", "test server");
server_.RegisterDefaultResponse("/test", http_response);
scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/test"),
net::URLFetcher::GET,
this));
fetcher->SetRequestContext(request_context_getter_.get());
fetcher->Start();
WaitForResponses(1);
EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status());
EXPECT_EQ(MOVED, fetcher->GetResponseCode());
EXPECT_EQ("<b>Moved!</b>", GetContentFromFetcher(*fetcher));
EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher));
const net::HttpResponseHeaders* headers = fetcher->GetResponseHeaders();
ASSERT_TRUE(headers);
ASSERT_TRUE(headers->HasHeaderValue("Server", "test server"));
}
TEST_F(HttpServerTest, RegisterTextResponse) {
server_.RegisterTextResponse("/test",
"Raspberry chocolate",
"text/plain",
SUCCESS);
scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/test"),
net::URLFetcher::GET,
this));
fetcher->SetRequestContext(request_context_getter_.get());
fetcher->Start();
WaitForResponses(1);
EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status());
EXPECT_EQ(SUCCESS, fetcher->GetResponseCode());
EXPECT_EQ("Raspberry chocolate", GetContentFromFetcher(*fetcher));
EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher));
}
// Test files cannot be opened on Android.
#if !defined(OS_ANDROID)
TEST_F(HttpServerTest, RegisterFileResponse) {
server_.RegisterFileResponse(
"/test",
test_util::GetTestFilePath("gdata/testfile.txt"),
"text/plain",
SUCCESS);
scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/test"),
net::URLFetcher::GET,
this));
fetcher->SetRequestContext(request_context_getter_.get());
fetcher->Start();
WaitForResponses(1);
EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status());
EXPECT_EQ(SUCCESS, fetcher->GetResponseCode());
// Trim the trailing whitespace as it can be CRLF on Windows...
const std::string content = GetContentFromFetcher(*fetcher);
std::string trimmed;
TrimWhitespaceASCII(content, TRIM_TRAILING, &trimmed);
EXPECT_EQ("test file", trimmed);
EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher));
}
#endif // !defined(OS_ANDROID)
TEST_F(HttpServerTest, DefaultNotFoundResponse) { TEST_F(HttpServerTest, DefaultNotFoundResponse) {
scoped_ptr<net::URLFetcher> fetcher( scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/non-existent"), net::URLFetcher::Create(server_.GetURL("/non-existent"),
...@@ -223,18 +160,27 @@ TEST_F(HttpServerTest, DefaultNotFoundResponse) { ...@@ -223,18 +160,27 @@ TEST_F(HttpServerTest, DefaultNotFoundResponse) {
} }
TEST_F(HttpServerTest, ConcurrentFetches) { TEST_F(HttpServerTest, ConcurrentFetches) {
server_.RegisterTextResponse("/test1", server_.RegisterRequestHandler(
"Raspberry chocolate", base::Bind(&HttpServerTest::HandleRequest,
"text/html", base::Unretained(this),
SUCCESS); "/test1",
server_.RegisterTextResponse("/test2", "Raspberry chocolate",
"Vanilla chocolate", "text/html",
"text/html", SUCCESS));
SUCCESS); server_.RegisterRequestHandler(
server_.RegisterTextResponse("/test3", base::Bind(&HttpServerTest::HandleRequest,
"No chocolates", base::Unretained(this),
"text/plain", "/test2",
NOT_FOUND); "Vanilla chocolate",
"text/html",
SUCCESS));
server_.RegisterRequestHandler(
base::Bind(&HttpServerTest::HandleRequest,
base::Unretained(this),
"/test3",
"No chocolates",
"text/plain",
NOT_FOUND));
scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>( scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>(
net::URLFetcher::Create(server_.GetURL("/test1"), net::URLFetcher::Create(server_.GetURL("/test1"),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment