OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stringprintf.h" |
| 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" |
| 17 #include "chrome/test/chromedriver/net/net_util.h" |
| 18 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
| 19 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/ip_endpoint.h" |
| 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/tcp_listen_socket.h" |
| 23 #include "net/server/http_server.h" |
| 24 #include "net/server/http_server_request_info.h" |
| 25 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 class FetchUrlTest : public testing::Test, |
| 31 public net::HttpServer::Delegate { |
| 32 public: |
| 33 FetchUrlTest() |
| 34 : io_thread_("io"), |
| 35 response_(kSendHello) { |
| 36 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 37 CHECK(io_thread_.StartWithOptions(options)); |
| 38 context_getter_ = new URLRequestContextGetter( |
| 39 io_thread_.message_loop_proxy()); |
| 40 base::WaitableEvent event(false, false); |
| 41 io_thread_.message_loop_proxy()->PostTask( |
| 42 FROM_HERE, |
| 43 base::Bind(&FetchUrlTest::InitOnIO, |
| 44 base::Unretained(this), &event)); |
| 45 event.Wait(); |
| 46 } |
| 47 |
| 48 virtual ~FetchUrlTest() { |
| 49 base::WaitableEvent event(false, false); |
| 50 io_thread_.message_loop_proxy()->PostTask( |
| 51 FROM_HERE, |
| 52 base::Bind(&FetchUrlTest::DestroyServerOnIO, |
| 53 base::Unretained(this), &event)); |
| 54 event.Wait(); |
| 55 } |
| 56 |
| 57 void InitOnIO(base::WaitableEvent* event) { |
| 58 net::TCPListenSocketFactory factory("127.0.0.1", 0); |
| 59 server_ = new net::HttpServer(factory, this); |
| 60 net::IPEndPoint address; |
| 61 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); |
| 62 server_url_ = GURL( |
| 63 base::StringPrintf("http://127.0.0.1:%d", address.port())); |
| 64 event->Signal(); |
| 65 } |
| 66 |
| 67 void DestroyServerOnIO(base::WaitableEvent* event) { |
| 68 server_ = NULL; |
| 69 event->Signal(); |
| 70 } |
| 71 |
| 72 // Overridden from net::HttpServer::Delegate: |
| 73 virtual void OnHttpRequest(int connection_id, |
| 74 const net::HttpServerRequestInfo& info) OVERRIDE { |
| 75 switch (response_) { |
| 76 case kSendHello: |
| 77 server_->Send200(connection_id, "hello", "text/plain"); |
| 78 break; |
| 79 case kSend404: |
| 80 server_->Send404(connection_id); |
| 81 break; |
| 82 case kClose: |
| 83 // net::HttpServer doesn't allow us to close connection during callback. |
| 84 MessageLoop::current()->PostTask( |
| 85 FROM_HERE, |
| 86 base::Bind(&net::HttpServer::Close, server_, connection_id)); |
| 87 break; |
| 88 default: |
| 89 break; |
| 90 } |
| 91 } |
| 92 |
| 93 virtual void OnWebSocketRequest( |
| 94 int connection_id, |
| 95 const net::HttpServerRequestInfo& info) OVERRIDE {} |
| 96 virtual void OnWebSocketMessage(int connection_id, |
| 97 const std::string& data) OVERRIDE {} |
| 98 virtual void OnClose(int connection_id) OVERRIDE {} |
| 99 |
| 100 protected: |
| 101 enum ServerResponse { |
| 102 kSendHello = 0, |
| 103 kSend404, |
| 104 kClose, |
| 105 }; |
| 106 |
| 107 base::Thread io_thread_; |
| 108 ServerResponse response_; |
| 109 scoped_refptr<net::HttpServer> server_; |
| 110 scoped_refptr<URLRequestContextGetter> context_getter_; |
| 111 GURL server_url_; |
| 112 }; |
| 113 |
| 114 } // namespace |
| 115 |
| 116 TEST_F(FetchUrlTest, Http200) { |
| 117 std::string response("stuff"); |
| 118 ASSERT_TRUE(FetchUrl(server_url_, context_getter_, &response)); |
| 119 ASSERT_STREQ("hello", response.c_str()); |
| 120 } |
| 121 |
| 122 TEST_F(FetchUrlTest, HttpNon200) { |
| 123 response_ = kSend404; |
| 124 std::string response("stuff"); |
| 125 ASSERT_FALSE(FetchUrl(server_url_, context_getter_, &response)); |
| 126 ASSERT_STREQ("stuff", response.c_str()); |
| 127 } |
| 128 |
| 129 TEST_F(FetchUrlTest, ConnectionClose) { |
| 130 response_ = kClose; |
| 131 std::string response("stuff"); |
| 132 ASSERT_FALSE(FetchUrl(server_url_, context_getter_, &response)); |
| 133 ASSERT_STREQ("stuff", response.c_str()); |
| 134 } |
| 135 |
| 136 TEST_F(FetchUrlTest, NoServer) { |
| 137 std::string response("stuff"); |
| 138 GURL bogus_url("http://localhost:33333"); |
| 139 ASSERT_FALSE(FetchUrl(bogus_url, context_getter_, &response)); |
| 140 ASSERT_STREQ("stuff", response.c_str()); |
| 141 } |
OLD | NEW |