history_state_util_unittest.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2012 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. #include "ios/web/history_state_util.h"
  5. #include <stddef.h>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #import "testing/gtest_mac.h"
  8. #include "testing/platform_test.h"
  9. #include "url/gurl.h"
  10. #if !defined(__has_feature) || !__has_feature(objc_arc)
  11. #error "This file requires ARC support."
  12. #endif
  13. namespace web {
  14. namespace {
  15. struct TestEntry {
  16. std::string fromUrl;
  17. std::string toUrl;
  18. std::string expectedUrl;
  19. };
  20. class HistoryStateUtilTest : public PlatformTest {
  21. protected:
  22. static const struct TestEntry tests_[];
  23. };
  24. const struct TestEntry HistoryStateUtilTest::tests_[] = {
  25. // Valid absolute changes.
  26. { "http://foo.com", "http://foo.com/bar", "http://foo.com/bar" },
  27. { "https://foo.com", "https://foo.com/bar", "https://foo.com/bar" },
  28. { "http://foo.com/", "http://foo.com#bar", "http://foo.com#bar" },
  29. { "http://foo.com:80", "http://foo.com:80/b", "http://foo.com:80/b"},
  30. { "http://foo.com:888", "http://foo.com:888/b", "http://foo.com:888/b"},
  31. // Valid relative changes.
  32. { "http://foo.com", "#bar", "http://foo.com#bar" },
  33. { "http://foo.com/", "#bar", "http://foo.com/#bar" },
  34. { "https://foo.com/", "bar", "https://foo.com/bar" },
  35. { "http://foo.com/foo/1", "/bar", "http://foo.com/bar" },
  36. { "http://foo.com/foo/1", "bar", "http://foo.com/foo/bar" },
  37. { "http://foo.com/", "bar.com", "http://foo.com/bar.com" },
  38. { "http://foo.com", "bar.com", "http://foo.com/bar.com" },
  39. { "http://foo.com:888", "bar.com", "http://foo.com:888/bar.com" },
  40. // Invalid scheme changes.
  41. { "http://foo.com", "https://foo.com#bar", "" },
  42. { "https://foo.com", "http://foo.com#bar", "" },
  43. // Invalid domain changes.
  44. { "http://foo.com/bar", "http://bar.com", "" },
  45. { "http://foo.com/bar", "http://www.foo.com/bar2", "" },
  46. // Valid port change.
  47. { "http://foo.com", "http://foo.com:80/bar", "http://foo.com/bar" },
  48. { "http://foo.com:80", "http://foo.com/bar", "http://foo.com/bar" },
  49. // Invalid port change.
  50. { "http://foo.com", "http://foo.com:42/bar", "" },
  51. { "http://foo.com:42", "http://foo.com/bar", "" },
  52. // Invalid URL.
  53. { "http://foo.com", "http://fo o.c om/ba r", "" },
  54. { "http://foo.com:80", "bar", "http://foo.com:80/bar" }
  55. };
  56. TEST_F(HistoryStateUtilTest, TestIsHistoryStateChangeValid) {
  57. for (size_t i = 0; i < std::size(tests_); ++i) {
  58. GURL fromUrl(tests_[i].fromUrl);
  59. GURL toUrl = history_state_util::GetHistoryStateChangeUrl(fromUrl, fromUrl,
  60. tests_[i].toUrl);
  61. bool expected_result = tests_[i].expectedUrl.size() > 0;
  62. bool actual_result = toUrl.is_valid();
  63. if (actual_result) {
  64. actual_result = history_state_util::IsHistoryStateChangeValid(fromUrl,
  65. toUrl);
  66. }
  67. EXPECT_EQ(expected_result, actual_result) << tests_[i].fromUrl << " "
  68. << tests_[i].toUrl;
  69. }
  70. }
  71. TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrl) {
  72. for (size_t i = 0; i < std::size(tests_); ++i) {
  73. GURL fromUrl(tests_[i].fromUrl);
  74. GURL expectedResult(tests_[i].expectedUrl);
  75. GURL actualResult = history_state_util::GetHistoryStateChangeUrl(
  76. fromUrl, fromUrl, tests_[i].toUrl);
  77. EXPECT_EQ(expectedResult, actualResult);
  78. }
  79. }
  80. // Ensures that the baseUrl is used to resolve the destination, not currentUrl.
  81. TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithBase) {
  82. GURL fromUrl("http://foo.com/relative/path");
  83. GURL baseUrl("http://foo.com");
  84. std::string destination = "bar";
  85. GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
  86. destination);
  87. EXPECT_TRUE(result.is_valid());
  88. EXPECT_EQ(GURL("http://foo.com/bar"), result);
  89. }
  90. // Ensures that an invalid baseUrl gracefully returns an invalid destination.
  91. TEST_F(HistoryStateUtilTest, TestGetHistoryStateChangeUrlWithInvalidBase) {
  92. GURL fromUrl("http://foo.com");
  93. GURL baseUrl("http://not a url");
  94. std::string destination = "baz";
  95. GURL result = history_state_util::GetHistoryStateChangeUrl(fromUrl, baseUrl,
  96. destination);
  97. EXPECT_FALSE(result.is_valid());
  98. }
  99. } // anonymous namespace
  100. } // namespace web