OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/url_request/url_request_unittest.h" | 5 #include "net/url_request/url_request_unittest.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 84 matching lines...) Loading... |
95 protected: | 95 protected: |
96 static void SetUpTestCase() { | 96 static void SetUpTestCase() { |
97 server_ = HTTPTestServer::CreateForkingServer( | 97 server_ = HTTPTestServer::CreateForkingServer( |
98 L"net/data/url_request_unittest/"); | 98 L"net/data/url_request_unittest/"); |
99 } | 99 } |
100 | 100 |
101 static void TearDownTestCase() { | 101 static void TearDownTestCase() { |
102 server_ = NULL; | 102 server_ = NULL; |
103 } | 103 } |
104 | 104 |
| 105 void HTTPUploadDataOperationTest(const std::string& method) { |
| 106 ASSERT_TRUE(NULL != server_.get()); |
| 107 const int kMsgSize = 20000; // multiple of 10 |
| 108 const int kIterations = 50; |
| 109 char *uploadBytes = new char[kMsgSize+1]; |
| 110 char *ptr = uploadBytes; |
| 111 char marker = 'a'; |
| 112 for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 113 memcpy(ptr, "----------", 10); |
| 114 ptr += 10; |
| 115 if (idx % 100 == 0) { |
| 116 ptr--; |
| 117 *ptr++ = marker; |
| 118 if (++marker > 'z') |
| 119 marker = 'a'; |
| 120 } |
| 121 } |
| 122 uploadBytes[kMsgSize] = '\0'; |
| 123 |
| 124 scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
| 125 |
| 126 for (int i = 0; i < kIterations; ++i) { |
| 127 TestDelegate d; |
| 128 URLRequest r(server_->TestServerPage("echo"), &d); |
| 129 r.set_context(context); |
| 130 r.set_method(method.c_str()); |
| 131 |
| 132 r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 133 |
| 134 r.Start(); |
| 135 EXPECT_TRUE(r.is_pending()); |
| 136 |
| 137 MessageLoop::current()->Run(); |
| 138 |
| 139 ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 140 (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 141 |
| 142 EXPECT_FALSE(d.received_data_before_response()); |
| 143 EXPECT_EQ(uploadBytes, d.data_received()); |
| 144 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); |
| 145 EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 146 } |
| 147 delete[] uploadBytes; |
| 148 } |
| 149 |
105 static scoped_refptr<HTTPTestServer> server_; | 150 static scoped_refptr<HTTPTestServer> server_; |
106 }; | 151 }; |
107 | 152 |
108 // static | 153 // static |
109 scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_; | 154 scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_; |
110 | 155 |
111 TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { | 156 TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
112 // In this unit test, we're using the HTTPTestServer as a proxy server and | 157 // In this unit test, we're using the HTTPTestServer as a proxy server and |
113 // issuing a CONNECT request with the magic host name "www.redirect.com". | 158 // issuing a CONNECT request with the magic host name "www.redirect.com". |
114 // The HTTPTestServer will return a 302 response, which we should not | 159 // The HTTPTestServer will return a 302 response, which we should not |
(...skipping 362 matching lines...) Loading... |
477 MessageLoop::current()->Run(); | 522 MessageLoop::current()->Run(); |
478 | 523 |
479 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 524 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
480 EXPECT_EQ(1, d.response_started_count()); | 525 EXPECT_EQ(1, d.response_started_count()); |
481 EXPECT_EQ(0, d.bytes_received()); | 526 EXPECT_EQ(0, d.bytes_received()); |
482 EXPECT_FALSE(d.received_data_before_response()); | 527 EXPECT_FALSE(d.received_data_before_response()); |
483 } | 528 } |
484 } | 529 } |
485 | 530 |
486 TEST_F(URLRequestTestHTTP, PostTest) { | 531 TEST_F(URLRequestTestHTTP, PostTest) { |
487 ASSERT_TRUE(NULL != server_.get()); | 532 HTTPUploadDataOperationTest("POST"); |
488 const int kMsgSize = 20000; // multiple of 10 | 533 } |
489 const int kIterations = 50; | |
490 char *uploadBytes = new char[kMsgSize+1]; | |
491 char *ptr = uploadBytes; | |
492 char marker = 'a'; | |
493 for (int idx = 0; idx < kMsgSize/10; idx++) { | |
494 memcpy(ptr, "----------", 10); | |
495 ptr += 10; | |
496 if (idx % 100 == 0) { | |
497 ptr--; | |
498 *ptr++ = marker; | |
499 if (++marker > 'z') | |
500 marker = 'a'; | |
501 } | |
502 } | |
503 uploadBytes[kMsgSize] = '\0'; | |
504 | 534 |
505 scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); | 535 TEST_F(URLRequestTestHTTP, PutTest) { |
506 | 536 HTTPUploadDataOperationTest("PUT"); |
507 for (int i = 0; i < kIterations; ++i) { | |
508 TestDelegate d; | |
509 URLRequest r(server_->TestServerPage("echo"), &d); | |
510 r.set_context(context); | |
511 r.set_method("POST"); | |
512 | |
513 r.AppendBytesToUpload(uploadBytes, kMsgSize); | |
514 | |
515 r.Start(); | |
516 EXPECT_TRUE(r.is_pending()); | |
517 | |
518 MessageLoop::current()->Run(); | |
519 | |
520 ASSERT_EQ(1, d.response_started_count()) << "request failed: " << | |
521 (int) r.status().status() << ", os error: " << r.status().os_error(); | |
522 | |
523 EXPECT_FALSE(d.received_data_before_response()); | |
524 EXPECT_EQ(uploadBytes, d.data_received()); | |
525 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); | |
526 EXPECT_EQ(d.data_received().compare(uploadBytes), 0); | |
527 } | |
528 delete[] uploadBytes; | |
529 } | 537 } |
530 | 538 |
531 TEST_F(URLRequestTestHTTP, PostEmptyTest) { | 539 TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
532 ASSERT_TRUE(NULL != server_.get()); | 540 ASSERT_TRUE(NULL != server_.get()); |
533 TestDelegate d; | 541 TestDelegate d; |
534 { | 542 { |
535 TestURLRequest r(server_->TestServerPage("echo"), &d); | 543 TestURLRequest r(server_->TestServerPage("echo"), &d); |
536 r.set_method("POST"); | 544 r.set_method("POST"); |
537 | 545 |
538 r.Start(); | 546 r.Start(); |
(...skipping 1617 matching lines...) Loading... |
2156 ASSERT_TRUE(NULL != server_.get()); | 2164 ASSERT_TRUE(NULL != server_.get()); |
2157 TestDelegate d; | 2165 TestDelegate d; |
2158 TestURLRequest | 2166 TestURLRequest |
2159 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d); | 2167 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d); |
2160 req.set_context(new URLRequestTestContext()); | 2168 req.set_context(new URLRequestTestContext()); |
2161 req.SetExtraRequestHeaders("Accept-Charset: koi-8r"); | 2169 req.SetExtraRequestHeaders("Accept-Charset: koi-8r"); |
2162 req.Start(); | 2170 req.Start(); |
2163 MessageLoop::current()->Run(); | 2171 MessageLoop::current()->Run(); |
2164 EXPECT_EQ(std::string("koi-8r"), d.data_received()); | 2172 EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
2165 } | 2173 } |
OLD | NEW |