| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 1 | // Copyright (C) 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| 19 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 20 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 22 | import com.google.gson.reflect.TypeToken; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 23 | import com.google.template.soy.data.restricted.NullData; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import javax.servlet.http.HttpServletResponse; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 27 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 28 | import org.eclipse.jgit.junit.TestRepository; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 33 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 34 | /** Tests for {@link HostIndexServlet}. */ |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 35 | @RunWith(JUnit4.class) |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 36 | public class HostIndexServletTest extends ServletTest { |
| 37 | private static final String NAME = "foo/bar/repo"; |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame] | 38 | private static final TypeToken<Map<String, RepositoryDescription>> REPOS = |
| 39 | new TypeToken<Map<String, RepositoryDescription>>() {}; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 40 | |
| 41 | @Override |
| 42 | @Before |
| 43 | public void setUp() throws Exception { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 44 | repo = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription(NAME))); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 45 | servlet = TestGitilesServlet.create(repo); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void rootHtml() throws Exception { |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 50 | Map<String, Object> data = buildData("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 51 | assertThat(data).containsEntry("hostName", URLS.getHostName(null)); |
| 52 | assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE); |
| 53 | assertThat(data).containsEntry("prefix", ""); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 54 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 55 | ImmutableList<Map<String, Object>> repos = |
| 56 | (ImmutableList<Map<String, Object>>) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 57 | assertThat(repos).hasSize(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 58 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 59 | Map<String, Object> ent = repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 60 | assertThat(ent.get("name").toString()).isEqualTo(NAME); |
| 61 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void fooSubdirHtml() throws Exception { |
| 66 | Map<String, ?> data = buildData("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 67 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo"); |
| 68 | assertThat(data).containsEntry("prefix", "foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 69 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 70 | ImmutableList<Map<String, Object>> breadcrumbs = |
| 71 | (ImmutableList<Map<String, Object>>) data.get("breadcrumbs"); |
| 72 | assertThat(breadcrumbs.size()).isEqualTo(2); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 73 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 74 | ImmutableList<Map<String, Object>> repos = |
| 75 | (ImmutableList<Map<String, Object>>) data.get("repositories"); |
| 76 | assertThat(repos.size()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 77 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 78 | Map<String, Object> ent = repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 79 | assertThat(ent.get("name").toString()).isEqualTo("bar/repo"); |
| 80 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void fooBarSubdirHtml() throws Exception { |
| 85 | Map<String, ?> data = buildData("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 86 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar"); |
| 87 | assertThat(data).containsEntry("prefix", "foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 88 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 89 | ImmutableList<Map<String, Object>> breadcrumbs = |
| 90 | (ImmutableList<Map<String, Object>>) data.get("breadcrumbs"); |
| 91 | assertThat(breadcrumbs.size()).isEqualTo(3); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 92 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 93 | ImmutableList<Map<String, Object>> repos = |
| 94 | (ImmutableList<Map<String, Object>>) data.get("repositories"); |
| 95 | assertThat(repos.size()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 96 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 97 | Map<String, Object> ent = repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 98 | assertThat(ent.get("name").toString()).isEqualTo("repo"); |
| 99 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void rootText() throws Exception { |
| 104 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| 105 | FakeHttpServletResponse res = buildText("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 106 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void fooSubdirText() throws Exception { |
| 111 | FakeHttpServletResponse res = buildText("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 112 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void fooBarSubdirText() throws Exception { |
| 117 | FakeHttpServletResponse res = buildText("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 118 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void rootJson() throws Exception { |
| 123 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 124 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 125 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 126 | assertThat(res).hasSize(1); |
| 127 | assertThat(res).containsKey(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 128 | RepositoryDescription d = res.get(name); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 129 | assertThat(d.name).isEqualTo(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void fooSubdirJson() throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 134 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 135 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 136 | assertThat(res).hasSize(1); |
| 137 | assertThat(res).containsKey("bar/repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 138 | RepositoryDescription d = res.get("bar/repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 139 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void fooBarSubdirJson() throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 144 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 145 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 146 | assertThat(res).hasSize(1); |
| 147 | assertThat(res).containsKey("repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 148 | RepositoryDescription d = res.get("repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 149 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void emptySubdirectoryList() throws Exception { |
| 154 | assertNotFound("/no.repos/", null); |
| 155 | } |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 156 | |
| 157 | @Test |
| 158 | public void headOnRoot() throws Exception { |
| 159 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 160 | req.setMethod("HEAD"); |
| 161 | req.setPathInfo("/"); |
| 162 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 163 | servlet.service(req, res); |
| 164 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void headOnMissingSubdir() throws Exception { |
| 169 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 170 | req.setMethod("HEAD"); |
| 171 | req.setPathInfo("/no.repos/"); |
| 172 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 173 | servlet.service(req, res); |
| 174 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void headOnPopulatedSubdir() throws Exception { |
| 179 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 180 | req.setMethod("HEAD"); |
| 181 | req.setPathInfo("/foo/"); |
| 182 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 183 | servlet.service(req, res); |
| 184 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 185 | } |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 186 | } |