1 /* 2 * Copyright (C) 2014 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 33 #include "core/dom/Document.h" 34 #include "core/frame/LocalFrame.h" 35 #include "core/frame/Location.h" 36 #include "core/page/Page.h" 37 #include "core/testing/URLTestHelpers.h" 38 #include "platform/weborigin/KURL.h" 39 #include "public/platform/Platform.h" 40 #include "public/platform/WebString.h" 41 #include "public/platform/WebURL.h" 42 #include "public/platform/WebURLRequest.h" 43 #include "public/platform/WebURLResponse.h" 44 #include "public/platform/WebUnitTestSupport.h" 45 #include "public/web/WebDocument.h" 46 #include "public/web/WebFrame.h" 47 #include "public/web/WebView.h" 48 #include "web/tests/FrameTestHelpers.h" 49 #include <gtest/gtest.h> 50 51 namespace { 52 53 using blink::URLTestHelpers::toKURL; 54 using namespace blink; 55 56 class MHTMLTest : public testing::Test { 57 public: 58 MHTMLTest() 59 { 60 } 61 62 protected: 63 virtual void SetUp() 64 { 65 m_helper.initialize(); 66 } 67 68 virtual void TearDown() 69 { 70 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 71 } 72 73 void registerMockedURLLoad(const std::string& url, const WebString& fileName) 74 { 75 URLTestHelpers::registerMockedURLLoad(toKURL(url), fileName, WebString::fromUTF8("mhtml/"), WebString::fromUTF8("text/html")); 76 } 77 78 void loadURLInTopFrame(const WebURL& url) 79 { 80 FrameTestHelpers::loadFrame(m_helper.webView()->mainFrame(), url.string().utf8().data()); 81 } 82 83 Page* page() const { return m_helper.webViewImpl()->page(); } 84 85 private: 86 FrameTestHelpers::WebViewHelper m_helper; 87 }; 88 89 // Checks that the domain is set to the actual MHTML file, not the URL it was 90 // generated from. 91 TEST_F(MHTMLTest, CheckDomain) 92 { 93 const char kFileURL[] = "file:///simple_test.mht"; 94 95 // Register the mocked frame and load it. 96 WebURL url = toKURL(kFileURL); 97 registerMockedURLLoad(kFileURL, WebString::fromUTF8("simple_test.mht")); 98 loadURLInTopFrame(url); 99 ASSERT_TRUE(page()); 100 LocalFrame* frame = toLocalFrame(page()->mainFrame()); 101 ASSERT_TRUE(frame); 102 Document* document = frame->document(); 103 ASSERT_TRUE(document); 104 105 EXPECT_STREQ(kFileURL, frame->domWindow()->location().href().ascii().data()); 106 107 SecurityOrigin* origin = document->securityOrigin(); 108 EXPECT_STRNE("localhost", origin->domain().ascii().data()); 109 } 110 111 } 112