| OLD | NEW |
| (Empty) |
| 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.h" | |
| 6 #include "content/test/test_browser_context.h" | |
| 7 #include "googleurl/src/gurl.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 class BrowserURLHandlerTest : public testing::Test { | |
| 11 }; | |
| 12 | |
| 13 // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar". | |
| 14 static bool FooRewriter(GURL* url, content::BrowserContext* browser_context) { | |
| 15 if (url->scheme() == "foo") { | |
| 16 *url = GURL("bar://bar"); | |
| 17 return true; | |
| 18 } | |
| 19 return false; | |
| 20 } | |
| 21 | |
| 22 // Test URL rewriter that rewrites all "bar://" URLs to "foo://foo". | |
| 23 static bool BarRewriter(GURL* url, content::BrowserContext* browser_context) { | |
| 24 if (url->scheme() == "bar") { | |
| 25 *url = GURL("foo://foo"); | |
| 26 return true; | |
| 27 } | |
| 28 return false; | |
| 29 } | |
| 30 | |
| 31 TEST_F(BrowserURLHandlerTest, BasicRewriteAndReverse) { | |
| 32 TestBrowserContext browser_context; | |
| 33 BrowserURLHandler handler; | |
| 34 | |
| 35 handler.AddHandlerPair(FooRewriter, BarRewriter); | |
| 36 | |
| 37 GURL url("foo://bar"); | |
| 38 GURL original_url(url); | |
| 39 bool reverse_on_redirect = false; | |
| 40 handler.RewriteURLIfNecessary(&url, &browser_context, &reverse_on_redirect); | |
| 41 ASSERT_TRUE(reverse_on_redirect); | |
| 42 ASSERT_EQ("bar://bar", url.spec()); | |
| 43 | |
| 44 // Check that reversing the URL works. | |
| 45 GURL saved_url(url); | |
| 46 bool reversed = handler.ReverseURLRewrite(&url, | |
| 47 original_url, | |
| 48 &browser_context); | |
| 49 ASSERT_TRUE(reversed); | |
| 50 ASSERT_EQ("foo://foo", url.spec()); | |
| 51 | |
| 52 // Check that reversing the URL only works with a matching |original_url|. | |
| 53 url = saved_url; | |
| 54 original_url = GURL("bam://bam"); // Won't be matched by FooRewriter. | |
| 55 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); | |
| 56 ASSERT_FALSE(reversed); | |
| 57 ASSERT_EQ(saved_url, url); | |
| 58 } | |
| 59 | |
| 60 TEST_F(BrowserURLHandlerTest, NullHandlerReverse) { | |
| 61 TestBrowserContext browser_context; | |
| 62 BrowserURLHandler handler; | |
| 63 | |
| 64 GURL url("bar://foo"); | |
| 65 GURL original_url(url); | |
| 66 | |
| 67 handler.AddHandlerPair(BrowserURLHandler::null_handler(), FooRewriter); | |
| 68 bool reversed = handler.ReverseURLRewrite(&url, | |
| 69 original_url, | |
| 70 &browser_context); | |
| 71 ASSERT_FALSE(reversed); | |
| 72 ASSERT_EQ(original_url, url); | |
| 73 | |
| 74 handler.AddHandlerPair(BrowserURLHandler::null_handler(), BarRewriter); | |
| 75 reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); | |
| 76 ASSERT_TRUE(reversed); | |
| 77 ASSERT_EQ("foo://foo", url.spec()); | |
| 78 } | |
| OLD | NEW |