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 "mojo/shell/url_resolver.h"
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
15 typedef testing::Test URLResolverTest
;
17 TEST_F(URLResolverTest
, MojoURLsFallThrough
) {
19 resolver
.AddURLMapping(GURL("mojo:test"), GURL("mojo:foo"));
20 const GURL
base_url("file:/base");
21 resolver
.SetMojoBaseURL(base_url
);
22 GURL mapped_url
= resolver
.ApplyMappings(GURL("mojo:test"));
23 std::string
resolved(resolver
.ResolveMojoURL(mapped_url
).spec());
24 // Resolved must start with |base_url|.
25 EXPECT_EQ(base_url
.spec(), resolved
.substr(0, base_url
.spec().size()));
26 // And must contain foo.
27 EXPECT_NE(std::string::npos
, resolved
.find("foo"));
30 TEST_F(URLResolverTest
, MapURL
) {
32 resolver
.AddURLMapping(GURL("https://domokit.org/test.mojo"),
33 GURL("file:///mojo/src/out/Debug/test.mojo"));
35 resolver
.ApplyMappings(GURL("https://domokit.org/test.mojo"));
36 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url
.spec());
39 TEST_F(URLResolverTest
, MultipleMapURL
) {
41 resolver
.AddURLMapping(GURL("https://a.org/foo"),
42 GURL("https://b.org/a/foo"));
43 resolver
.AddURLMapping(GURL("https://b.org/a/foo"),
44 GURL("https://c.org/b/a/foo"));
45 GURL mapped_url
= resolver
.ApplyMappings(GURL("https://a.org/foo"));
46 EXPECT_EQ("https://c.org/b/a/foo", mapped_url
.spec());
49 TEST_F(URLResolverTest
, MapOrigin
) {
51 resolver
.AddOriginMapping(GURL("https://domokit.org"),
52 GURL("file:///mojo/src/out/Debug"));
54 resolver
.ApplyMappings(GURL("https://domokit.org/test.mojo"));
55 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url
.spec());
58 TEST_F(URLResolverTest
, MultipleMapOrigin
) {
60 resolver
.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
61 resolver
.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
62 GURL mapped_url
= resolver
.ApplyMappings(GURL("https://a.org/foo"));
63 EXPECT_EQ("https://c.org/b/a/foo", mapped_url
.spec());
66 TEST_F(URLResolverTest
, MapOriginThenURL
) {
68 resolver
.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
69 resolver
.AddURLMapping(GURL("https://b.org/a/foo"),
70 GURL("https://c.org/b/a/foo"));
71 GURL mapped_url
= resolver
.ApplyMappings(GURL("https://a.org/foo"));
72 EXPECT_EQ("https://c.org/b/a/foo", mapped_url
.spec());
75 TEST_F(URLResolverTest
, MapURLThenOrigin
) {
77 resolver
.AddURLMapping(GURL("https://a.org/foo"),
78 GURL("https://b.org/a/foo"));
79 resolver
.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
80 GURL mapped_url
= resolver
.ApplyMappings(GURL("https://a.org/foo"));
81 EXPECT_EQ("https://c.org/b/a/foo", mapped_url
.spec());
85 #define ARG_LITERAL(x) x
87 #define ARG_LITERAL(x) L ## x
90 TEST_F(URLResolverTest
, GetOriginMappings
) {
91 base::CommandLine::StringVector args
;
92 args
.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
93 std::vector
<URLResolver::OriginMapping
> mappings
=
94 URLResolver::GetOriginMappings(args
);
95 ASSERT_EQ(1U, mappings
.size());
96 EXPECT_EQ("https://a.org", mappings
[0].origin
);
97 EXPECT_EQ("https://b.org/a", mappings
[0].base_url
);
100 args
.push_back(ARG_LITERAL("-map-origin=https://a.org=https://b.org/a"));
101 mappings
= URLResolver::GetOriginMappings(args
);
102 ASSERT_EQ(1U, mappings
.size());
103 EXPECT_EQ("https://a.org", mappings
[0].origin
);
104 EXPECT_EQ("https://b.org/a", mappings
[0].base_url
);
107 args
.push_back(ARG_LITERAL("--map-origin"));
108 mappings
= URLResolver::GetOriginMappings(args
);
109 EXPECT_EQ(0U, mappings
.size());
112 args
.push_back(ARG_LITERAL("--map-origin="));
113 mappings
= URLResolver::GetOriginMappings(args
);
114 EXPECT_EQ(0U, mappings
.size());
117 args
.push_back(ARG_LITERAL("mojo_shell"));
118 args
.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
119 args
.push_back(ARG_LITERAL("--map-origin=https://b.org=https://c.org/b"));
120 args
.push_back(ARG_LITERAL("https://a.org/foo"));
121 mappings
= URLResolver::GetOriginMappings(args
);
122 ASSERT_EQ(2U, mappings
.size());
123 EXPECT_EQ("https://a.org", mappings
[0].origin
);
124 EXPECT_EQ("https://b.org/a", mappings
[0].base_url
);
125 EXPECT_EQ("https://b.org", mappings
[1].origin
);
126 EXPECT_EQ("https://c.org/b", mappings
[1].base_url
);
129 TEST_F(URLResolverTest
, TestQueryForURLMapping
) {
130 URLResolver resolver
;
131 resolver
.SetMojoBaseURL(GURL("file:/base"));
132 resolver
.AddURLMapping(GURL("https://a.org/foo"),
133 GURL("https://b.org/a/foo"));
134 resolver
.AddURLMapping(GURL("https://b.org/a/foo"),
135 GURL("https://c.org/b/a/foo"));
136 GURL mapped_url
= resolver
.ApplyMappings(GURL("https://a.org/foo?a=b"));
137 EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url
.spec());
140 TEST_F(URLResolverTest
, TestQueryForBaseURL
) {
141 URLResolver resolver
;
142 resolver
.SetMojoBaseURL(GURL("file:///base"));
143 GURL mapped_url
= resolver
.ResolveMojoURL(GURL("mojo:foo?a=b"));
144 EXPECT_EQ("file:///base/foo.mojo?a=b", mapped_url
.spec());