summaryrefslogtreecommitdiffstats
path: root/ios/web/history_state_util_unittest.mm
blob: 5efbfd3fe014708041e65e051e74187229a11c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/history_state_util.h"

#include <stddef.h>

#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "url/gurl.h"

namespace web {
namespace {
struct TestEntry {
  std::string fromUrl;
  std::string toUrl;
  std::string expectedUrl;
};

class HistoryStateUtilTest : public ::testing::Test {
 protected:
  static const struct TestEntry tests_[];
};

const struct TestEntry HistoryStateUtilTest::tests_[] = {
    // Valid absolute changes.
    { "http://foo.com", "http://foo.com/bar", "http://foo.com/bar" },
    { "https://foo.com", "https://foo.com/bar", "https://foo.com/bar" },
    { "http://foo.com/", "http://foo.com#bar", "http://foo.com#bar" },
    { "http://foo.com:80", "http://foo.com:80/b",  "http://foo.com:80/b"},
    { "http://foo.com:888", "http://foo.com:888/b",  "http://foo.com:888/b"},
    // Valid relative changes.
    { "http://foo.com", "#bar", "http://foo.com#bar" },
    { "http://foo.com/", "#bar", "http://foo.com/#bar" },
    { "https://foo.com/", "bar", "https://foo.com/bar" },
    { "http://foo.com/foo/1", "/bar", "http://foo.com/bar" },
    { "http://foo.com/foo/1", "bar", "http://foo.com/foo/bar" },
    { "http://foo.com/", "bar.com", "http://foo.com/bar.com" },
    { "http://foo.com", "bar.com", "http://foo.com/bar.com" },
    { "http://foo.com:888", "bar.com", "http://foo.com:888/bar.com" },
    // Invalid scheme changes.
    { "http://foo.com", "https://foo.com#bar", "" },
    { "https://foo.com", "http://foo.com#bar", "" },
    // Invalid domain changes.
    { "http://foo.com/bar", "http://bar.com", "" },
    { "http://foo.com/bar", "http://www.foo.com/bar2", "" },
    // Valid port change.
    { "http://foo.com", "http://foo.com:80/bar", "http://foo.com/bar" },
    { "http://foo.com:80", "http://foo.com/bar", "http://foo.com/bar" },
    // Invalid port change.
    { "http://foo.com", "http://foo.com:42/bar", "" },
    { "http://foo.com:42", "http://foo.com/bar", "" },
    // Invalid URL.
    { "http://foo.com", "http://fo o.c om/ba r", "" },
    { "http://foo.com:80", "bar", "http://foo.com:80/bar" }
};

TEST_F(HistoryStateUtilTest, TestIsHistoryStateChangeValid) {
  for (size_t i = 0; i < arraysize(tests_); ++i) {
    GURL fromUrl(tests_[i].fromUrl);
    GURL toUrl = history_state_util::GetHistoryStateChangeUrl(fromUrl, fromUrl,
                                                              tests_[i].toUrl);
    bool expected_result = tests_[i].expectedUrl.size() > 0;
    bool actual_result = toUrl.is_valid();
    if (actual_result) {
      actual_result = history_state_util::IsHistoryStateChangeValid(fromUrl,
                                                                    toUrl);
    }
    EXPECT_EQ(expected_result, actual_result) << tests_[i].fromUrl << " "
                                              << tests_[i].toUrl;
  }
}

TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrl) {
  for (size_t i = 0; i < arraysize(tests_); ++i) {
    GURL fromUrl(tests_[i].fromUrl);
    GURL expectedResult(tests_[i].expectedUrl);
    GURL actualResult = history_state_util::GetHistoryStateChangeUrl(
        fromUrl, fromUrl, tests_[i].toUrl);
    EXPECT_EQ(expectedResult, actualResult);
  }
}

// Ensures that the baseUrl is used to resolve the destination, not currentUrl.
TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithBase) {
  GURL fromUrl("http://foo.com/relative/path");
  GURL baseUrl("http://foo.com");
  std::string destination = "bar";

  GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
                                                             destination);
  EXPECT_TRUE(result.is_valid());
  EXPECT_EQ(GURL("http://foo.com/bar"), result);
}

// Ensures that an invalid baseUrl gracefully returns an invalid destination.
TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithInvalidBase) {
  GURL fromUrl("http://foo.com");
  GURL baseUrl("http://not a url");
  std::string destination = "baz";

  GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
                                                             destination);
  EXPECT_FALSE(result.is_valid());
}

}  // anonymous namespace
}  // namespace web