OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "modules/fetch/Response.h" | 5 #include "modules/fetch/Response.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
9 #include "bindings/core/v8/V8BindingForTesting.h" | 9 #include "bindings/core/v8/V8BindingForTesting.h" |
10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
11 #include "core/frame/Frame.h" | 11 #include "core/frame/Frame.h" |
12 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
13 #include "modules/fetch/BodyStreamBuffer.h" | 13 #include "modules/fetch/BodyStreamBuffer.h" |
14 #include "modules/fetch/BytesConsumer.h" | 14 #include "modules/fetch/BytesConsumer.h" |
15 #include "modules/fetch/BytesConsumerTestUtil.h" | 15 #include "modules/fetch/BytesConsumerTestUtil.h" |
16 #include "modules/fetch/DataConsumerHandleTestUtil.h" | 16 #include "modules/fetch/DataConsumerHandleTestUtil.h" |
17 #include "modules/fetch/FetchResponseData.h" | 17 #include "modules/fetch/FetchResponseData.h" |
18 #include "platform/blob/BlobData.h" | 18 #include "platform/blob/BlobData.h" |
19 #include "platform/testing/UnitTestHelpers.h" | 19 #include "platform/testing/UnitTestHelpers.h" |
20 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" | 20 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "wtf/PtrUtil.h" | 22 #include "wtf/PtrUtil.h" |
| 23 #include "wtf/Vector.h" |
23 #include <memory> | 24 #include <memory> |
24 | 25 |
25 namespace blink { | 26 namespace blink { |
26 namespace { | 27 namespace { |
27 | 28 |
28 std::unique_ptr<WebServiceWorkerResponse> createTestWebServiceWorkerResponse() { | 29 std::unique_ptr<WebServiceWorkerResponse> createTestWebServiceWorkerResponse() { |
29 const KURL url(ParsedURLString, "http://www.webresponse.com/"); | 30 const KURL url(ParsedURLString, "http://www.webresponse.com/"); |
30 const unsigned short status = 200; | 31 const unsigned short status = 200; |
31 const String statusText = "the best status text"; | 32 const String statusText = "the best status text"; |
32 struct { | 33 struct { |
33 const char* key; | 34 const char* key; |
34 const char* value; | 35 const char* value; |
35 } headers[] = {{"cache-control", "no-cache"}, | 36 } headers[] = {{"cache-control", "no-cache"}, |
36 {"set-cookie", "foop"}, | 37 {"set-cookie", "foop"}, |
37 {"foo", "bar"}, | 38 {"foo", "bar"}, |
38 {0, 0}}; | 39 {0, 0}}; |
39 | 40 Vector<WebURL> urlList; |
| 41 urlList.append(url); |
40 std::unique_ptr<WebServiceWorkerResponse> webResponse = | 42 std::unique_ptr<WebServiceWorkerResponse> webResponse = |
41 makeUnique<WebServiceWorkerResponse>(); | 43 makeUnique<WebServiceWorkerResponse>(); |
42 webResponse->setURL(url); | 44 webResponse->setURLList(urlList); |
43 webResponse->setStatus(status); | 45 webResponse->setStatus(status); |
44 webResponse->setStatusText(statusText); | 46 webResponse->setStatusText(statusText); |
45 webResponse->setResponseType(WebServiceWorkerResponseTypeDefault); | 47 webResponse->setResponseType(WebServiceWorkerResponseTypeDefault); |
46 for (int i = 0; headers[i].key; ++i) | 48 for (int i = 0; headers[i].key; ++i) |
47 webResponse->setHeader(WebString::fromUTF8(headers[i].key), | 49 webResponse->setHeader(WebString::fromUTF8(headers[i].key), |
48 WebString::fromUTF8(headers[i].value)); | 50 WebString::fromUTF8(headers[i].value)); |
49 return webResponse; | 51 return webResponse; |
50 } | 52 } |
51 | 53 |
52 TEST(ServiceWorkerResponseTest, FromFetchResponseData) { | 54 TEST(ServiceWorkerResponseTest, FromFetchResponseData) { |
53 std::unique_ptr<DummyPageHolder> page = | 55 std::unique_ptr<DummyPageHolder> page = |
54 DummyPageHolder::create(IntSize(1, 1)); | 56 DummyPageHolder::create(IntSize(1, 1)); |
55 const KURL url(ParsedURLString, "http://www.response.com"); | 57 const KURL url(ParsedURLString, "http://www.response.com"); |
56 | 58 |
57 FetchResponseData* fetchResponseData = FetchResponseData::create(); | 59 FetchResponseData* fetchResponseData = FetchResponseData::create(); |
58 fetchResponseData->setURL(url); | 60 Vector<KURL> urlList; |
59 | 61 urlList.append(url); |
| 62 fetchResponseData->setURLList(urlList); |
60 Response* response = Response::create(&page->document(), fetchResponseData); | 63 Response* response = Response::create(&page->document(), fetchResponseData); |
61 ASSERT(response); | 64 ASSERT(response); |
62 EXPECT_EQ(url, response->url()); | 65 EXPECT_EQ(url, response->url()); |
63 } | 66 } |
64 | 67 |
65 TEST(ServiceWorkerResponseTest, FromWebServiceWorkerResponse) { | 68 TEST(ServiceWorkerResponseTest, FromWebServiceWorkerResponse) { |
66 V8TestingScope scope; | 69 V8TestingScope scope; |
67 std::unique_ptr<WebServiceWorkerResponse> webResponse = | 70 std::unique_ptr<WebServiceWorkerResponse> webResponse = |
68 createTestWebServiceWorkerResponse(); | 71 createTestWebServiceWorkerResponse(); |
69 Response* response = Response::create(scope.getScriptState(), *webResponse); | 72 Response* response = Response::create(scope.getScriptState(), *webResponse); |
70 ASSERT(response); | 73 ASSERT(response); |
71 EXPECT_EQ(webResponse->url(), response->url()); | 74 ASSERT_EQ(1u, webResponse->urlList().size()); |
| 75 EXPECT_EQ(webResponse->urlList()[0], response->url()); |
72 EXPECT_EQ(webResponse->status(), response->status()); | 76 EXPECT_EQ(webResponse->status(), response->status()); |
73 EXPECT_STREQ(webResponse->statusText().utf8().c_str(), | 77 EXPECT_STREQ(webResponse->statusText().utf8().c_str(), |
74 response->statusText().utf8().data()); | 78 response->statusText().utf8().data()); |
75 | 79 |
76 Headers* responseHeaders = response->headers(); | 80 Headers* responseHeaders = response->headers(); |
77 | 81 |
78 WebVector<WebString> keys = webResponse->getHeaderKeys(); | 82 WebVector<WebString> keys = webResponse->getHeaderKeys(); |
79 EXPECT_EQ(keys.size(), responseHeaders->headerList()->size()); | 83 EXPECT_EQ(keys.size(), responseHeaders->headerList()->size()); |
80 for (size_t i = 0, max = keys.size(); i < max; ++i) { | 84 for (size_t i = 0, max = keys.size(); i < max; ++i) { |
81 WebString key = keys[i]; | 85 WebString key = keys[i]; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 src->add(Command(Command::Data, "world")); | 220 src->add(Command(Command::Data, "world")); |
217 src->add(Command(Command::Done)); | 221 src->add(Command(Command::Done)); |
218 return new BodyStreamBuffer(scriptState, src); | 222 return new BodyStreamBuffer(scriptState, src); |
219 } | 223 } |
220 | 224 |
221 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneDefault) { | 225 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneDefault) { |
222 V8TestingScope scope; | 226 V8TestingScope scope; |
223 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); | 227 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); |
224 FetchResponseData* fetchResponseData = | 228 FetchResponseData* fetchResponseData = |
225 FetchResponseData::createWithBuffer(buffer); | 229 FetchResponseData::createWithBuffer(buffer); |
226 fetchResponseData->setURL(KURL(ParsedURLString, "http://www.response.com")); | 230 Vector<KURL> urlList; |
| 231 urlList.append(KURL(ParsedURLString, "http://www.response.com")); |
| 232 fetchResponseData->setURLList(urlList); |
227 Response* response = | 233 Response* response = |
228 Response::create(scope.getExecutionContext(), fetchResponseData); | 234 Response::create(scope.getExecutionContext(), fetchResponseData); |
229 EXPECT_EQ(response->internalBodyBuffer(), buffer); | 235 EXPECT_EQ(response->internalBodyBuffer(), buffer); |
230 checkResponseStream(scope.getScriptState(), response, true); | 236 checkResponseStream(scope.getScriptState(), response, true); |
231 } | 237 } |
232 | 238 |
233 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneBasic) { | 239 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneBasic) { |
234 V8TestingScope scope; | 240 V8TestingScope scope; |
235 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); | 241 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); |
236 FetchResponseData* fetchResponseData = | 242 FetchResponseData* fetchResponseData = |
237 FetchResponseData::createWithBuffer(buffer); | 243 FetchResponseData::createWithBuffer(buffer); |
238 fetchResponseData->setURL(KURL(ParsedURLString, "http://www.response.com")); | 244 Vector<KURL> urlList; |
| 245 urlList.append(KURL(ParsedURLString, "http://www.response.com")); |
| 246 fetchResponseData->setURLList(urlList); |
239 fetchResponseData = fetchResponseData->createBasicFilteredResponse(); | 247 fetchResponseData = fetchResponseData->createBasicFilteredResponse(); |
240 Response* response = | 248 Response* response = |
241 Response::create(scope.getExecutionContext(), fetchResponseData); | 249 Response::create(scope.getExecutionContext(), fetchResponseData); |
242 EXPECT_EQ(response->internalBodyBuffer(), buffer); | 250 EXPECT_EQ(response->internalBodyBuffer(), buffer); |
243 checkResponseStream(scope.getScriptState(), response, true); | 251 checkResponseStream(scope.getScriptState(), response, true); |
244 } | 252 } |
245 | 253 |
246 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneCORS) { | 254 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneCORS) { |
247 V8TestingScope scope; | 255 V8TestingScope scope; |
248 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); | 256 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); |
249 FetchResponseData* fetchResponseData = | 257 FetchResponseData* fetchResponseData = |
250 FetchResponseData::createWithBuffer(buffer); | 258 FetchResponseData::createWithBuffer(buffer); |
251 fetchResponseData->setURL(KURL(ParsedURLString, "http://www.response.com")); | 259 Vector<KURL> urlList; |
| 260 urlList.append(KURL(ParsedURLString, "http://www.response.com")); |
| 261 fetchResponseData->setURLList(urlList); |
252 fetchResponseData = fetchResponseData->createCORSFilteredResponse(); | 262 fetchResponseData = fetchResponseData->createCORSFilteredResponse(); |
253 Response* response = | 263 Response* response = |
254 Response::create(scope.getExecutionContext(), fetchResponseData); | 264 Response::create(scope.getExecutionContext(), fetchResponseData); |
255 EXPECT_EQ(response->internalBodyBuffer(), buffer); | 265 EXPECT_EQ(response->internalBodyBuffer(), buffer); |
256 checkResponseStream(scope.getScriptState(), response, true); | 266 checkResponseStream(scope.getScriptState(), response, true); |
257 } | 267 } |
258 | 268 |
259 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneOpaque) { | 269 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneOpaque) { |
260 V8TestingScope scope; | 270 V8TestingScope scope; |
261 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); | 271 BodyStreamBuffer* buffer = createHelloWorldBuffer(scope.getScriptState()); |
262 FetchResponseData* fetchResponseData = | 272 FetchResponseData* fetchResponseData = |
263 FetchResponseData::createWithBuffer(buffer); | 273 FetchResponseData::createWithBuffer(buffer); |
264 fetchResponseData->setURL(KURL(ParsedURLString, "http://www.response.com")); | 274 Vector<KURL> urlList; |
| 275 urlList.append(KURL(ParsedURLString, "http://www.response.com")); |
| 276 fetchResponseData->setURLList(urlList); |
265 fetchResponseData = fetchResponseData->createOpaqueFilteredResponse(); | 277 fetchResponseData = fetchResponseData->createOpaqueFilteredResponse(); |
266 Response* response = | 278 Response* response = |
267 Response::create(scope.getExecutionContext(), fetchResponseData); | 279 Response::create(scope.getExecutionContext(), fetchResponseData); |
268 EXPECT_EQ(response->internalBodyBuffer(), buffer); | 280 EXPECT_EQ(response->internalBodyBuffer(), buffer); |
269 checkResponseStream(scope.getScriptState(), response, false); | 281 checkResponseStream(scope.getScriptState(), response, false); |
270 } | 282 } |
271 | 283 |
272 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneError) { | 284 TEST(ServiceWorkerResponseTest, BodyStreamBufferCloneError) { |
273 V8TestingScope scope; | 285 V8TestingScope scope; |
274 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 286 BodyStreamBuffer* buffer = new BodyStreamBuffer( |
275 scope.getScriptState(), | 287 scope.getScriptState(), |
276 BytesConsumer::createErrored(BytesConsumer::Error())); | 288 BytesConsumer::createErrored(BytesConsumer::Error())); |
277 FetchResponseData* fetchResponseData = | 289 FetchResponseData* fetchResponseData = |
278 FetchResponseData::createWithBuffer(buffer); | 290 FetchResponseData::createWithBuffer(buffer); |
279 fetchResponseData->setURL(KURL(ParsedURLString, "http://www.response.com")); | 291 Vector<KURL> urlList; |
| 292 urlList.append(KURL(ParsedURLString, "http://www.response.com")); |
| 293 fetchResponseData->setURLList(urlList); |
280 Response* response = | 294 Response* response = |
281 Response::create(scope.getExecutionContext(), fetchResponseData); | 295 Response::create(scope.getExecutionContext(), fetchResponseData); |
282 DummyExceptionStateForTesting exceptionState; | 296 DummyExceptionStateForTesting exceptionState; |
283 Response* clonedResponse = | 297 Response* clonedResponse = |
284 response->clone(scope.getScriptState(), exceptionState); | 298 response->clone(scope.getScriptState(), exceptionState); |
285 EXPECT_FALSE(exceptionState.hadException()); | 299 EXPECT_FALSE(exceptionState.hadException()); |
286 | 300 |
287 BytesConsumerTestUtil::MockFetchDataLoaderClient* client1 = | 301 BytesConsumerTestUtil::MockFetchDataLoaderClient* client1 = |
288 new BytesConsumerTestUtil::MockFetchDataLoaderClient(); | 302 new BytesConsumerTestUtil::MockFetchDataLoaderClient(); |
289 BytesConsumerTestUtil::MockFetchDataLoaderClient* client2 = | 303 BytesConsumerTestUtil::MockFetchDataLoaderClient* client2 = |
290 new BytesConsumerTestUtil::MockFetchDataLoaderClient(); | 304 new BytesConsumerTestUtil::MockFetchDataLoaderClient(); |
291 EXPECT_CALL(*client1, didFetchDataLoadFailed()); | 305 EXPECT_CALL(*client1, didFetchDataLoadFailed()); |
292 EXPECT_CALL(*client2, didFetchDataLoadFailed()); | 306 EXPECT_CALL(*client2, didFetchDataLoadFailed()); |
293 | 307 |
294 response->internalBodyBuffer()->startLoading( | 308 response->internalBodyBuffer()->startLoading( |
295 FetchDataLoader::createLoaderAsString(), client1); | 309 FetchDataLoader::createLoaderAsString(), client1); |
296 clonedResponse->internalBodyBuffer()->startLoading( | 310 clonedResponse->internalBodyBuffer()->startLoading( |
297 FetchDataLoader::createLoaderAsString(), client2); | 311 FetchDataLoader::createLoaderAsString(), client2); |
298 blink::testing::runPendingTasks(); | 312 blink::testing::runPendingTasks(); |
299 } | 313 } |
300 | 314 |
301 } // namespace | 315 } // namespace |
302 } // namespace blink | 316 } // namespace blink |
OLD | NEW |