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. 4 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" 8 #include "url/gurl.h" 9 10 namespace content { 11 12 class BrowserURLHandlerImplTest : public testing::Test { 13 }; 14 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"); 19 return true; 20 } 21 return false; 22 } 23 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"); 28 return true; 29 } 30 return false; 31 } 32 33 TEST_F(BrowserURLHandlerImplTest, BasicRewriteAndReverse) { 34 TestBrowserContext browser_context; 35 BrowserURLHandlerImpl handler; 36 37 handler.AddHandlerPair(FooRewriter, BarRewriter); 38 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()); 45 46 // Check that reversing the URL works. 47 GURL saved_url(url); 48 bool reversed = handler.ReverseURLRewrite(&url, 49 original_url, 50 &browser_context); 51 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. 57 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); 58 ASSERT_FALSE(reversed); 59 ASSERT_EQ(saved_url, url); 60 } 61 62 TEST_F(BrowserURLHandlerImplTest, NullHandlerReverse) { 63 TestBrowserContext browser_context; 64 BrowserURLHandlerImpl handler; 65 66 GURL url("bar://foo"); 67 GURL original_url(url); 68 69 handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter); 70 bool reversed = handler.ReverseURLRewrite(&url, 71 original_url, 72 &browser_context); 73 ASSERT_FALSE(reversed); 74 ASSERT_EQ(original_url, url); 75 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()); 80 } 81 82 } // namespace content 83