OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/runner/url_resolver.h" | 5 #include "mojo/runner/url_resolver.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "mojo/util/filename_util.h" | 10 #include "mojo/util/filename_util.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 URLResolver resolver; | 133 URLResolver resolver; |
134 resolver.SetMojoBaseURL(GURL("file:/base")); | 134 resolver.SetMojoBaseURL(GURL("file:/base")); |
135 resolver.AddURLMapping(GURL("https://a.org/foo"), | 135 resolver.AddURLMapping(GURL("https://a.org/foo"), |
136 GURL("https://b.org/a/foo")); | 136 GURL("https://b.org/a/foo")); |
137 resolver.AddURLMapping(GURL("https://b.org/a/foo"), | 137 resolver.AddURLMapping(GURL("https://b.org/a/foo"), |
138 GURL("https://c.org/b/a/foo")); | 138 GURL("https://c.org/b/a/foo")); |
139 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo?a=b")); | 139 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo?a=b")); |
140 EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url.spec()); | 140 EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url.spec()); |
141 } | 141 } |
142 | 142 |
143 TEST_F(URLResolverTest, TestQueryForBaseURL) { | 143 TEST_F(URLResolverTest, TestQueryForBaseFileURL) { |
144 URLResolver resolver; | 144 URLResolver resolver; |
145 resolver.SetMojoBaseURL(GURL("file:///base")); | 145 resolver.SetMojoBaseURL(GURL("file:///base")); |
146 GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b")); | 146 GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b")); |
147 EXPECT_EQ("file:///base/foo.mojo?a=b", mapped_url.spec()); | 147 EXPECT_EQ("file:///base/foo/foo.mojo?a=b", mapped_url.spec()); |
148 } | 148 } |
149 | 149 |
150 // Verifies that ResolveMojoURL prefers the directory with the name of the host | 150 TEST_F(URLResolverTest, TestQueryForBaseHttpURL) { |
151 // over the raw file. | |
152 TEST_F(URLResolverTest, PreferDirectory) { | |
153 base::ScopedTempDir tmp_dir; | |
154 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); | |
155 URLResolver resolver; | 151 URLResolver resolver; |
156 // With no directory |mojo:foo| maps to path/foo.mojo. | 152 resolver.SetMojoBaseURL(GURL("http://127.0.0.1:1234")); |
157 resolver.SetMojoBaseURL(util::FilePathToFileURL(tmp_dir.path())); | 153 GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b")); |
158 const GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo")); | 154 EXPECT_EQ("http://127.0.0.1:1234/foo/foo.mojo?a=b", mapped_url.spec()); |
159 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo.mojo", | |
160 mapped_url.spec()); | |
161 | |
162 // With an empty directory |mojo:foo| maps to path/foo.mojo. | |
163 const base::FilePath foo_file_path( | |
164 tmp_dir.path().Append(FILE_PATH_LITERAL("foo"))); | |
165 ASSERT_TRUE(base::CreateDirectory(foo_file_path)); | |
166 const GURL mapped_url_with_dir = resolver.ResolveMojoURL(GURL("mojo:foo")); | |
167 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo.mojo", | |
168 mapped_url_with_dir.spec()); | |
169 | |
170 // When foo.mojo exists in the directory (path/foo/foo.mojo), then it should | |
171 // be picked up. | |
172 // With an empty directory |mojo:foo| maps to path/foo/foo.mojo. | |
173 ASSERT_EQ(1, | |
174 base::WriteFile(foo_file_path.Append(FILE_PATH_LITERAL("foo.mojo")), | |
175 "a", 1)); | |
176 const GURL mapped_url_in_dir = resolver.ResolveMojoURL(GURL("mojo:foo")); | |
177 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo/foo.mojo", | |
178 mapped_url_in_dir.spec()); | |
179 } | 155 } |
180 | 156 |
181 } // namespace | 157 } // namespace |
182 } // namespace test | 158 } // namespace test |
183 } // namespace runner | 159 } // namespace runner |
184 } // namespace mojo | 160 } // namespace mojo |
OLD | NEW |