[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 5 | #include "content/browser/browser_url_handler_impl.h" |
[email protected] | 18bdd3dd | 2012-06-04 02:31:27 | [diff] [blame] | 6 | #include "content/public/test/test_browser_context.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 8 | #include "url/gurl.h" |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 9 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 10 | namespace content { |
| 11 | |
[email protected] | d9c2e51 | 2012-10-25 18:54:36 | [diff] [blame] | 12 | class BrowserURLHandlerImplTest : public testing::Test { |
| 13 | }; |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 14 | |
| 15 | // Test URL rewriter that rewrites all "foo://" URLs to "bar://bar". |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 16 | static bool FooRewriter(GURL* url, BrowserContext* browser_context) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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". |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 25 | static bool BarRewriter(GURL* url, BrowserContext* browser_context) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 26 | if (url->scheme() == "bar") { |
| 27 | *url = GURL("foo://foo"); |
| 28 | return true; |
| 29 | } |
| 30 | return false; |
| 31 | } |
| 32 | |
[email protected] | d9c2e51 | 2012-10-25 18:54:36 | [diff] [blame] | 33 | TEST_F(BrowserURLHandlerImplTest, BasicRewriteAndReverse) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 34 | TestBrowserContext browser_context; |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 35 | BrowserURLHandlerImpl handler; |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 36 | |
| 37 | handler.AddHandlerPair(FooRewriter, BarRewriter); |
| 38 | |
| 39 | GURL url("foo://bar"); |
| 40 | GURL original_url(url); |
| 41 | bool reverse_on_redirect = false; |
[email protected] | 266c4b4 | 2011-08-23 16:48:55 | [diff] [blame] | 42 | handler.RewriteURLIfNecessary(&url, &browser_context, &reverse_on_redirect); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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); |
[email protected] | 266c4b4 | 2011-08-23 16:48:55 | [diff] [blame] | 48 | bool reversed = handler.ReverseURLRewrite(&url, |
| 49 | original_url, |
| 50 | &browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 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. |
[email protected] | 266c4b4 | 2011-08-23 16:48:55 | [diff] [blame] | 57 | reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 58 | ASSERT_FALSE(reversed); |
| 59 | ASSERT_EQ(saved_url, url); |
| 60 | } |
| 61 | |
[email protected] | d9c2e51 | 2012-10-25 18:54:36 | [diff] [blame] | 62 | TEST_F(BrowserURLHandlerImplTest, NullHandlerReverse) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 63 | TestBrowserContext browser_context; |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 64 | BrowserURLHandlerImpl handler; |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 65 | |
| 66 | GURL url("bar://foo"); |
| 67 | GURL original_url(url); |
| 68 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 69 | handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), FooRewriter); |
[email protected] | 266c4b4 | 2011-08-23 16:48:55 | [diff] [blame] | 70 | bool reversed = handler.ReverseURLRewrite(&url, |
| 71 | original_url, |
| 72 | &browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 73 | ASSERT_FALSE(reversed); |
| 74 | ASSERT_EQ(original_url, url); |
| 75 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 76 | handler.AddHandlerPair(BrowserURLHandlerImpl::null_handler(), BarRewriter); |
[email protected] | 266c4b4 | 2011-08-23 16:48:55 | [diff] [blame] | 77 | reversed = handler.ReverseURLRewrite(&url, original_url, &browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 78 | ASSERT_TRUE(reversed); |
| 79 | ASSERT_EQ("foo://foo", url.spec()); |
| 80 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 81 | |
sgurun | d8e1571c | 2014-10-20 16:59:22 | [diff] [blame] | 82 | // Verify that the reverse handler for view-source does not duplicate query |
| 83 | // parameters. |
| 84 | TEST_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] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 97 | } // namespace content |