Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / dom_distiller / core / url_utils_unittest.cc
blob92059fd36a87232682964befe2cdf589ade854ff
1 // Copyright 2014 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 "components/dom_distiller/core/url_utils.h"
7 #include "components/dom_distiller/core/url_constants.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 namespace dom_distiller {
13 namespace url_utils {
15 TEST(DomDistillerUrlUtilsTest, TestPathUtil) {
16 const std::string single_key = "mypath?foo=bar";
17 EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(single_key, "foo"));
18 const std::string two_keys = "mypath?key1=foo&key2=bar";
19 EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(two_keys, "key1"));
20 EXPECT_EQ("bar", GetValueForKeyInUrlPathQuery(two_keys, "key2"));
21 const std::string multiple_same_key = "mypath?key=foo&key=bar";
22 EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(multiple_same_key, "key"));
25 TEST(DomDistillerUrlUtilsTest, TestGetValueForKeyInUrlPathQuery) {
26 // Tests an invalid url.
27 const std::string invalid_url = "http://%40[::1]/";
28 EXPECT_EQ("", GetValueForKeyInUrlPathQuery(invalid_url, "key"));
30 // Test a valid URL with the key we are searching for.
31 const std::string valid_url_with_key = "http://www.google.com?key=foo";
32 EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_with_key, "key"));
34 // Test a valid URL without the key we are searching for.
35 const std::string valid_url_no_key = "http://www.google.com";
36 EXPECT_EQ("", GetValueForKeyInUrlPathQuery(valid_url_no_key, "key"));
38 // Test a valid URL with 2 values of the key we are searching for.
39 const std::string valid_url_two_keys =
40 "http://www.google.com?key=foo&key=bar";
41 EXPECT_EQ("foo", GetValueForKeyInUrlPathQuery(valid_url_two_keys, "key"));
44 std::string ThroughDistiller(std::string url) {
45 return GetOriginalUrlFromDistillerUrl(
46 GetDistillerViewUrlFromUrl(kDomDistillerScheme, GURL(url))).spec();
49 std::string GetOriginalUrlFromDistillerUrl(std::string url) {
50 return GetOriginalUrlFromDistillerUrl(GURL(url)).spec();
53 TEST(DomDistillerUrlUtilsTest, TestDistillerEndToEnd) {
54 // Tests a normal url.
55 const std::string url = "http://example.com/";
56 EXPECT_EQ(url, ThroughDistiller(url));
57 EXPECT_EQ(url, GetOriginalUrlFromDistillerUrl(url));
59 // Tests a url with arguments and anchor points.
60 const std::string url_arguments =
61 "https://example.com/?key=value&key=value2&key2=value3#here";
62 EXPECT_EQ(url_arguments, ThroughDistiller(url_arguments));
63 EXPECT_EQ(url_arguments, GetOriginalUrlFromDistillerUrl(url_arguments));
65 // Tests a url with file:// scheme.
66 const std::string url_file = "file:///home/userid/path/index.html";
67 EXPECT_EQ(url_file, ThroughDistiller(url_file));
68 EXPECT_EQ(url_file, GetOriginalUrlFromDistillerUrl(url_file));
70 } // namespace url_utils
72 } // namespace dom_distiller