| 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/http/http_util.h" | 10 #include "net/http/http_util.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 void Start() { | 96 void Start() { |
| 97 InSequence s; | 97 InSequence s; |
| 98 EXPECT_CALL(*url_loader_, loadAsynchronously(_, loader_.get())); | 98 EXPECT_CALL(*url_loader_, loadAsynchronously(_, loader_.get())); |
| 99 loader_->Start( | 99 loader_->Start( |
| 100 NewCallback(this, &BufferedResourceLoaderTest::StartCallback), | 100 NewCallback(this, &BufferedResourceLoaderTest::StartCallback), |
| 101 NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback), | 101 NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback), |
| 102 frame_.get()); | 102 frame_.get()); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void FullResponse(int64 instance_size) { | 105 void FullResponse(int64 instance_size) { |
| 106 EXPECT_CALL(*this, StartCallback(net::OK)); | 106 FullResponse(instance_size, net::OK); |
| 107 } |
| 108 |
| 109 void FullResponse(int64 instance_size, int status) { |
| 110 EXPECT_CALL(*this, StartCallback(status)); |
| 111 if (status != net::OK) { |
| 112 EXPECT_CALL(*url_loader_, cancel()) |
| 113 .WillOnce(RequestCanceled(loader_)); |
| 114 } |
| 107 | 115 |
| 108 WebURLResponse response(gurl_); | 116 WebURLResponse response(gurl_); |
| 109 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), | 117 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), |
| 110 WebString::fromUTF8(base::StringPrintf("%" | 118 WebString::fromUTF8(base::StringPrintf("%" |
| 111 PRId64, instance_size))); | 119 PRId64, instance_size))); |
| 112 response.setExpectedContentLength(instance_size); | 120 response.setExpectedContentLength(instance_size); |
| 113 response.setHTTPStatusCode(kHttpOK); | 121 response.setHTTPStatusCode(kHttpOK); |
| 114 loader_->didReceiveResponse(url_loader_, response); | 122 loader_->didReceiveResponse(url_loader_, response); |
| 115 EXPECT_EQ(instance_size, loader_->content_length()); | 123 |
| 116 EXPECT_EQ(instance_size, loader_->instance_size()); | 124 if (status == net::OK) { |
| 117 EXPECT_FALSE(loader_->partial_response()); | 125 EXPECT_EQ(instance_size, loader_->content_length()); |
| 126 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 127 } |
| 128 |
| 129 EXPECT_FALSE(loader_->range_supported()); |
| 118 } | 130 } |
| 119 | 131 |
| 120 void PartialResponse(int64 first_position, int64 last_position, | 132 void PartialResponse(int64 first_position, int64 last_position, |
| 121 int64 instance_size) { | 133 int64 instance_size) { |
| 122 EXPECT_CALL(*this, StartCallback(net::OK)); | 134 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 123 int64 content_length = last_position - first_position + 1; | 135 int64 content_length = last_position - first_position + 1; |
| 124 | 136 |
| 125 WebURLResponse response(gurl_); | 137 WebURLResponse response(gurl_); |
| 138 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), |
| 139 WebString::fromUTF8("bytes")); |
| 126 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), | 140 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), |
| 127 WebString::fromUTF8(base::StringPrintf("bytes " | 141 WebString::fromUTF8(base::StringPrintf("bytes " |
| 128 "%" PRId64 "-%" PRId64 "/%" PRId64, | 142 "%" PRId64 "-%" PRId64 "/%" PRId64, |
| 129 first_position, | 143 first_position, |
| 130 last_position, | 144 last_position, |
| 131 instance_size))); | 145 instance_size))); |
| 132 response.setExpectedContentLength(content_length); | 146 response.setExpectedContentLength(content_length); |
| 133 response.setHTTPStatusCode(kHttpPartialContent); | 147 response.setHTTPStatusCode(kHttpPartialContent); |
| 134 loader_->didReceiveResponse(url_loader_, response); | 148 loader_->didReceiveResponse(url_loader_, response); |
| 135 EXPECT_EQ(content_length, loader_->content_length()); | 149 EXPECT_EQ(content_length, loader_->content_length()); |
| 136 EXPECT_EQ(instance_size, loader_->instance_size()); | 150 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 137 EXPECT_TRUE(loader_->partial_response()); | 151 EXPECT_TRUE(loader_->range_supported()); |
| 138 } | 152 } |
| 139 | 153 |
| 140 void Redirect(const char* url) { | 154 void Redirect(const char* url) { |
| 141 GURL redirectUrl(url); | 155 GURL redirectUrl(url); |
| 142 WebKit::WebURLRequest newRequest(redirectUrl); | 156 WebKit::WebURLRequest newRequest(redirectUrl); |
| 143 WebKit::WebURLResponse redirectResponse(gurl_); | 157 WebKit::WebURLResponse redirectResponse(gurl_); |
| 144 | 158 |
| 145 loader_->willSendRequest(url_loader_, newRequest, redirectResponse); | 159 loader_->willSendRequest(url_loader_, newRequest, redirectResponse); |
| 146 | 160 |
| 147 MessageLoop::current()->RunAllPending(); | 161 MessageLoop::current()->RunAllPending(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 WebURLResponse response(gurl_); | 231 WebURLResponse response(gurl_); |
| 218 response.setHTTPStatusCode(404); | 232 response.setHTTPStatusCode(404); |
| 219 response.setHTTPStatusText("Not Found\n"); | 233 response.setHTTPStatusText("Not Found\n"); |
| 220 loader_->didReceiveResponse(url_loader_, response); | 234 loader_->didReceiveResponse(url_loader_, response); |
| 221 } | 235 } |
| 222 | 236 |
| 223 // Tests that partial content is requested but not fulfilled. | 237 // Tests that partial content is requested but not fulfilled. |
| 224 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { | 238 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { |
| 225 Initialize(kHttpUrl, 100, -1); | 239 Initialize(kHttpUrl, 100, -1); |
| 226 Start(); | 240 Start(); |
| 227 FullResponse(1024); | 241 FullResponse(1024, net::ERR_INVALID_RESPONSE); |
| 228 StopWhenLoad(); | |
| 229 } | 242 } |
| 230 | 243 |
| 231 // Tests that a 200 response is received. | 244 // Tests that a 200 response is received. |
| 232 TEST_F(BufferedResourceLoaderTest, FullResponse) { | 245 TEST_F(BufferedResourceLoaderTest, FullResponse) { |
| 233 Initialize(kHttpUrl, -1, -1); | 246 Initialize(kHttpUrl, -1, -1); |
| 234 Start(); | 247 Start(); |
| 235 FullResponse(1024); | 248 FullResponse(1024); |
| 236 StopWhenLoad(); | 249 StopWhenLoad(); |
| 237 } | 250 } |
| 238 | 251 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 Start(); | 496 Start(); |
| 484 Redirect(kHttpRedirectToSameDomainUrl1); | 497 Redirect(kHttpRedirectToSameDomainUrl1); |
| 485 Redirect(kHttpRedirectToDifferentDomainUrl1); | 498 Redirect(kHttpRedirectToDifferentDomainUrl1); |
| 486 EXPECT_FALSE(loader_->HasSingleOrigin()); | 499 EXPECT_FALSE(loader_->HasSingleOrigin()); |
| 487 StopWhenLoad(); | 500 StopWhenLoad(); |
| 488 } | 501 } |
| 489 | 502 |
| 490 // TODO(hclam): add unit test for defer loading. | 503 // TODO(hclam): add unit test for defer loading. |
| 491 | 504 |
| 492 } // namespace webkit_glue | 505 } // namespace webkit_glue |
| OLD | NEW |