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(
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,
net::StreamListenSocket* connection) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
......
......@@ -30,7 +30,7 @@ class HttpListenSocket: public net::TCPListenSocket {
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,
// it assumes that the request syntax is correct. It *does not* support
// a Chunked Transfer Encoding.
......@@ -38,22 +38,27 @@ class HttpListenSocket: public net::TCPListenSocket {
// The common use case is below:
//
// scoped_ptr<HttpServer> test_server_;
// GURL hello_world_url_;
// GURL file_url_;
// (...)
//
// void SetUp() {
// test_server_.reset(new HttpServer());
// DCHECK(test_server_.InitializeAndWaitUntilReady());
// hello_world_url = test_server->RegisterTextResponse(
// "/abc",
// "<b>Hello world!</b>",
// "text/html");
// metadata_url = test_server->RegisterFileResponse(
// "metadata/file.doc")
// "testing/data/metadata.xml",
// "text/xml",
// 200);
// test_server->RegisterRequestHandler(
// base::Bind(&FooTest::HandleRequest, base::Unretained(this)));
// }
//
// void HandleRequest(const HttpRequest& request) {
// GURL absolute_url = test_server_.GetURL(request.relative_url);
// if (absolute_url.path() != "/test")
// return scoped_ptr<test_server::HttpResponse>();
//
// 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 {
public:
typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)>
......@@ -93,27 +98,6 @@ class HttpServer : private net::StreamListenSocket::Delegate {
// on UI thread.
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:
// Initializes and starts the server. If initialization succeeds, Starts()
// will return true.
......
......@@ -79,16 +79,25 @@ class HttpServerTest : public testing::Test,
message_loop_.Run(); // Will be terminated in OnURLFetchComplete().
}
// Handles the request and returns a simple text content. Saves the
// request URL for verification.
scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
// Handles |request| sent to |path| and returns the response per |content|,
// |content type|, and |code|. Saves the request URL for verification.
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;
scoped_ptr<HttpResponse> http_response(new HttpResponse);
http_response->set_code(SUCCESS);
http_response->set_content("<b>Worked!</b>");
http_response->set_content_type("text/html");
return http_response.Pass();
GURL absolute_url = server_.GetURL(request.relative_url);
if (absolute_url.path() == path) {
scoped_ptr<HttpResponse> http_response(new HttpResponse);
http_response->set_code(code);
http_response->set_content(content);
http_response->set_content_type(content_type);
return http_response.Pass();
}
return scoped_ptr<HttpResponse>();
}
protected:
......@@ -115,7 +124,11 @@ TEST_F(HttpServerTest, GetURL) {
TEST_F(HttpServerTest, RegisterRequestHandler) {
server_.RegisterRequestHandler(base::Bind(&HttpServerTest::HandleRequest,
base::Unretained(this)));
base::Unretained(this),
"/test",
"<b>Worked!</b>",
"text/html",
SUCCESS));
scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/test?q=foo"),
......@@ -133,82 +146,6 @@ TEST_F(HttpServerTest, RegisterRequestHandler) {
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) {
scoped_ptr<net::URLFetcher> fetcher(
net::URLFetcher::Create(server_.GetURL("/non-existent"),
......@@ -223,18 +160,27 @@ TEST_F(HttpServerTest, DefaultNotFoundResponse) {
}
TEST_F(HttpServerTest, ConcurrentFetches) {
server_.RegisterTextResponse("/test1",
"Raspberry chocolate",
"text/html",
SUCCESS);
server_.RegisterTextResponse("/test2",
"Vanilla chocolate",
"text/html",
SUCCESS);
server_.RegisterTextResponse("/test3",
"No chocolates",
"text/plain",
NOT_FOUND);
server_.RegisterRequestHandler(
base::Bind(&HttpServerTest::HandleRequest,
base::Unretained(this),
"/test1",
"Raspberry chocolate",
"text/html",
SUCCESS));
server_.RegisterRequestHandler(
base::Bind(&HttpServerTest::HandleRequest,
base::Unretained(this),
"/test2",
"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>(
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