Chromium Code Reviews| 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(base::StringPrintf("http://127.0.0.1:%d", address.port()) ); | |
|
klundberg
2012/11/30 20:58:06
Seems longer than 80 characters.
kkania
2013/02/28 21:51:58
Done.
| |
| 63 event->Signal(); | |
| 64 } | |
| 65 | |
| 66 void DestroyServerOnIO(base::WaitableEvent* event) { | |
| 67 server_ = NULL; | |
| 68 event->Signal(); | |
| 69 } | |
| 70 | |
| 71 // Overridden from net::HttpServer::Delegate: | |
| 72 virtual void OnHttpRequest(int connection_id, | |
| 73 const net::HttpServerRequestInfo& info) OVERRIDE { | |
| 74 switch (response_) { | |
| 75 case kSendHello: | |
| 76 server_->Send200(connection_id, "hello", "text/plain"); | |
| 77 break; | |
| 78 case kSend404: | |
| 79 server_->Send404(connection_id); | |
| 80 break; | |
| 81 case kClose: | |
| 82 // net::HttpServer doesn't allow us to close connection during callback. | |
| 83 MessageLoop::current()->PostTask( | |
| 84 FROM_HERE, | |
| 85 base::Bind(&net::HttpServer::Close, server_, connection_id)); | |
| 86 break; | |
| 87 default: | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 virtual void OnWebSocketRequest( | |
| 93 int connection_id, | |
| 94 const net::HttpServerRequestInfo& info) OVERRIDE {} | |
| 95 virtual void OnWebSocketMessage(int connection_id, | |
| 96 const std::string& data) OVERRIDE {} | |
| 97 virtual void OnClose(int connection_id) OVERRIDE {} | |
| 98 | |
| 99 protected: | |
| 100 enum ServerResponse { | |
| 101 kSendHello = 0, | |
| 102 kSend404, | |
| 103 kClose, | |
| 104 }; | |
| 105 | |
| 106 base::Thread io_thread_; | |
| 107 ServerResponse response_; | |
| 108 scoped_refptr<net::HttpServer> server_; | |
| 109 scoped_refptr<URLRequestContextGetter> context_getter_; | |
| 110 GURL server_url_; | |
| 111 }; | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 115 TEST_F(FetchUrlTest, Http200) { | |
| 116 std::string response("stuff"); | |
| 117 ASSERT_TRUE(FetchUrl(server_url_, context_getter_, &response)); | |
| 118 ASSERT_STREQ("hello", response.c_str()); | |
| 119 } | |
| 120 | |
| 121 TEST_F(FetchUrlTest, HttpNon200) { | |
| 122 response_ = kSend404; | |
| 123 std::string response("stuff"); | |
| 124 ASSERT_FALSE(FetchUrl(server_url_, context_getter_, &response)); | |
| 125 ASSERT_STREQ("stuff", response.c_str()); | |
| 126 } | |
| 127 | |
| 128 TEST_F(FetchUrlTest, ConnectionClose) { | |
| 129 response_ = kClose; | |
| 130 std::string response("stuff"); | |
| 131 ASSERT_FALSE(FetchUrl(server_url_, context_getter_, &response)); | |
| 132 ASSERT_STREQ("stuff", response.c_str()); | |
| 133 } | |
| 134 | |
| 135 TEST_F(FetchUrlTest, NoServer) { | |
| 136 std::string response("stuff"); | |
| 137 GURL bogus_url("http://localhost:33333"); | |
| 138 ASSERT_FALSE(FetchUrl(bogus_url, context_getter_, &response)); | |
| 139 ASSERT_STREQ("stuff", response.c_str()); | |
| 140 } | |
| OLD | NEW |