1 // 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.
5 #include "content/browser/browser_url_handler_impl.h"
6 #include "content/public/test/test_browser_context.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 class BrowserURLHandlerImplTest
: public testing::Test
{
15 // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar".
16 static bool FooRewriter(GURL
* url
, BrowserContext
* browser_context
) {
17 if (url
->scheme() == "foo") {
18 *url
= GURL("bar://bar");
24 // Test URL rewriter that rewrites all "bar://" URLs to "foo://foo".
25 static bool BarRewriter(GURL
* url
, BrowserContext
* browser_context
) {
26 if (url
->scheme() == "bar") {
27 *url
= GURL("foo://foo");
33 TEST_F(BrowserURLHandlerImplTest
, BasicRewriteAndReverse
) {
34 TestBrowserContext browser_context
;
35 BrowserURLHandlerImpl handler
;
37 handler
.AddHandlerPair(FooRewriter
, BarRewriter
);
39 GURL
url("foo://bar");
40 GURL
original_url(url
);
41 bool reverse_on_redirect
= false;
42 handler
.RewriteURLIfNecessary(&url
, &browser_context
, &reverse_on_redirect
);
43 ASSERT_TRUE(reverse_on_redirect
);
44 ASSERT_EQ("bar://bar", url
.spec());
46 // Check that reversing the URL works.
48 bool reversed
= handler
.ReverseURLRewrite(&url
,
51 ASSERT_TRUE(reversed
);
52 ASSERT_EQ("foo://foo", url
.spec());
54 // Check that reversing the URL only works with a matching |original_url|.
56 original_url
= GURL("bam://bam"); // Won't be matched by FooRewriter.
57 reversed
= handler
.ReverseURLRewrite(&url
, original_url
, &browser_context
);
58 ASSERT_FALSE(reversed
);
59 ASSERT_EQ(saved_url
, url
);
62 TEST_F(BrowserURLHandlerImplTest
, NullHandlerReverse
) {
63 TestBrowserContext browser_context
;
64 BrowserURLHandlerImpl handler
;
66 GURL
url("bar://foo");
67 GURL
original_url(url
);
69 handler
.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter
);
70 bool reversed
= handler
.ReverseURLRewrite(&url
,
73 ASSERT_FALSE(reversed
);
74 ASSERT_EQ(original_url
, url
);
76 handler
.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), BarRewriter
);
77 reversed
= handler
.ReverseURLRewrite(&url
, original_url
, &browser_context
);
78 ASSERT_TRUE(reversed
);
79 ASSERT_EQ("foo://foo", url
.spec());
82 // Verify that the reverse handler for view-source does not duplicate query
84 TEST_F(BrowserURLHandlerImplTest
, ViewSourceReverse
) {
85 TestBrowserContext browser_context
;
86 BrowserURLHandlerImpl handler
;
88 GURL
url("http://foo/?a=1");
89 GURL
original_url("view-source:http://some_url");
90 bool reversed
= handler
.ReverseURLRewrite(&url
,
93 ASSERT_TRUE(reversed
);
94 ASSERT_EQ("view-source:http://foo/?a=1", url
.spec());
97 } // namespace content