Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1039)

Unified Diff: Source/modules/serviceworkers/RequestTest.cpp

Issue 516123004: Support ServiceWorker created request objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@minimaster
Patch Set: fix more builds Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/serviceworkers/Request.cpp ('k') | Source/platform/exported/WebServiceWorkerRequest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/serviceworkers/RequestTest.cpp
diff --git a/Source/modules/serviceworkers/RequestTest.cpp b/Source/modules/serviceworkers/RequestTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..74092cf7d3f8b773111a8083e7560fefc28ad997
--- /dev/null
+++ b/Source/modules/serviceworkers/RequestTest.cpp
@@ -0,0 +1,101 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+
+#include "bindings/core/v8/ExceptionState.h"
+#include "bindings/core/v8/ScriptState.h"
+#include "core/dom/Document.h"
+#include "core/frame/Frame.h"
+#include "core/testing/DummyPageHolder.h"
+#include "modules/serviceworkers/Request.h"
+#include "public/platform/WebServiceWorkerRequest.h"
+#include "wtf/HashMap.h"
+#include "wtf/text/WTFString.h"
+#include <gtest/gtest.h>
+
+namespace blink {
+namespace {
+
+class ServiceWorkerRequestTest : public ::testing::Test {
+public:
+ ServiceWorkerRequestTest() : m_page(DummyPageHolder::create(IntSize(1, 1))) { }
+
+ ScriptState* scriptState() { return ScriptState::forMainWorld(m_page->document().frame()); }
+ ExecutionContext* executionContext() { return scriptState()->executionContext(); }
+
+private:
+ OwnPtr<DummyPageHolder> m_page;
+};
+
+TEST_F(ServiceWorkerRequestTest, FromString)
+{
+ TrackExceptionState exceptionState;
+
+ KURL url(ParsedURLString, "http://www.example.com/");
+ RefPtrWillBeRawPtr<Request> request = Request::create(executionContext(), url, exceptionState);
+ ASSERT_FALSE(exceptionState.hadException());
+ ASSERT(request);
+ EXPECT_EQ(url, request->url());
+}
+
+TEST_F(ServiceWorkerRequestTest, FromRequest)
+{
+ TrackExceptionState exceptionState;
+
+ KURL url(ParsedURLString, "http://www.example.com/");
+ RefPtrWillBeRawPtr<Request> request1 = Request::create(executionContext(), url, exceptionState);
+ ASSERT(request1);
+
+ RefPtrWillBeRawPtr<Request> request2 = Request::create(executionContext(), request1.get(), exceptionState);
+ ASSERT_FALSE(exceptionState.hadException());
+ ASSERT(request2);
+ EXPECT_EQ(url, request2->url());
+}
+
+TEST_F(ServiceWorkerRequestTest, FromAndToWebRequest)
+{
+ WebServiceWorkerRequest webRequest;
+
+ const KURL url(ParsedURLString, "http://www.example.com/");
+ const String method = "GET";
+ struct { const char* key; const char* value; } headers[] = { {"X-Foo", "bar"}, {"X-Quux", "foop"}, {0, 0} };
+ const String referrer = "http://www.referrer.com/";
+ const WebReferrerPolicy referrerPolicy = WebReferrerPolicyAlways;
+
+ webRequest.setURL(url);
+ webRequest.setMethod(method);
+ for (int i = 0; headers[i].key; ++i)
+ webRequest.setHeader(WebString::fromUTF8(headers[i].key), WebString::fromUTF8(headers[i].value));
+ webRequest.setReferrer(referrer, referrerPolicy);
+
+ RefPtrWillBeRawPtr<Request> request = Request::create(webRequest);
+ ASSERT(request);
+ EXPECT_EQ(url, request->url());
+ EXPECT_EQ(method, request->method());
+ EXPECT_EQ(referrer, request->referrer());
+
+ RefPtrWillBeRawPtr<Headers> requestHeaders = request->headers();
+
+ WTF::HashMap<String, String> headersMap;
+ for (int i = 0; headers[i].key; ++i)
+ headersMap.add(headers[i].key, headers[i].value);
+ EXPECT_EQ(headersMap.size(), requestHeaders->size());
+ for (WTF::HashMap<String, String>::iterator iter = headersMap.begin(); iter != headersMap.end(); ++iter) {
+ TrackExceptionState exceptionState;
+ EXPECT_EQ(iter->value, requestHeaders->get(iter->key, exceptionState));
+ EXPECT_FALSE(exceptionState.hadException());
+ }
+
+ WebServiceWorkerRequest secondWebRequest;
+ request->populateWebServiceWorkerRequest(secondWebRequest);
+ EXPECT_EQ(url, KURL(secondWebRequest.url()));
+ EXPECT_EQ(method, String(secondWebRequest.method()));
+ EXPECT_EQ(referrer, KURL(secondWebRequest.referrerUrl()));
+ EXPECT_EQ(referrerPolicy, secondWebRequest.referrerPolicy());
+ EXPECT_EQ(webRequest.headers(), secondWebRequest.headers());
+}
+
+} // namespace
+} // namespace blink
« no previous file with comments | « Source/modules/serviceworkers/Request.cpp ('k') | Source/platform/exported/WebServiceWorkerRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698