Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ExceptionState.h" | |
| 8 #include "core/testing/DummyPageHolder.h" | |
| 9 #include "modules/serviceworkers/Request.h" | |
| 10 #include "public/platform/WebServiceWorkerRequest.h" | |
| 11 #include "wtf/HashMap.h" | |
| 12 #include "wtf/text/WTFString.h" | |
| 13 #include <gtest/gtest.h> | |
| 14 | |
| 15 namespace blink { | |
| 16 namespace { | |
| 17 | |
| 18 class ServiceWorkerRequestTest : public ::testing::Test { | |
| 19 public: | |
| 20 ServiceWorkerRequestTest() : m_page(DummyPageHolder::create(IntSize(1, 1))) { } | |
| 21 | |
| 22 ScriptState* scriptState() { return ScriptState::forMainWorld(m_page->docume nt().frame()); } | |
| 23 ExecutionContext* executionContext() { return scriptState()->executionContex t(); } | |
| 24 | |
| 25 private: | |
| 26 OwnPtr<DummyPageHolder> m_page; | |
| 27 }; | |
| 28 | |
| 29 TEST_F(ServiceWorkerRequestTest, FromString) | |
| 30 { | |
| 31 TrackExceptionState exceptionState; | |
| 32 | |
| 33 KURL url(ParsedURLString, "http://www.example.com/"); | |
| 34 RefPtrWillBeRawPtr<Request> request = Request::create(executionContext(), ur l, exceptionState); | |
| 35 ASSERT_FALSE(exceptionState.hadException()); | |
| 36 ASSERT(request); | |
| 37 EXPECT_EQ(url, request->url()); | |
| 38 } | |
| 39 | |
| 40 TEST_F(ServiceWorkerRequestTest, FromRequest) | |
| 41 { | |
| 42 TrackExceptionState exceptionState; | |
| 43 | |
| 44 KURL url(ParsedURLString, "http://www.example.com/"); | |
| 45 RefPtrWillBeRawPtr<Request> request1 = Request::create(executionContext(), u rl, exceptionState); | |
| 46 ASSERT(request1); | |
| 47 | |
| 48 RefPtrWillBeRawPtr<Request> request2 = Request::create(executionContext(), r equest1.get(), exceptionState); | |
| 49 ASSERT_FALSE(exceptionState.hadException()); | |
| 50 ASSERT(request2); | |
| 51 EXPECT_EQ(url, request2->url()); | |
| 52 } | |
| 53 | |
| 54 TEST_F(ServiceWorkerRequestTest, FromAndToWebRequest) | |
| 55 { | |
| 56 WebServiceWorkerRequest webRequest; | |
| 57 | |
| 58 const KURL url(ParsedURLString, "http://www.example.com/"); | |
| 59 const String method = "GET"; | |
| 60 struct { const char* key; const char* value; } headers[] = { {"X-Foo", "bar" }, {"X-Quux", "foop"}, {0, 0} }; | |
| 61 const String referrer = "http://www.referrer.com/"; | |
| 62 | |
| 63 webRequest.setURL(url); | |
| 64 webRequest.setMethod(method); | |
| 65 for (int i = 0; headers[i].key; ++i) | |
| 66 webRequest.setHeader(WebString::fromUTF8(headers[i].key), WebString::fro mUTF8(headers[i].value)); | |
| 67 webRequest.setReferrer(referrer, WebReferrerPolicyAlways); | |
| 68 | |
| 69 RefPtrWillBeRawPtr<Request> request = Request::create(webRequest); | |
| 70 ASSERT(request); | |
| 71 EXPECT_EQ(url, request->url()); | |
| 72 EXPECT_EQ(method, request->method()); | |
| 73 EXPECT_EQ(referrer, request->referrer()); | |
| 74 | |
| 75 RefPtrWillBeRawPtr<Headers> requestHeaders = request->headers(); | |
| 76 | |
| 77 WTF::HashMap<String, String> headersMap; | |
| 78 for (int i = 0; headers[i].key; ++i) | |
| 79 headersMap.add(headers[i].key, headers[i].value); | |
| 80 EXPECT_EQ(headersMap.size(), requestHeaders->size()); | |
| 81 for (WTF::HashMap<String, String>::iterator iter = headersMap.begin(); iter != headersMap.end(); ++iter) { | |
| 82 TrackExceptionState exceptionState; | |
| 83 EXPECT_EQ(iter->value, requestHeaders->get(iter->key, exceptionState)); | |
| 84 EXPECT_FALSE(exceptionState.hadException()); | |
| 85 } | |
| 86 | |
| 87 WebServiceWorkerRequest secondWebRequest; | |
| 88 request->populateWebServiceWorkerRequest(secondWebRequest); | |
| 89 EXPECT_EQ(url, KURL(secondWebRequest.url())); | |
| 90 EXPECT_EQ(method, String(secondWebRequest.method())); | |
| 91 EXPECT_EQ(referrer, KURL(secondWebRequest.referrerUrl()).string()); | |
|
jochen (gone - plz use gerrit)
2014/09/03 09:12:57
and test it here
gavinp
2014/09/03 11:45:59
Done.
| |
| 92 EXPECT_EQ(webRequest.headers(), secondWebRequest.headers()); | |
| 93 } | |
| 94 | |
| 95 } // namespace | |
| 96 } // namespace blink | |
| OLD | NEW |