blob: a79ee8c9a8367595e4a9784a28e0f5565712c97b [file] [log] [blame]
Shawn Pearcec709c4c2015-08-28 15:30:42 -07001// 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070018import static com.google.gitiles.TestGitilesUrls.URLS;
19import static java.nio.charset.StandardCharsets.UTF_8;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070020
David Pursehouse6740b3e2019-07-17 11:06:08 +090021import com.google.common.collect.ImmutableList;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070022import com.google.gson.reflect.TypeToken;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070023import com.google.template.soy.data.restricted.NullData;
Dave Borowitz3b744b12016-08-19 16:11:10 -040024import java.util.Map;
25import javax.servlet.http.HttpServletResponse;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070026import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
27import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
28import org.eclipse.jgit.junit.TestRepository;
29import org.junit.Before;
30import org.junit.Test;
Shawn Pearce9be60222015-08-31 11:41:19 -070031import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070033
Shawn Pearcec709c4c2015-08-28 15:30:42 -070034/** Tests for {@link HostIndexServlet}. */
Shawn Pearce9be60222015-08-31 11:41:19 -070035@RunWith(JUnit4.class)
Shawn Pearcec709c4c2015-08-28 15:30:42 -070036public class HostIndexServletTest extends ServletTest {
37 private static final String NAME = "foo/bar/repo";
Dave Borowitz8e290352015-10-26 11:33:30 -040038 private static final TypeToken<Map<String, RepositoryDescription>> REPOS =
39 new TypeToken<Map<String, RepositoryDescription>>() {};
Shawn Pearcec709c4c2015-08-28 15:30:42 -070040
41 @Override
42 @Before
43 public void setUp() throws Exception {
David Pursehouseccaa85d2017-05-30 10:47:27 +090044 repo = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription(NAME)));
Shawn Pearcec709c4c2015-08-28 15:30:42 -070045 servlet = TestGitilesServlet.create(repo);
46 }
47
48 @Test
49 public void rootHtml() throws Exception {
David Pursehouse6740b3e2019-07-17 11:06:08 +090050 Map<String, Object> data = buildData("/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040051 assertThat(data).containsEntry("hostName", URLS.getHostName(null));
52 assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE);
53 assertThat(data).containsEntry("prefix", "");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070054
David Pursehouse6740b3e2019-07-17 11:06:08 +090055 ImmutableList<Map<String, Object>> repos =
56 (ImmutableList<Map<String, Object>>) data.get("repositories");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040057 assertThat(repos).hasSize(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070058
David Pursehouse6740b3e2019-07-17 11:06:08 +090059 Map<String, Object> ent = repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040060 assertThat(ent.get("name").toString()).isEqualTo(NAME);
61 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070062 }
63
64 @Test
65 public void fooSubdirHtml() throws Exception {
66 Map<String, ?> data = buildData("/foo/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040067 assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo");
68 assertThat(data).containsEntry("prefix", "foo/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070069
David Pursehouse6740b3e2019-07-17 11:06:08 +090070 ImmutableList<Map<String, Object>> breadcrumbs =
71 (ImmutableList<Map<String, Object>>) data.get("breadcrumbs");
72 assertThat(breadcrumbs.size()).isEqualTo(2);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070073
David Pursehouse6740b3e2019-07-17 11:06:08 +090074 ImmutableList<Map<String, Object>> repos =
75 (ImmutableList<Map<String, Object>>) data.get("repositories");
76 assertThat(repos.size()).isEqualTo(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070077
David Pursehouse6740b3e2019-07-17 11:06:08 +090078 Map<String, Object> ent = repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040079 assertThat(ent.get("name").toString()).isEqualTo("bar/repo");
80 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070081 }
82
83 @Test
84 public void fooBarSubdirHtml() throws Exception {
85 Map<String, ?> data = buildData("/foo/bar/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040086 assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar");
87 assertThat(data).containsEntry("prefix", "foo/bar/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070088
David Pursehouse6740b3e2019-07-17 11:06:08 +090089 ImmutableList<Map<String, Object>> breadcrumbs =
90 (ImmutableList<Map<String, Object>>) data.get("breadcrumbs");
91 assertThat(breadcrumbs.size()).isEqualTo(3);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070092
David Pursehouse6740b3e2019-07-17 11:06:08 +090093 ImmutableList<Map<String, Object>> repos =
94 (ImmutableList<Map<String, Object>>) data.get("repositories");
95 assertThat(repos.size()).isEqualTo(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070096
David Pursehouse6740b3e2019-07-17 11:06:08 +090097 Map<String, Object> ent = repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040098 assertThat(ent.get("name").toString()).isEqualTo("repo");
99 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700100 }
101
102 @Test
103 public void rootText() throws Exception {
104 String name = repo.getRepository().getDescription().getRepositoryName();
105 FakeHttpServletResponse res = buildText("/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400106 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700107 }
108
109 @Test
110 public void fooSubdirText() throws Exception {
111 FakeHttpServletResponse res = buildText("/foo/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400112 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700113 }
114
115 @Test
116 public void fooBarSubdirText() throws Exception {
117 FakeHttpServletResponse res = buildText("/foo/bar/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400118 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700119 }
120
121 @Test
122 public void rootJson() throws Exception {
123 String name = repo.getRepository().getDescription().getRepositoryName();
Dave Borowitza774f592015-10-26 11:41:27 -0400124 Map<String, RepositoryDescription> res = buildJson(REPOS, "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700125
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400126 assertThat(res).hasSize(1);
127 assertThat(res).containsKey(name);
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700128 RepositoryDescription d = res.get(name);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400129 assertThat(d.name).isEqualTo(name);
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700130 }
131
132 @Test
133 public void fooSubdirJson() throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400134 Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700135
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400136 assertThat(res).hasSize(1);
137 assertThat(res).containsKey("bar/repo");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700138 RepositoryDescription d = res.get("bar/repo");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400139 assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName());
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700140 }
141
142 @Test
143 public void fooBarSubdirJson() throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400144 Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/bar/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700145
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400146 assertThat(res).hasSize(1);
147 assertThat(res).containsKey("repo");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700148 RepositoryDescription d = res.get("repo");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400149 assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName());
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700150 }
151
152 @Test
153 public void emptySubdirectoryList() throws Exception {
154 assertNotFound("/no.repos/", null);
155 }
Shawn Pearce10e68e62016-01-02 09:37:58 -0800156
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 Pearcec709c4c2015-08-28 15:30:42 -0700186}