OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 <windows.h> |
| 6 #include <wininet.h> |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/scoped_handle_win.h" |
| 11 #include "chrome_frame/test/test_server.h" |
| 12 #include "net/base/cookie_monster.h" |
| 13 #include "net/base/host_resolver_proc.h" |
| 14 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/http/http_cache.h" |
| 16 #include "net/proxy/proxy_service.h" |
| 17 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_unittest.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 class TestServerTest: public testing::Test { |
| 22 protected: |
| 23 virtual void SetUp() { |
| 24 PathService::Get(base::DIR_SOURCE_ROOT, &source_path_); |
| 25 source_path_ = source_path_.Append(FILE_PATH_LITERAL("chrome_frame")); |
| 26 } |
| 27 virtual void TearDown() { |
| 28 } |
| 29 |
| 30 public: |
| 31 const FilePath& source_path() const { |
| 32 return source_path_; |
| 33 } |
| 34 |
| 35 protected: |
| 36 FilePath source_path_; |
| 37 }; |
| 38 |
| 39 namespace { |
| 40 |
| 41 class ScopedInternet { |
| 42 public: |
| 43 explicit ScopedInternet(HINTERNET handle) |
| 44 : h_(handle) { |
| 45 } |
| 46 ~ScopedInternet() { |
| 47 if (h_) { |
| 48 InternetCloseHandle(h_); |
| 49 } |
| 50 } |
| 51 |
| 52 operator HINTERNET() { |
| 53 return h_; |
| 54 } |
| 55 |
| 56 protected: |
| 57 HINTERNET h_; |
| 58 }; |
| 59 |
| 60 class URLRequestTestContext : public URLRequestContext { |
| 61 public: |
| 62 URLRequestTestContext() { |
| 63 host_resolver_ = net::CreateSystemHostResolver(); |
| 64 proxy_service_ = net::ProxyService::CreateNull(); |
| 65 ssl_config_service_ = new net::SSLConfigServiceDefaults; |
| 66 http_transaction_factory_ = |
| 67 new net::HttpCache( |
| 68 net::HttpNetworkLayer::CreateFactory(host_resolver_, proxy_service_, |
| 69 ssl_config_service_), |
| 70 disk_cache::CreateInMemoryCacheBackend(0)); |
| 71 // In-memory cookie store. |
| 72 cookie_store_ = new net::CookieMonster(); |
| 73 } |
| 74 |
| 75 virtual ~URLRequestTestContext() { |
| 76 delete http_transaction_factory_; |
| 77 } |
| 78 }; |
| 79 |
| 80 class TestURLRequest : public URLRequest { |
| 81 public: |
| 82 TestURLRequest(const GURL& url, Delegate* delegate) |
| 83 : URLRequest(url, delegate) { |
| 84 set_context(new URLRequestTestContext()); |
| 85 } |
| 86 }; |
| 87 |
| 88 class UrlTaskChain { |
| 89 public: |
| 90 UrlTaskChain(const char* url, UrlTaskChain* next) |
| 91 : url_(url), next_(next) { |
| 92 } |
| 93 |
| 94 void Run() { |
| 95 EXPECT_EQ(0, delegate_.response_started_count()); |
| 96 |
| 97 MessageLoopForIO loop; |
| 98 |
| 99 TestURLRequest r(GURL(url_), &delegate_); |
| 100 r.Start(); |
| 101 EXPECT_TRUE(r.is_pending()); |
| 102 |
| 103 MessageLoop::current()->Run(); |
| 104 |
| 105 EXPECT_EQ(1, delegate_.response_started_count()); |
| 106 EXPECT_FALSE(delegate_.received_data_before_response()); |
| 107 EXPECT_NE(0, delegate_.bytes_received()); |
| 108 } |
| 109 |
| 110 UrlTaskChain* next() const { |
| 111 return next_; |
| 112 } |
| 113 |
| 114 const std::string& response() const { |
| 115 return delegate_.data_received(); |
| 116 } |
| 117 |
| 118 protected: |
| 119 std::string url_; |
| 120 TestDelegate delegate_; |
| 121 UrlTaskChain* next_; |
| 122 }; |
| 123 |
| 124 DWORD WINAPI FetchUrl(void* param) { |
| 125 UrlTaskChain* task = reinterpret_cast<UrlTaskChain*>(param); |
| 126 while (task != NULL) { |
| 127 task->Run(); |
| 128 task = task->next(); |
| 129 } |
| 130 |
| 131 return 0; |
| 132 } |
| 133 |
| 134 struct QuitMessageHit { |
| 135 explicit QuitMessageHit(MessageLoopForUI* loop) : loop_(loop), hit_(false) { |
| 136 } |
| 137 |
| 138 MessageLoopForUI* loop_; |
| 139 bool hit_; |
| 140 }; |
| 141 |
| 142 void QuitMessageLoop(QuitMessageHit* msg) { |
| 143 msg->hit_ = true; |
| 144 msg->loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 145 } |
| 146 |
| 147 } // end namespace |
| 148 |
| 149 TEST_F(TestServerTest, DISABLED_TestServer) { |
| 150 // The web server needs a loop to exist on this thread during construction |
| 151 // the loop must be created before we construct the server. |
| 152 MessageLoopForUI loop; |
| 153 |
| 154 test_server::SimpleWebServer server(1337); |
| 155 test_server::SimpleResponse person("/person", "Guthrie Govan!"); |
| 156 server.AddResponse(&person); |
| 157 test_server::FileResponse file("/file", source_path().Append( |
| 158 FILE_PATH_LITERAL("CFInstance.js"))); |
| 159 server.AddResponse(&file); |
| 160 test_server::RedirectResponse redir("/goog", "http://www.google.com/"); |
| 161 server.AddResponse(&redir); |
| 162 |
| 163 // We should never hit this, but it's our way to break out of the test if |
| 164 // things start hanging. |
| 165 QuitMessageHit quit_msg(&loop); |
| 166 loop.PostDelayedTask(FROM_HERE, |
| 167 NewRunnableFunction(QuitMessageLoop, &quit_msg), |
| 168 10 * 1000); |
| 169 |
| 170 UrlTaskChain quit_task("http://localhost:1337/quit", NULL); |
| 171 UrlTaskChain fnf_task("http://localhost:1337/404", &quit_task); |
| 172 UrlTaskChain person_task("http://localhost:1337/person", &fnf_task); |
| 173 UrlTaskChain file_task("http://localhost:1337/file", &person_task); |
| 174 UrlTaskChain goog_task("http://localhost:1337/goog", &file_task); |
| 175 |
| 176 DWORD tid = 0; |
| 177 ScopedHandle worker(::CreateThread(NULL, 0, FetchUrl, &goog_task, 0, &tid)); |
| 178 loop.MessageLoop::Run(); |
| 179 |
| 180 EXPECT_FALSE(quit_msg.hit_); |
| 181 if (!quit_msg.hit_) { |
| 182 EXPECT_EQ(::WaitForSingleObject(worker, 10 * 1000), WAIT_OBJECT_0); |
| 183 |
| 184 EXPECT_EQ(person.accessed(), 1); |
| 185 EXPECT_EQ(file.accessed(), 1); |
| 186 EXPECT_EQ(redir.accessed(), 1); |
| 187 |
| 188 EXPECT_TRUE(person_task.response().find("Guthrie") != std::string::npos); |
| 189 EXPECT_TRUE(file_task.response().find("function") != std::string::npos); |
| 190 EXPECT_TRUE(goog_task.response().find("<title>") != std::string::npos); |
| 191 } else { |
| 192 ::TerminateThread(worker, ~0); |
| 193 } |
| 194 } |
OLD | NEW |