OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <windows.h> | 9 #include <windows.h> |
10 #endif | 10 #endif |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 EXPECT_NE(0, cipher_suite); | 116 EXPECT_NE(0, cipher_suite); |
117 } | 117 } |
118 | 118 |
119 } // namespace | 119 } // namespace |
120 | 120 |
121 // A network delegate that blocks requests, optionally cancelling or redirecting | 121 // A network delegate that blocks requests, optionally cancelling or redirecting |
122 // them. | 122 // them. |
123 class BlockingNetworkDelegate : public TestNetworkDelegate { | 123 class BlockingNetworkDelegate : public TestNetworkDelegate { |
124 public: | 124 public: |
125 BlockingNetworkDelegate() | 125 BlockingNetworkDelegate() |
126 : callback_retval_(net::OK), | 126 : retval_(net::ERR_IO_PENDING), |
| 127 callback_retval_(net::OK), |
127 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 128 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
128 | 129 |
| 130 void set_retval(int retval) { retval_ = retval; } |
129 void set_callback_retval(int retval) { callback_retval_ = retval; } | 131 void set_callback_retval(int retval) { callback_retval_ = retval; } |
130 void set_redirect_url(const GURL& url) { redirect_url_ = url; } | 132 void set_redirect_url(const GURL& url) { redirect_url_ = url; } |
131 | 133 |
132 private: | 134 private: |
133 // TestNetworkDelegate: | 135 // TestNetworkDelegate: |
134 virtual int OnBeforeURLRequest(net::URLRequest* request, | 136 virtual int OnBeforeURLRequest(net::URLRequest* request, |
135 net::CompletionCallback* callback, | 137 net::CompletionCallback* callback, |
136 GURL* new_url) { | 138 GURL* new_url) { |
137 if (redirect_url_ == request->url()) { | 139 if (redirect_url_ == request->url()) { |
138 // We've already seen this request and redirected elsewhere. | 140 // We've already seen this request and redirected elsewhere. |
139 return net::OK; | 141 return net::OK; |
140 } | 142 } |
141 | 143 |
142 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); | 144 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
143 | 145 |
144 if (!redirect_url_.is_empty()) | 146 if (!redirect_url_.is_empty()) |
145 *new_url = redirect_url_; | 147 *new_url = redirect_url_; |
| 148 |
| 149 if (retval_ != net::ERR_IO_PENDING) |
| 150 return retval_; |
| 151 |
146 MessageLoop::current()->PostTask( | 152 MessageLoop::current()->PostTask( |
147 FROM_HERE, | 153 FROM_HERE, |
148 method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback, | 154 method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback, |
149 callback)); | 155 callback)); |
150 return net::ERR_IO_PENDING; | 156 return net::ERR_IO_PENDING; |
151 } | 157 } |
152 | 158 |
153 void DoCallback(net::CompletionCallback* callback) { | 159 void DoCallback(net::CompletionCallback* callback) { |
154 callback->Run(callback_retval_); | 160 callback->Run(callback_retval_); |
155 } | 161 } |
156 | 162 |
| 163 int retval_; |
157 int callback_retval_; | 164 int callback_retval_; |
158 GURL redirect_url_; | 165 GURL redirect_url_; |
159 ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; | 166 ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; |
160 }; | 167 }; |
161 | 168 |
162 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f | 169 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
163 class URLRequestTest : public PlatformTest { | 170 class URLRequestTest : public PlatformTest { |
164 public: | 171 public: |
165 URLRequestTest() : default_context_(new TestURLRequestContext(true)) { | 172 URLRequestTest() : default_context_(new TestURLRequestContext(true)) { |
166 default_context_->set_network_delegate(&default_network_delegate_); | 173 default_context_->set_network_delegate(&default_network_delegate_); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 MessageLoop::current()->Run(); | 343 MessageLoop::current()->Run(); |
337 | 344 |
338 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); | 345 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
339 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().os_error()); | 346 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().os_error()); |
340 EXPECT_EQ(1, network_delegate.created_requests()); | 347 EXPECT_EQ(1, network_delegate.created_requests()); |
341 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 348 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
342 } | 349 } |
343 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 350 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
344 } | 351 } |
345 | 352 |
| 353 // Tests that the network delegate can cancel a request synchronously. |
| 354 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { |
| 355 ASSERT_TRUE(test_server_.Start()); |
| 356 |
| 357 TestDelegate d; |
| 358 BlockingNetworkDelegate network_delegate; |
| 359 network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
| 360 |
| 361 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 362 context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 363 context->set_network_delegate(&network_delegate); |
| 364 context->Init(); |
| 365 |
| 366 { |
| 367 TestURLRequest r(test_server_.GetURL(""), &d); |
| 368 r.set_context(context); |
| 369 |
| 370 r.Start(); |
| 371 MessageLoop::current()->Run(); |
| 372 |
| 373 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 374 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().os_error()); |
| 375 EXPECT_EQ(1, network_delegate.created_requests()); |
| 376 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 377 } |
| 378 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 379 } |
| 380 |
346 // Tests that the network delegate can block and redirect a request to a new | 381 // Tests that the network delegate can block and redirect a request to a new |
347 // URL. | 382 // URL. |
348 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { | 383 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
349 ASSERT_TRUE(test_server_.Start()); | 384 ASSERT_TRUE(test_server_.Start()); |
350 | 385 |
351 TestDelegate d; | 386 TestDelegate d; |
352 BlockingNetworkDelegate network_delegate; | 387 BlockingNetworkDelegate network_delegate; |
353 GURL redirect_url(test_server_.GetURL("simple.html")); | 388 GURL redirect_url(test_server_.GetURL("simple.html")); |
354 network_delegate.set_redirect_url(redirect_url); | 389 network_delegate.set_redirect_url(redirect_url); |
355 | 390 |
(...skipping 2777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3133 req.SetExtraRequestHeaders(headers); | 3168 req.SetExtraRequestHeaders(headers); |
3134 req.Start(); | 3169 req.Start(); |
3135 MessageLoop::current()->Run(); | 3170 MessageLoop::current()->Run(); |
3136 // If the net tests are being run with ChromeFrame then we need to allow for | 3171 // If the net tests are being run with ChromeFrame then we need to allow for |
3137 // the 'chromeframe' suffix which is added to the user agent before the | 3172 // the 'chromeframe' suffix which is added to the user agent before the |
3138 // closing parentheses. | 3173 // closing parentheses. |
3139 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); | 3174 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); |
3140 } | 3175 } |
3141 | 3176 |
3142 } // namespace net | 3177 } // namespace net |
OLD | NEW |