| OLD | NEW |
| 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 "chrome/browser/google_apis/test_server/http_server.h" | 5 #include "net/test/embedded_test_server/http_server.h" |
| 6 | 6 |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
| 9 #include "chrome/browser/google_apis/test_server/http_request.h" | |
| 10 #include "chrome/browser/google_apis/test_server/http_response.h" | |
| 11 #include "net/http/http_response_headers.h" | 9 #include "net/http/http_response_headers.h" |
| 10 #include "net/test/embedded_test_server/http_request.h" |
| 11 #include "net/test/embedded_test_server/http_response.h" |
| 12 #include "net/url_request/url_fetcher.h" | 12 #include "net/url_request/url_fetcher.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace google_apis { | 17 namespace google_apis { |
| 18 namespace test_server { | 18 namespace test_server { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 virtual void TearDown() OVERRIDE { | 64 virtual void TearDown() OVERRIDE { |
| 65 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); | 65 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // net::URLFetcherDelegate override. | 68 // net::URLFetcherDelegate override. |
| 69 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | 69 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 70 ++num_responses_received_; | 70 ++num_responses_received_; |
| 71 if (num_responses_received_ == num_responses_expected_) | 71 if (num_responses_received_ == num_responses_expected_) |
| 72 message_loop_.Quit(); | 72 MessageLoop::current()->Quit(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Waits until the specified number of responses are received. | 75 // Waits until the specified number of responses are received. |
| 76 void WaitForResponses(int num_responses) { | 76 void WaitForResponses(int num_responses) { |
| 77 num_responses_received_ = 0; | 77 num_responses_received_ = 0; |
| 78 num_responses_expected_ = num_responses; | 78 num_responses_expected_ = num_responses; |
| 79 message_loop_.Run(); // Will be terminated in OnURLFetchComplete(). | 79 // Will be terminated in OnURLFetchComplete(). |
| 80 MessageLoop::current()->Run(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 // Handles |request| sent to |path| and returns the response per |content|, | 83 // Handles |request| sent to |path| and returns the response per |content|, |
| 83 // |content type|, and |code|. Saves the request URL for verification. | 84 // |content type|, and |code|. Saves the request URL for verification. |
| 84 scoped_ptr<HttpResponse> HandleRequest(const std::string& path, | 85 scoped_ptr<HttpResponse> HandleRequest(const std::string& path, |
| 85 const std::string& content, | 86 const std::string& content, |
| 86 const std::string& content_type, | 87 const std::string& content_type, |
| 87 ResponseCode code, | 88 ResponseCode code, |
| 88 const HttpRequest& request) { | 89 const HttpRequest& request) { |
| 89 request_relative_url_ = request.relative_url; | 90 request_relative_url_ = request.relative_url; |
| 90 | 91 |
| 91 GURL absolute_url = server_->GetURL(request.relative_url); | 92 GURL absolute_url = server_->GetURL(request.relative_url); |
| 92 if (absolute_url.path() == path) { | 93 if (absolute_url.path() == path) { |
| 93 scoped_ptr<HttpResponse> http_response(new HttpResponse); | 94 scoped_ptr<HttpResponse> http_response(new HttpResponse); |
| 94 http_response->set_code(code); | 95 http_response->set_code(code); |
| 95 http_response->set_content(content); | 96 http_response->set_content(content); |
| 96 http_response->set_content_type(content_type); | 97 http_response->set_content_type(content_type); |
| 97 return http_response.Pass(); | 98 return http_response.Pass(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 return scoped_ptr<HttpResponse>(); | 101 return scoped_ptr<HttpResponse>(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 protected: | 104 protected: |
| 104 int num_responses_received_; | 105 int num_responses_received_; |
| 105 int num_responses_expected_; | 106 int num_responses_expected_; |
| 106 std::string request_relative_url_; | 107 std::string request_relative_url_; |
| 107 base::Thread io_thread_; | 108 base::Thread io_thread_; |
| 108 MessageLoop message_loop_; | |
| 109 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 109 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 110 scoped_ptr<HttpServer> server_; | 110 scoped_ptr<HttpServer> server_; |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 TEST_F(HttpServerTest, GetBaseURL) { | 113 TEST_F(HttpServerTest, GetBaseURL) { |
| 114 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%d/", server_->port()), | 114 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%d/", server_->port()), |
| 115 server_->base_url().spec()); | 115 server_->base_url().spec()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(HttpServerTest, GetURL) { | 118 TEST_F(HttpServerTest, GetURL) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); | 214 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); |
| 215 | 215 |
| 216 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); | 216 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); |
| 217 EXPECT_EQ(NOT_FOUND, fetcher3->GetResponseCode()); | 217 EXPECT_EQ(NOT_FOUND, fetcher3->GetResponseCode()); |
| 218 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); | 218 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); |
| 219 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); | 219 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); |
| 220 } | 220 } |
| 221 | 221 |
| 222 } // namespace test_server | 222 } // namespace test_server |
| 223 } // namespace google_apis | 223 } // namespace google_apis |
| OLD | NEW |