blob: daceffcee4720da29ec6d5704f531cda5829916c [file] [log] [blame]
[email protected]f1eb87a2011-05-06 17:49:411// 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
3// found in the LICENSE file.
4
[email protected]825b1662012-03-12 19:07:315#include "content/browser/browser_url_handler_impl.h"
[email protected]18bdd3dd2012-06-04 02:31:276#include "content/public/test/test_browser_context.h"
[email protected]f1eb87a2011-05-06 17:49:417#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:588#include "url/gurl.h"
[email protected]f1eb87a2011-05-06 17:49:419
[email protected]46488322012-10-30 03:22:2010namespace content {
11
[email protected]d9c2e512012-10-25 18:54:3612class BrowserURLHandlerImplTest : public testing::Test {
13};
[email protected]f1eb87a2011-05-06 17:49:4114
15// Test URL rewriter that rewrites all "foo://" URLs to "bar://bar".
[email protected]46488322012-10-30 03:22:2016static bool FooRewriter(GURL* url, BrowserContext* browser_context) {
[email protected]f1eb87a2011-05-06 17:49:4117 if (url->scheme() == "foo") {
18 *url = GURL("bar://bar");
19 return true;
20 }
21 return false;
22}
23
24// Test URL rewriter that rewrites all "bar://" URLs to "foo://foo".
[email protected]46488322012-10-30 03:22:2025static bool BarRewriter(GURL* url, BrowserContext* browser_context) {
[email protected]f1eb87a2011-05-06 17:49:4126 if (url->scheme() == "bar") {
27 *url = GURL("foo://foo");
28 return true;
29 }
30 return false;
31}
32
[email protected]d9c2e512012-10-25 18:54:3633TEST_F(BrowserURLHandlerImplTest, BasicRewriteAndReverse) {
[email protected]46488322012-10-30 03:22:2034 TestBrowserContext browser_context;
[email protected]825b1662012-03-12 19:07:3135 BrowserURLHandlerImpl handler;
[email protected]f1eb87a2011-05-06 17:49:4136
37 handler.AddHandlerPair(FooRewriter, BarRewriter);
38
39 GURL url("foo://bar");
40 GURL original_url(url);
41 bool reverse_on_redirect = false;
[email protected]266c4b42011-08-23 16:48:5542 handler.RewriteURLIfNecessary(&url, &browser_context, &reverse_on_redirect);
[email protected]f1eb87a2011-05-06 17:49:4143 ASSERT_TRUE(reverse_on_redirect);
44 ASSERT_EQ("bar://bar", url.spec());
45
46 // Check that reversing the URL works.
47 GURL saved_url(url);
[email protected]266c4b42011-08-23 16:48:5548 bool reversed = handler.ReverseURLRewrite(&url,
49 original_url,
50 &browser_context);
[email protected]f1eb87a2011-05-06 17:49:4151 ASSERT_TRUE(reversed);
52 ASSERT_EQ("foo://foo", url.spec());
53
54 // Check that reversing the URL only works with a matching |original_url|.
55 url = saved_url;
56 original_url = GURL("bam://bam"); // Won't be matched by FooRewriter.
[email protected]266c4b42011-08-23 16:48:5557 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context);
[email protected]f1eb87a2011-05-06 17:49:4158 ASSERT_FALSE(reversed);
59 ASSERT_EQ(saved_url, url);
60}
61
[email protected]d9c2e512012-10-25 18:54:3662TEST_F(BrowserURLHandlerImplTest, NullHandlerReverse) {
[email protected]46488322012-10-30 03:22:2063 TestBrowserContext browser_context;
[email protected]825b1662012-03-12 19:07:3164 BrowserURLHandlerImpl handler;
[email protected]f1eb87a2011-05-06 17:49:4165
66 GURL url("bar://foo");
67 GURL original_url(url);
68
[email protected]825b1662012-03-12 19:07:3169 handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter);
[email protected]266c4b42011-08-23 16:48:5570 bool reversed = handler.ReverseURLRewrite(&url,
71 original_url,
72 &browser_context);
[email protected]f1eb87a2011-05-06 17:49:4173 ASSERT_FALSE(reversed);
74 ASSERT_EQ(original_url, url);
75
[email protected]825b1662012-03-12 19:07:3176 handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), BarRewriter);
[email protected]266c4b42011-08-23 16:48:5577 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context);
[email protected]f1eb87a2011-05-06 17:49:4178 ASSERT_TRUE(reversed);
79 ASSERT_EQ("foo://foo", url.spec());
80}
[email protected]46488322012-10-30 03:22:2081
sgurund8e1571c2014-10-20 16:59:2282// Verify that the reverse handler for view-source does not duplicate query
83// parameters.
84TEST_F(BrowserURLHandlerImplTest, ViewSourceReverse) {
85 TestBrowserContext browser_context;
86 BrowserURLHandlerImpl handler;
87
88 GURL url("http://foo/?a=1");
89 GURL original_url("view-source:http://some_url");
90 bool reversed = handler.ReverseURLRewrite(&url,
91 original_url,
92 &browser_context);
93 ASSERT_TRUE(reversed);
94 ASSERT_EQ("view-source:http://foo/?a=1", url.spec());
95}
96
[email protected]46488322012-10-30 03:22:2097} // namespace content