Chromium Code Reviews| 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 "net/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "crypto/nss_util.h" | |
| 13 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
| 14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 15 #include "net/log/test_net_log.h" | 16 #include "net/log/test_net_log.h" |
| 16 #include "net/socket/client_socket_factory.h" | 17 #include "net/socket/client_socket_factory.h" |
| 17 #include "net/socket/stream_socket.h" | 18 #include "net/socket/stream_socket.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server_connection_listener .h" | 19 #include "net/test/embedded_test_server/embedded_test_server_connection_listener .h" |
| 19 #include "net/test/embedded_test_server/http_request.h" | 20 #include "net/test/embedded_test_server/http_request.h" |
| 20 #include "net/test/embedded_test_server/http_response.h" | 21 #include "net/test/embedded_test_server/http_response.h" |
| 21 #include "net/test/spawned_test_server/base_test_server.h" | 22 #include "net/test/embedded_test_server/request_helpers.h" |
| 22 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
| 23 #include "net/url_request/url_fetcher_delegate.h" | 24 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_request.h" | |
| 24 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 28 |
| 29 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | |
| 30 #include "net/cert_net/nss_ocsp.h" | |
| 31 #endif | |
| 32 | |
| 27 namespace net { | 33 namespace net { |
| 28 namespace test_server { | 34 namespace test_server { |
| 29 | 35 |
| 30 namespace { | 36 namespace { |
| 31 | 37 |
| 32 // Gets the content from the given URLFetcher. | 38 // Gets the content from the given URLFetcher. |
| 33 std::string GetContentFromFetcher(const URLFetcher& fetcher) { | 39 std::string GetContentFromFetcher(const URLFetcher& fetcher) { |
| 34 std::string result; | 40 std::string result; |
| 35 const bool success = fetcher.GetResponseAsString(&result); | 41 const bool success = fetcher.GetResponseAsString(&result); |
| 36 EXPECT_TRUE(success); | 42 EXPECT_TRUE(success); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 bool did_read_from_socket_; | 100 bool did_read_from_socket_; |
| 95 | 101 |
| 96 base::RunLoop accept_loop_; | 102 base::RunLoop accept_loop_; |
| 97 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 103 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 98 | 104 |
| 99 mutable base::Lock lock_; | 105 mutable base::Lock lock_; |
| 100 | 106 |
| 101 DISALLOW_COPY_AND_ASSIGN(TestConnectionListener); | 107 DISALLOW_COPY_AND_ASSIGN(TestConnectionListener); |
| 102 }; | 108 }; |
| 103 | 109 |
| 104 class EmbeddedTestServerTest: public testing::Test, | 110 class EmbeddedTestServerTest |
| 105 public URLFetcherDelegate { | 111 : public testing::TestWithParam<EmbeddedTestServer::Type>, |
| 112 public URLFetcherDelegate { | |
| 106 public: | 113 public: |
| 107 EmbeddedTestServerTest() | 114 EmbeddedTestServerTest() |
| 108 : num_responses_received_(0), | 115 : num_responses_received_(0), |
| 109 num_responses_expected_(0), | 116 num_responses_expected_(0), |
| 110 io_thread_("io_thread") { | 117 io_thread_("io_thread") { |
| 111 } | 118 } |
| 112 | 119 |
| 113 void SetUp() override { | 120 void SetUp() override { |
| 121 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | |
| 122 // This is needed so NSS's HTTP client functions are initialized on the | |
| 123 // right thread. These tests create SSLClientSockets on a different thread. | |
| 124 // TODO(davidben): Initialization can't be deferred to SSLClientSocket. See | |
| 125 // https://crbug.com/539520. | |
| 126 crypto::EnsureNSSInit(); | |
| 127 EnsureNSSHttpIOInit(); | |
| 128 #endif | |
| 129 | |
| 114 base::Thread::Options thread_options; | 130 base::Thread::Options thread_options; |
| 115 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 131 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 116 ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); | 132 ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); |
| 117 | 133 |
| 118 request_context_getter_ = | 134 request_context_getter_ = |
| 119 new TestURLRequestContextGetter(io_thread_.task_runner()); | 135 new TestURLRequestContextGetter(io_thread_.task_runner()); |
| 120 | 136 |
| 121 server_.reset(new EmbeddedTestServer); | 137 server_.reset(new EmbeddedTestServer(GetParam())); |
| 122 server_->SetConnectionListener(&connection_listener_); | 138 server_->SetConnectionListener(&connection_listener_); |
| 123 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); | |
| 124 } | 139 } |
| 125 | 140 |
| 126 void TearDown() override { | 141 void TearDown() override { |
| 127 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); | 142 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); |
| 143 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | |
| 144 ShutdownNSSHttpIO(); | |
| 145 #endif | |
| 128 } | 146 } |
| 129 | 147 |
| 130 // URLFetcherDelegate override. | 148 // URLFetcherDelegate override. |
| 131 void OnURLFetchComplete(const URLFetcher* source) override { | 149 void OnURLFetchComplete(const URLFetcher* source) override { |
| 132 ++num_responses_received_; | 150 ++num_responses_received_; |
| 133 if (num_responses_received_ == num_responses_expected_) | 151 if (num_responses_received_ == num_responses_expected_) |
| 134 base::MessageLoop::current()->QuitWhenIdle(); | 152 base::MessageLoop::current()->QuitWhenIdle(); |
| 135 } | 153 } |
| 136 | 154 |
| 137 // Waits until the specified number of responses are received. | 155 // Waits until the specified number of responses are received. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 166 protected: | 184 protected: |
| 167 int num_responses_received_; | 185 int num_responses_received_; |
| 168 int num_responses_expected_; | 186 int num_responses_expected_; |
| 169 std::string request_relative_url_; | 187 std::string request_relative_url_; |
| 170 base::Thread io_thread_; | 188 base::Thread io_thread_; |
| 171 scoped_refptr<TestURLRequestContextGetter> request_context_getter_; | 189 scoped_refptr<TestURLRequestContextGetter> request_context_getter_; |
| 172 TestConnectionListener connection_listener_; | 190 TestConnectionListener connection_listener_; |
| 173 scoped_ptr<EmbeddedTestServer> server_; | 191 scoped_ptr<EmbeddedTestServer> server_; |
| 174 }; | 192 }; |
| 175 | 193 |
| 176 TEST_F(EmbeddedTestServerTest, GetBaseURL) { | 194 TEST_P(EmbeddedTestServerTest, GetBaseURL) { |
| 177 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%u/", server_->port()), | 195 ASSERT_TRUE(server_->Start()); |
| 178 server_->base_url().spec()); | 196 if (server_->is_using_ssl()) { |
| 197 EXPECT_EQ(base::StringPrintf("https://127.0.0.1:%u/", server_->port()), | |
| 198 server_->base_url().spec()); | |
| 199 } else { | |
| 200 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%u/", server_->port()), | |
| 201 server_->base_url().spec()); | |
| 202 } | |
| 179 } | 203 } |
| 180 | 204 |
| 181 TEST_F(EmbeddedTestServerTest, GetURL) { | 205 TEST_P(EmbeddedTestServerTest, GetURL) { |
| 182 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%u/path?query=foo", | 206 ASSERT_TRUE(server_->Start()); |
| 183 server_->port()), | 207 if (server_->is_using_ssl()) { |
| 184 server_->GetURL("/path?query=foo").spec()); | 208 EXPECT_EQ(base::StringPrintf("https://127.0.0.1:%u/path?query=foo", |
| 209 server_->port()), | |
| 210 server_->GetURL("/path?query=foo").spec()); | |
| 211 } else { | |
| 212 EXPECT_EQ(base::StringPrintf("http://127.0.0.1:%u/path?query=foo", | |
| 213 server_->port()), | |
| 214 server_->GetURL("/path?query=foo").spec()); | |
| 215 } | |
| 185 } | 216 } |
| 186 | 217 |
| 187 TEST_F(EmbeddedTestServerTest, GetURLWithHostname) { | 218 TEST_P(EmbeddedTestServerTest, GetURLWithHostname) { |
| 188 EXPECT_EQ(base::StringPrintf("http://foo.com:%d/path?query=foo", | 219 ASSERT_TRUE(server_->Start()); |
| 189 server_->port()), | 220 if (server_->is_using_ssl()) { |
| 190 server_->GetURL("foo.com", "/path?query=foo").spec()); | 221 EXPECT_EQ(base::StringPrintf("https://foo.com:%d/path?query=foo", |
| 222 server_->port()), | |
| 223 server_->GetURL("foo.com", "/path?query=foo").spec()); | |
| 224 } else { | |
| 225 EXPECT_EQ( | |
| 226 base::StringPrintf("http://foo.com:%d/path?query=foo", server_->port()), | |
| 227 server_->GetURL("foo.com", "/path?query=foo").spec()); | |
| 228 } | |
| 191 } | 229 } |
| 192 | 230 |
| 193 TEST_F(EmbeddedTestServerTest, RegisterRequestHandler) { | 231 TEST_P(EmbeddedTestServerTest, RegisterRequestHandler) { |
| 194 server_->RegisterRequestHandler( | 232 server_->RegisterRequestHandler( |
| 195 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 233 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
| 196 base::Unretained(this), | 234 base::Unretained(this), |
| 197 "/test", | 235 "/test", |
| 198 "<b>Worked!</b>", | 236 "<b>Worked!</b>", |
| 199 "text/html", | 237 "text/html", |
| 200 HTTP_OK)); | 238 HTTP_OK)); |
| 239 ASSERT_TRUE(server_->Start()); | |
| 201 | 240 |
| 202 scoped_ptr<URLFetcher> fetcher = | 241 scoped_ptr<URLFetcher> fetcher = |
| 203 URLFetcher::Create(server_->GetURL("/test?q=foo"), URLFetcher::GET, this); | 242 URLFetcher::Create(server_->GetURL("/test?q=foo"), URLFetcher::GET, this); |
| 204 fetcher->SetRequestContext(request_context_getter_.get()); | 243 fetcher->SetRequestContext(request_context_getter_.get()); |
| 205 fetcher->Start(); | 244 fetcher->Start(); |
| 206 WaitForResponses(1); | 245 WaitForResponses(1); |
| 207 | 246 |
| 208 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 247 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
| 209 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); | 248 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); |
| 210 EXPECT_EQ("<b>Worked!</b>", GetContentFromFetcher(*fetcher)); | 249 EXPECT_EQ("<b>Worked!</b>", GetContentFromFetcher(*fetcher)); |
| 211 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher)); | 250 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher)); |
| 212 | 251 |
| 213 EXPECT_EQ("/test?q=foo", request_relative_url_); | 252 EXPECT_EQ("/test?q=foo", request_relative_url_); |
| 214 } | 253 } |
| 215 | 254 |
| 216 TEST_F(EmbeddedTestServerTest, ServeFilesFromDirectory) { | 255 TEST_P(EmbeddedTestServerTest, ServeFilesFromDirectory) { |
| 217 base::FilePath src_dir; | 256 base::FilePath src_dir; |
| 218 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | 257 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
| 219 server_->ServeFilesFromDirectory( | 258 server_->ServeFilesFromDirectory( |
| 220 src_dir.AppendASCII("net").AppendASCII("data")); | 259 src_dir.AppendASCII("net").AppendASCII("data")); |
| 260 ASSERT_TRUE(server_->Start()); | |
| 221 | 261 |
| 222 scoped_ptr<URLFetcher> fetcher = | 262 scoped_ptr<URLFetcher> fetcher = |
| 223 URLFetcher::Create(server_->GetURL("/test.html"), URLFetcher::GET, this); | 263 URLFetcher::Create(server_->GetURL("/test.html"), URLFetcher::GET, this); |
| 224 fetcher->SetRequestContext(request_context_getter_.get()); | 264 fetcher->SetRequestContext(request_context_getter_.get()); |
| 225 fetcher->Start(); | 265 fetcher->Start(); |
| 226 WaitForResponses(1); | 266 WaitForResponses(1); |
| 227 | 267 |
| 228 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 268 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
| 229 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); | 269 EXPECT_EQ(HTTP_OK, fetcher->GetResponseCode()); |
| 230 EXPECT_EQ("<p>Hello World!</p>", GetContentFromFetcher(*fetcher)); | 270 EXPECT_EQ("<p>Hello World!</p>", GetContentFromFetcher(*fetcher)); |
| 231 EXPECT_EQ("", GetContentTypeFromFetcher(*fetcher)); | 271 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher)); |
| 232 } | 272 } |
| 233 | 273 |
| 234 TEST_F(EmbeddedTestServerTest, DefaultNotFoundResponse) { | 274 TEST_P(EmbeddedTestServerTest, DefaultNotFoundResponse) { |
| 275 ASSERT_TRUE(server_->Start()); | |
| 276 | |
| 235 scoped_ptr<URLFetcher> fetcher = URLFetcher::Create( | 277 scoped_ptr<URLFetcher> fetcher = URLFetcher::Create( |
| 236 server_->GetURL("/non-existent"), URLFetcher::GET, this); | 278 server_->GetURL("/non-existent"), URLFetcher::GET, this); |
| 237 fetcher->SetRequestContext(request_context_getter_.get()); | 279 fetcher->SetRequestContext(request_context_getter_.get()); |
| 238 | 280 |
| 239 fetcher->Start(); | 281 fetcher->Start(); |
| 240 WaitForResponses(1); | 282 WaitForResponses(1); |
| 241 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); | 283 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
| 242 EXPECT_EQ(HTTP_NOT_FOUND, fetcher->GetResponseCode()); | 284 EXPECT_EQ(HTTP_NOT_FOUND, fetcher->GetResponseCode()); |
| 243 } | 285 } |
| 244 | 286 |
| 245 TEST_F(EmbeddedTestServerTest, ConnectionListenerAccept) { | 287 TEST_P(EmbeddedTestServerTest, ConnectionListenerAccept) { |
| 288 ASSERT_TRUE(server_->Start()); | |
| 289 | |
| 246 TestNetLog net_log; | 290 TestNetLog net_log; |
| 247 net::AddressList address_list; | 291 net::AddressList address_list; |
| 248 EXPECT_TRUE(server_->GetAddressList(&address_list)); | 292 EXPECT_TRUE(server_->GetAddressList(&address_list)); |
| 249 | 293 |
| 250 scoped_ptr<StreamSocket> socket = | 294 scoped_ptr<StreamSocket> socket = |
| 251 ClientSocketFactory::GetDefaultFactory()->CreateTransportClientSocket( | 295 ClientSocketFactory::GetDefaultFactory()->CreateTransportClientSocket( |
| 252 address_list, &net_log, NetLog::Source()); | 296 address_list, &net_log, NetLog::Source()); |
| 253 TestCompletionCallback callback; | 297 TestCompletionCallback callback; |
| 254 ASSERT_EQ(OK, callback.GetResult(socket->Connect(callback.callback()))); | 298 ASSERT_EQ(OK, callback.GetResult(socket->Connect(callback.callback()))); |
| 255 | 299 |
| 256 connection_listener_.WaitUntilFirstConnectionAccepted(); | 300 connection_listener_.WaitUntilFirstConnectionAccepted(); |
| 257 | 301 |
| 258 EXPECT_EQ(1u, connection_listener_.SocketAcceptedCount()); | 302 EXPECT_EQ(1u, connection_listener_.SocketAcceptedCount()); |
| 259 EXPECT_FALSE(connection_listener_.DidReadFromSocket()); | 303 EXPECT_FALSE(connection_listener_.DidReadFromSocket()); |
| 260 } | 304 } |
| 261 | 305 |
| 262 TEST_F(EmbeddedTestServerTest, ConnectionListenerRead) { | 306 TEST_P(EmbeddedTestServerTest, ConnectionListenerRead) { |
| 307 ASSERT_TRUE(server_->Start()); | |
| 308 | |
| 263 scoped_ptr<URLFetcher> fetcher = URLFetcher::Create( | 309 scoped_ptr<URLFetcher> fetcher = URLFetcher::Create( |
| 264 server_->GetURL("/non-existent"), URLFetcher::GET, this); | 310 server_->GetURL("/non-existent"), URLFetcher::GET, this); |
| 265 fetcher->SetRequestContext(request_context_getter_.get()); | 311 fetcher->SetRequestContext(request_context_getter_.get()); |
| 266 | 312 |
| 267 fetcher->Start(); | 313 fetcher->Start(); |
| 268 WaitForResponses(1); | 314 WaitForResponses(1); |
| 269 EXPECT_EQ(1u, connection_listener_.SocketAcceptedCount()); | 315 EXPECT_EQ(1u, connection_listener_.SocketAcceptedCount()); |
| 270 EXPECT_TRUE(connection_listener_.DidReadFromSocket()); | 316 EXPECT_TRUE(connection_listener_.DidReadFromSocket()); |
| 271 } | 317 } |
| 272 | 318 |
| 273 TEST_F(EmbeddedTestServerTest, ConcurrentFetches) { | 319 TEST_P(EmbeddedTestServerTest, ConcurrentFetches) { |
| 274 server_->RegisterRequestHandler( | 320 server_->RegisterRequestHandler( |
| 275 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 321 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
| 276 base::Unretained(this), | 322 base::Unretained(this), |
| 277 "/test1", | 323 "/test1", |
| 278 "Raspberry chocolate", | 324 "Raspberry chocolate", |
| 279 "text/html", | 325 "text/html", |
| 280 HTTP_OK)); | 326 HTTP_OK)); |
| 281 server_->RegisterRequestHandler( | 327 server_->RegisterRequestHandler( |
| 282 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 328 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
| 283 base::Unretained(this), | 329 base::Unretained(this), |
| 284 "/test2", | 330 "/test2", |
| 285 "Vanilla chocolate", | 331 "Vanilla chocolate", |
| 286 "text/html", | 332 "text/html", |
| 287 HTTP_OK)); | 333 HTTP_OK)); |
| 288 server_->RegisterRequestHandler( | 334 server_->RegisterRequestHandler( |
| 289 base::Bind(&EmbeddedTestServerTest::HandleRequest, | 335 base::Bind(&EmbeddedTestServerTest::HandleRequest, |
| 290 base::Unretained(this), | 336 base::Unretained(this), |
| 291 "/test3", | 337 "/test3", |
| 292 "No chocolates", | 338 "No chocolates", |
| 293 "text/plain", | 339 "text/plain", |
| 294 HTTP_NOT_FOUND)); | 340 HTTP_NOT_FOUND)); |
| 341 ASSERT_TRUE(server_->Start()); | |
| 295 | 342 |
| 296 scoped_ptr<URLFetcher> fetcher1 = | 343 scoped_ptr<URLFetcher> fetcher1 = |
| 297 URLFetcher::Create(server_->GetURL("/test1"), URLFetcher::GET, this); | 344 URLFetcher::Create(server_->GetURL("/test1"), URLFetcher::GET, this); |
| 298 fetcher1->SetRequestContext(request_context_getter_.get()); | 345 fetcher1->SetRequestContext(request_context_getter_.get()); |
| 299 scoped_ptr<URLFetcher> fetcher2 = | 346 scoped_ptr<URLFetcher> fetcher2 = |
| 300 URLFetcher::Create(server_->GetURL("/test2"), URLFetcher::GET, this); | 347 URLFetcher::Create(server_->GetURL("/test2"), URLFetcher::GET, this); |
| 301 fetcher2->SetRequestContext(request_context_getter_.get()); | 348 fetcher2->SetRequestContext(request_context_getter_.get()); |
| 302 scoped_ptr<URLFetcher> fetcher3 = | 349 scoped_ptr<URLFetcher> fetcher3 = |
| 303 URLFetcher::Create(server_->GetURL("/test3"), URLFetcher::GET, this); | 350 URLFetcher::Create(server_->GetURL("/test3"), URLFetcher::GET, this); |
| 304 fetcher3->SetRequestContext(request_context_getter_.get()); | 351 fetcher3->SetRequestContext(request_context_getter_.get()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 318 EXPECT_EQ(HTTP_OK, fetcher2->GetResponseCode()); | 365 EXPECT_EQ(HTTP_OK, fetcher2->GetResponseCode()); |
| 319 EXPECT_EQ("Vanilla chocolate", GetContentFromFetcher(*fetcher2)); | 366 EXPECT_EQ("Vanilla chocolate", GetContentFromFetcher(*fetcher2)); |
| 320 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); | 367 EXPECT_EQ("text/html", GetContentTypeFromFetcher(*fetcher2)); |
| 321 | 368 |
| 322 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); | 369 EXPECT_EQ(URLRequestStatus::SUCCESS, fetcher3->GetStatus().status()); |
| 323 EXPECT_EQ(HTTP_NOT_FOUND, fetcher3->GetResponseCode()); | 370 EXPECT_EQ(HTTP_NOT_FOUND, fetcher3->GetResponseCode()); |
| 324 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); | 371 EXPECT_EQ("No chocolates", GetContentFromFetcher(*fetcher3)); |
| 325 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); | 372 EXPECT_EQ("text/plain", GetContentTypeFromFetcher(*fetcher3)); |
| 326 } | 373 } |
| 327 | 374 |
| 375 class CancelRequestDelegate : public TestDelegate { | |
| 376 public: | |
| 377 CancelRequestDelegate() {} | |
| 378 | |
| 379 void OnResponseStarted(URLRequest* request) override { | |
| 380 TestDelegate::OnResponseStarted(request); | |
| 381 request->Cancel(); | |
| 382 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 383 FROM_HERE, base::RunLoop().QuitClosure(), | |
| 384 base::TimeDelta::FromSeconds(1)); | |
| 385 } | |
| 386 }; | |
| 387 | |
| 388 class InfiniteResponse : public BasicHttpResponse { | |
| 389 public: | |
| 390 InfiniteResponse() : weak_ptr_factory_(this){}; | |
| 391 | |
|
mmenke
2015/10/21 20:21:36
private:
svaldez
2015/10/21 21:22:29
Done.
| |
| 392 void SendInfinite(const SendBytesCallback& send) { | |
| 393 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 394 FROM_HERE, | |
| 395 base::Bind(send, "echo", | |
| 396 base::Bind(&InfiniteResponse::SendInfinite, | |
| 397 weak_ptr_factory_.GetWeakPtr(), send))); | |
| 398 } | |
| 399 | |
| 400 void SendResponse(const SendBytesCallback& send, | |
| 401 const SendCompleteCallback& done) override { | |
| 402 send.Run(ToResponseString(), | |
| 403 base::Bind(&InfiniteResponse::SendInfinite, | |
| 404 weak_ptr_factory_.GetWeakPtr(), send)); | |
| 405 } | |
| 406 | |
| 407 base::WeakPtrFactory<InfiniteResponse> weak_ptr_factory_; | |
|
mmenke
2015/10/21 20:21:36
DISALLOW_COPY_AND_ASSIGN
mmenke
2015/10/21 20:21:37
include weak_ptr.
svaldez
2015/10/21 21:22:28
Done.
svaldez
2015/10/21 21:22:28
Done.
| |
| 408 }; | |
| 409 | |
| 410 scoped_ptr<HttpResponse> HandleInfiniteRequest(const HttpRequest& request) { | |
|
mmenke
2015/10/21 20:21:37
Just use HandlePrefixedRequest instead?
svaldez
2015/10/21 21:22:28
Done.
| |
| 411 if (ShouldHandle(request, "/infinite")) | |
| 412 return make_scoped_ptr(new InfiniteResponse); | |
| 413 return nullptr; | |
| 414 } | |
| 415 | |
| 416 TEST_P(EmbeddedTestServerTest, CancelDuringRead) { | |
|
mmenke
2015/10/21 20:21:36
Cancel -> Close, Read -> Write? (From the server'
mmenke
2015/10/21 20:21:37
Think this test is worth a comment. Something alo
svaldez
2015/10/21 21:22:28
Done.
svaldez
2015/10/21 21:22:28
Done.
| |
| 417 CancelRequestDelegate cancel_delegate; | |
| 418 TestURLRequestContext context; | |
| 419 server_->RegisterRequestHandler(base::Bind(&HandleInfiniteRequest)); | |
| 420 ASSERT_TRUE(server_->Start()); | |
| 421 | |
| 422 scoped_ptr<URLRequest> request = context.CreateRequest( | |
| 423 server_->GetURL("/"), DEFAULT_PRIORITY, &cancel_delegate); | |
|
mmenke
2015/10/21 20:21:36
/infinite
svaldez
2015/10/21 21:22:28
Done.
| |
| 424 request->Start(); | |
| 425 base::RunLoop().RunUntilIdle(); | |
|
mmenke
2015/10/21 20:21:36
This doesn't run long enough.
Use a TestDelegate
svaldez
2015/10/21 21:22:28
Not sure I understand. We still need to have some
mmenke
2015/10/21 21:36:38
You aren't even waiting for the response to be rec
| |
| 426 } | |
| 427 | |
| 428 INSTANTIATE_TEST_CASE_P(EmbeddedTestServerTestInstantiation, | |
| 429 EmbeddedTestServerTest, | |
| 430 testing::Values(EmbeddedTestServer::TYPE_HTTP, | |
| 431 EmbeddedTestServer::TYPE_HTTPS)); | |
| 432 | |
| 328 // Below test exercises EmbeddedTestServer's ability to cope with the situation | 433 // Below test exercises EmbeddedTestServer's ability to cope with the situation |
| 329 // where there is no MessageLoop available on the thread at EmbeddedTestServer | 434 // where there is no MessageLoop available on the thread at EmbeddedTestServer |
| 330 // initialization and/or destruction. | 435 // initialization and/or destruction. |
| 331 | 436 |
| 332 typedef std::tr1::tuple<bool, bool> ThreadingTestParams; | 437 typedef std::tr1::tuple<bool, bool, EmbeddedTestServer::Type> |
| 438 ThreadingTestParams; | |
| 333 | 439 |
| 334 class EmbeddedTestServerThreadingTest | 440 class EmbeddedTestServerThreadingTest |
| 335 : public testing::TestWithParam<ThreadingTestParams> {}; | 441 : public testing::TestWithParam<ThreadingTestParams> { |
| 442 void SetUp() override { | |
| 443 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | |
| 444 // This is needed so NSS's HTTP client functions are initialized on the | |
| 445 // right thread. These tests create SSLClientSockets on a different thread. | |
| 446 // TODO(davidben): Initialization can't be deferred to SSLClientSocket. See | |
| 447 // https://crbug.com/539520. | |
| 448 crypto::EnsureNSSInit(); | |
| 449 EnsureNSSHttpIOInit(); | |
| 450 #endif | |
| 451 } | |
| 452 | |
| 453 void TearDown() override { | |
| 454 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | |
| 455 ShutdownNSSHttpIO(); | |
| 456 #endif | |
| 457 } | |
| 458 }; | |
| 336 | 459 |
| 337 class EmbeddedTestServerThreadingTestDelegate | 460 class EmbeddedTestServerThreadingTestDelegate |
| 338 : public base::PlatformThread::Delegate, | 461 : public base::PlatformThread::Delegate, |
| 339 public URLFetcherDelegate { | 462 public URLFetcherDelegate { |
| 340 public: | 463 public: |
| 341 EmbeddedTestServerThreadingTestDelegate( | 464 EmbeddedTestServerThreadingTestDelegate( |
| 342 bool message_loop_present_on_initialize, | 465 bool message_loop_present_on_initialize, |
| 343 bool message_loop_present_on_shutdown) | 466 bool message_loop_present_on_shutdown, |
| 467 EmbeddedTestServer::Type type) | |
| 344 : message_loop_present_on_initialize_(message_loop_present_on_initialize), | 468 : message_loop_present_on_initialize_(message_loop_present_on_initialize), |
| 345 message_loop_present_on_shutdown_(message_loop_present_on_shutdown) {} | 469 message_loop_present_on_shutdown_(message_loop_present_on_shutdown), |
| 470 type_(type) {} | |
| 346 | 471 |
| 347 // base::PlatformThread::Delegate: | 472 // base::PlatformThread::Delegate: |
| 348 void ThreadMain() override { | 473 void ThreadMain() override { |
| 349 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner; | 474 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner; |
| 350 base::Thread io_thread("io_thread"); | 475 base::Thread io_thread("io_thread"); |
| 351 base::Thread::Options thread_options; | 476 base::Thread::Options thread_options; |
| 352 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 477 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 353 ASSERT_TRUE(io_thread.StartWithOptions(thread_options)); | 478 ASSERT_TRUE(io_thread.StartWithOptions(thread_options)); |
| 354 io_thread_runner = io_thread.task_runner(); | 479 io_thread_runner = io_thread.task_runner(); |
| 355 | 480 |
| 356 scoped_ptr<base::MessageLoop> loop; | 481 scoped_ptr<base::MessageLoop> loop; |
| 357 if (message_loop_present_on_initialize_) | 482 if (message_loop_present_on_initialize_) |
| 358 loop.reset(new base::MessageLoopForIO); | 483 loop.reset(new base::MessageLoopForIO); |
| 359 | 484 |
| 360 // Create the test server instance. | 485 // Create the test server instance. |
| 361 EmbeddedTestServer server; | 486 EmbeddedTestServer server(type_); |
| 362 base::FilePath src_dir; | 487 base::FilePath src_dir; |
| 363 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | 488 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
| 364 ASSERT_TRUE(server.InitializeAndWaitUntilReady()); | 489 ASSERT_TRUE(server.Start()); |
| 365 | 490 |
| 366 // Make a request and wait for the reply. | 491 // Make a request and wait for the reply. |
| 367 if (!loop) | 492 if (!loop) |
| 368 loop.reset(new base::MessageLoopForIO); | 493 loop.reset(new base::MessageLoopForIO); |
| 369 | 494 |
| 370 scoped_ptr<URLFetcher> fetcher = | 495 scoped_ptr<URLFetcher> fetcher = |
| 371 URLFetcher::Create(server.GetURL("/test?q=foo"), URLFetcher::GET, this); | 496 URLFetcher::Create(server.GetURL("/test?q=foo"), URLFetcher::GET, this); |
| 372 fetcher->SetRequestContext( | 497 fetcher->SetRequestContext( |
| 373 new TestURLRequestContextGetter(loop->task_runner())); | 498 new TestURLRequestContextGetter(loop->task_runner())); |
| 374 fetcher->Start(); | 499 fetcher->Start(); |
| 375 loop->Run(); | 500 loop->Run(); |
| 376 fetcher.reset(); | 501 fetcher.reset(); |
| 377 | 502 |
| 378 // Shut down. | 503 // Shut down. |
| 379 if (message_loop_present_on_shutdown_) | 504 if (message_loop_present_on_shutdown_) |
| 380 loop.reset(); | 505 loop.reset(); |
| 381 | 506 |
| 382 ASSERT_TRUE(server.ShutdownAndWaitUntilComplete()); | 507 ASSERT_TRUE(server.ShutdownAndWaitUntilComplete()); |
| 383 } | 508 } |
| 384 | 509 |
| 385 // URLFetcherDelegate override. | 510 // URLFetcherDelegate override. |
| 386 void OnURLFetchComplete(const URLFetcher* source) override { | 511 void OnURLFetchComplete(const URLFetcher* source) override { |
| 387 base::MessageLoop::current()->QuitWhenIdle(); | 512 base::MessageLoop::current()->QuitWhenIdle(); |
| 388 } | 513 } |
| 389 | 514 |
| 390 private: | 515 private: |
| 391 bool message_loop_present_on_initialize_; | 516 bool message_loop_present_on_initialize_; |
| 392 bool message_loop_present_on_shutdown_; | 517 bool message_loop_present_on_shutdown_; |
| 518 EmbeddedTestServer::Type type_; | |
| 393 | 519 |
| 394 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServerThreadingTestDelegate); | 520 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServerThreadingTestDelegate); |
| 395 }; | 521 }; |
| 396 | 522 |
| 397 TEST_P(EmbeddedTestServerThreadingTest, RunTest) { | 523 TEST_P(EmbeddedTestServerThreadingTest, RunTest) { |
| 398 // The actual test runs on a separate thread so it can screw with the presence | 524 // The actual test runs on a separate thread so it can screw with the presence |
| 399 // of a MessageLoop - the test suite already sets up a MessageLoop for the | 525 // of a MessageLoop - the test suite already sets up a MessageLoop for the |
| 400 // main test thread. | 526 // main test thread. |
| 401 base::PlatformThreadHandle thread_handle; | 527 base::PlatformThreadHandle thread_handle; |
| 402 EmbeddedTestServerThreadingTestDelegate delegate( | 528 EmbeddedTestServerThreadingTestDelegate delegate( |
| 403 std::tr1::get<0>(GetParam()), | 529 std::tr1::get<0>(GetParam()), std::tr1::get<1>(GetParam()), |
| 404 std::tr1::get<1>(GetParam())); | 530 std::tr1::get<2>(GetParam())); |
| 405 ASSERT_TRUE(base::PlatformThread::Create(0, &delegate, &thread_handle)); | 531 ASSERT_TRUE(base::PlatformThread::Create(0, &delegate, &thread_handle)); |
| 406 base::PlatformThread::Join(thread_handle); | 532 base::PlatformThread::Join(thread_handle); |
| 407 } | 533 } |
| 408 | 534 |
| 409 INSTANTIATE_TEST_CASE_P(EmbeddedTestServerThreadingTestInstantiation, | 535 INSTANTIATE_TEST_CASE_P( |
| 410 EmbeddedTestServerThreadingTest, | 536 EmbeddedTestServerThreadingTestInstantiation, |
| 411 testing::Combine(testing::Bool(), testing::Bool())); | 537 EmbeddedTestServerThreadingTest, |
| 538 testing::Combine(testing::Bool(), | |
| 539 testing::Bool(), | |
| 540 testing::Values(EmbeddedTestServer::TYPE_HTTP, | |
| 541 EmbeddedTestServer::TYPE_HTTPS))); | |
| 412 | 542 |
| 413 } // namespace test_server | 543 } // namespace test_server |
| 414 } // namespace net | 544 } // namespace net |
| OLD | NEW |