| 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 | |
| 21 | import com.google.gson.reflect.TypeToken; |
| 22 | import com.google.template.soy.data.SoyListData; |
| 23 | import com.google.template.soy.data.SoyMapData; |
| 24 | import com.google.template.soy.data.restricted.NullData; |
| 25 | |
| 26 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 27 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 28 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 29 | import org.eclipse.jgit.junit.TestRepository; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 32 | import org.junit.runner.RunWith; |
| 33 | import org.junit.runners.JUnit4; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.Map; |
| 36 | |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 37 | import javax.servlet.http.HttpServletResponse; |
| 38 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 39 | /** Tests for {@link HostIndexServlet}. */ |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 40 | @RunWith(JUnit4.class) |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 41 | public class HostIndexServletTest extends ServletTest { |
| 42 | private static final String NAME = "foo/bar/repo"; |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame] | 43 | private static final TypeToken<Map<String, RepositoryDescription>> REPOS = |
| 44 | new TypeToken<Map<String, RepositoryDescription>>() {}; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | @Before |
| 48 | public void setUp() throws Exception { |
| 49 | repo = new TestRepository<DfsRepository>( |
| 50 | new InMemoryRepository(new DfsRepositoryDescription(NAME))); |
| 51 | servlet = TestGitilesServlet.create(repo); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void rootHtml() throws Exception { |
| 56 | Map<String, ?> data = buildData("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 57 | assertThat(data).containsEntry("hostName", URLS.getHostName(null)); |
| 58 | assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE); |
| 59 | assertThat(data).containsEntry("prefix", ""); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 60 | |
| 61 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 62 | assertThat(repos).hasSize(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 63 | |
| 64 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 65 | assertThat(ent.get("name").toString()).isEqualTo(NAME); |
| 66 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void fooSubdirHtml() throws Exception { |
| 71 | Map<String, ?> data = buildData("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 72 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo"); |
| 73 | assertThat(data).containsEntry("prefix", "foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 74 | |
| 75 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 76 | assertThat(breadcrumbs.length()).isEqualTo(2); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 77 | |
| 78 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 79 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 80 | |
| 81 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 82 | assertThat(ent.get("name").toString()).isEqualTo("bar/repo"); |
| 83 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void fooBarSubdirHtml() throws Exception { |
| 88 | Map<String, ?> data = buildData("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 89 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar"); |
| 90 | assertThat(data).containsEntry("prefix", "foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 91 | |
| 92 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 93 | assertThat(breadcrumbs.length()).isEqualTo(3); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 94 | |
| 95 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 96 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 97 | |
| 98 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 99 | assertThat(ent.get("name").toString()).isEqualTo("repo"); |
| 100 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void rootText() throws Exception { |
| 105 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| 106 | FakeHttpServletResponse res = buildText("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 107 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void fooSubdirText() throws Exception { |
| 112 | FakeHttpServletResponse res = buildText("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 113 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void fooBarSubdirText() throws Exception { |
| 118 | FakeHttpServletResponse res = buildText("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 119 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void rootJson() throws Exception { |
| 124 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 125 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 126 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 127 | assertThat(res).hasSize(1); |
| 128 | assertThat(res).containsKey(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 129 | RepositoryDescription d = res.get(name); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 130 | assertThat(d.name).isEqualTo(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void fooSubdirJson() throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 135 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 136 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 137 | assertThat(res).hasSize(1); |
| 138 | assertThat(res).containsKey("bar/repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 139 | RepositoryDescription d = res.get("bar/repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 140 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void fooBarSubdirJson() throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 145 | Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 146 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 147 | assertThat(res).hasSize(1); |
| 148 | assertThat(res).containsKey("repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 149 | RepositoryDescription d = res.get("repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 150 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void emptySubdirectoryList() throws Exception { |
| 155 | assertNotFound("/no.repos/", null); |
| 156 | } |
| Shawn Pearce | 10e68e6 | 2016-01-02 09:37:58 -0800 | [diff] [blame] | 157 | |
| 158 | @Test |
| 159 | public void headOnRoot() throws Exception { |
| 160 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 161 | req.setMethod("HEAD"); |
| 162 | req.setPathInfo("/"); |
| 163 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 164 | servlet.service(req, res); |
| 165 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void headOnMissingSubdir() throws Exception { |
| 170 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 171 | req.setMethod("HEAD"); |
| 172 | req.setPathInfo("/no.repos/"); |
| 173 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 174 | servlet.service(req, res); |
| 175 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void headOnPopulatedSubdir() throws Exception { |
| 180 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 181 | req.setMethod("HEAD"); |
| 182 | req.setPathInfo("/foo/"); |
| 183 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 184 | servlet.service(req, res); |
| 185 | assertThat(res.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 186 | } |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 187 | } |