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