| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 1 | // Copyright (C) 2014 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; |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 18 | import static java.nio.charset.StandardCharsets.UTF_8; |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 19 | import static javax.servlet.http.HttpServletResponse.SC_OK; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 20 | |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 21 | import com.google.common.io.BaseEncoding; |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 22 | import com.google.common.net.HttpHeaders; |
| Andrew Bonventre | 199efc4 | 2017-05-10 13:57:39 -0700 | [diff] [blame] | 23 | import com.google.gitiles.FileJsonData.File; |
| Dave Borowitz | 3c44150 | 2014-09-05 16:06:37 -0700 | [diff] [blame] | 24 | import com.google.gitiles.TreeJsonData.Tree; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 25 | import com.google.template.soy.data.SoyListData; |
| 26 | import com.google.template.soy.data.restricted.StringData; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Map; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; |
| 30 | import org.eclipse.jgit.dircache.DirCacheEntry; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.lib.FileMode; |
| 32 | import org.eclipse.jgit.lib.ObjectId; |
| 33 | import org.eclipse.jgit.revwalk.RevBlob; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | 228f357 | 2014-05-02 14:26:25 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.revwalk.RevTree; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 36 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 37 | import org.junit.runner.RunWith; |
| 38 | import org.junit.runners.JUnit4; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 39 | |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 40 | /** Tests for {@PathServlet}. */ |
| 41 | @SuppressWarnings("unchecked") |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 42 | @RunWith(JUnit4.class) |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 43 | public class PathServletTest extends ServletTest { |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 44 | @Test |
| 45 | public void rootTreeHtml() throws Exception { |
| 46 | repo.branch("master").commit().add("foo", "contents").create(); |
| 47 | |
| 48 | Map<String, ?> data = buildData("/repo/+/master/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 49 | assertThat(data).containsEntry("type", "TREE"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 50 | List<Map<String, ?>> entries = getTreeEntries(data); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 51 | assertThat(entries).hasSize(1); |
| 52 | assertThat(entries.get(0).get("name")).isEqualTo("foo"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void subTreeHtml() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 57 | repo.branch("master") |
| 58 | .commit() |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 59 | .add("foo/bar", "bar contents") |
| 60 | .add("baz", "baz contents") |
| 61 | .create(); |
| 62 | |
| 63 | Map<String, ?> data = buildData("/repo/+/master/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 64 | assertThat(data).containsEntry("type", "TREE"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 65 | List<Map<String, ?>> entries = getTreeEntries(data); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 66 | assertThat(entries).hasSize(2); |
| 67 | assertThat(entries.get(0).get("name")).isEqualTo("baz"); |
| 68 | assertThat(entries.get(1).get("name")).isEqualTo("foo/"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 69 | |
| 70 | data = buildData("/repo/+/master/foo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 71 | assertThat(data).containsEntry("type", "TREE"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 72 | entries = getTreeEntries(data); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 73 | assertThat(entries).hasSize(1); |
| 74 | assertThat(entries.get(0).get("name")).isEqualTo("bar"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 75 | |
| 76 | data = buildData("/repo/+/master/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 77 | assertThat(data).containsEntry("type", "TREE"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 78 | entries = getTreeEntries(data); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 79 | assertThat(entries).hasSize(1); |
| 80 | assertThat(entries.get(0).get("name")).isEqualTo("bar"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void fileHtml() throws Exception { |
| 85 | repo.branch("master").commit().add("foo", "foo\ncontents\n").create(); |
| 86 | |
| 87 | Map<String, ?> data = buildData("/repo/+/master/foo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 88 | assertThat(data).containsEntry("type", "REGULAR_FILE"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 89 | |
| 90 | SoyListData lines = (SoyListData) getBlobData(data).get("lines"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 91 | assertThat(lines.length()).isEqualTo(2); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 92 | |
| 93 | SoyListData spans = lines.getListData(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 94 | assertThat(spans.length()).isEqualTo(1); |
| 95 | assertThat(spans.getMapData(0).get("classes")).isEqualTo(StringData.forValue("pln")); |
| 96 | assertThat(spans.getMapData(0).get("text")).isEqualTo(StringData.forValue("foo")); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 97 | |
| 98 | spans = lines.getListData(1); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 99 | assertThat(spans.length()).isEqualTo(1); |
| 100 | assertThat(spans.getMapData(0).get("classes")).isEqualTo(StringData.forValue("pln")); |
| 101 | assertThat(spans.getMapData(0).get("text")).isEqualTo(StringData.forValue("contents")); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| Ronald Bhuleskar | c9208e5 | 2019-07-10 17:44:08 -0700 | [diff] [blame] | 105 | public void fileWithMaxLines() throws Exception { |
| 106 | int MAX_LINE_COUNT = 50000; |
| 107 | StringBuilder contentBuilder = new StringBuilder(); |
| 108 | for (int i = 1; i < MAX_LINE_COUNT; i++) { |
| 109 | contentBuilder.append("\n"); |
| 110 | } |
| 111 | repo.branch("master").commit().add("bar", contentBuilder.toString()).create(); |
| 112 | |
| 113 | Map<String, ?> data = buildData("/repo/+/master/bar"); |
| 114 | SoyListData lines = (SoyListData) getBlobData(data).get("lines"); |
| 115 | assertThat(lines.length()).isEqualTo(MAX_LINE_COUNT - 1); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void fileLargerThanSupportedLines() throws Exception { |
| 120 | int MAX_LINE_COUNT = 50000; |
| 121 | StringBuilder contentBuilder = new StringBuilder(); |
| 122 | for (int i = 1; i <= MAX_LINE_COUNT; i++) { |
| 123 | contentBuilder.append("\n"); |
| 124 | } |
| 125 | repo.branch("master").commit().add("largebar", contentBuilder.toString()).create(); |
| 126 | |
| 127 | Map<String, ?> data = buildData("/repo/+/master/largebar"); |
| 128 | SoyListData lines = (SoyListData) getBlobData(data).get("lines"); |
| 129 | assertThat(lines).isNull(); |
| 130 | } |
| 131 | |
| 132 | @Test |
| Luca Milanesio | beb12ac | 2019-11-18 12:14:38 -0800 | [diff] [blame] | 133 | public void largeFileHtml() throws Exception { |
| 134 | int largeContentSize = BlobSoyData.MAX_FILE_SIZE + 1; |
| 135 | repo.branch("master").commit().add("foo", generateContent(largeContentSize)).create(); |
| 136 | |
| 137 | Map<String, ?> data = (Map<String, ?>) buildData("/repo/+/master/foo").get("data"); |
| 138 | assertThat(data).containsEntry("lines", null); |
| 139 | assertThat(data).containsEntry("size", "" + largeContentSize); |
| 140 | } |
| 141 | |
| 142 | private static String generateContent(int contentSize) { |
| 143 | char[] str = new char[contentSize]; |
| 144 | for (int i = 0; i < contentSize; i++) { |
| 145 | str[i] = (char) ('0' + (i % 78)); |
| 146 | } |
| 147 | return new String(str); |
| 148 | } |
| 149 | |
| 150 | @Test |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 151 | public void symlinkHtml() throws Exception { |
| 152 | final RevBlob link = repo.blob("foo"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 153 | repo.branch("master") |
| 154 | .commit() |
| 155 | .add("foo", "contents") |
| 156 | .edit( |
| 157 | new PathEdit("bar") { |
| 158 | @Override |
| 159 | public void apply(DirCacheEntry ent) { |
| 160 | ent.setFileMode(FileMode.SYMLINK); |
| 161 | ent.setObjectId(link); |
| 162 | } |
| 163 | }) |
| 164 | .create(); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 165 | |
| 166 | Map<String, ?> data = buildData("/repo/+/master/bar"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 167 | assertThat(data).containsEntry("type", "SYMLINK"); |
| 168 | assertThat(getBlobData(data)).containsEntry("target", "foo"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void gitlinkHtml() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 173 | String gitmodules = |
| 174 | "[submodule \"gitiles\"]\n" |
| 175 | + " path = gitiles\n" |
| 176 | + " url = https://gerrit.googlesource.com/gitiles\n"; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 177 | final String gitilesSha = "2b2f34bba3c2be7e2506ce6b1f040949da350cf9"; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 178 | repo.branch("master") |
| 179 | .commit() |
| 180 | .add(".gitmodules", gitmodules) |
| 181 | .edit( |
| 182 | new PathEdit("gitiles") { |
| 183 | @Override |
| 184 | public void apply(DirCacheEntry ent) { |
| 185 | ent.setFileMode(FileMode.GITLINK); |
| 186 | ent.setObjectId(ObjectId.fromString(gitilesSha)); |
| 187 | } |
| 188 | }) |
| 189 | .create(); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 190 | |
| 191 | Map<String, ?> data = buildData("/repo/+/master/gitiles"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 192 | assertThat(data).containsEntry("type", "GITLINK"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 193 | |
| 194 | Map<String, ?> linkData = getBlobData(data); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 195 | assertThat(linkData).containsEntry("sha", gitilesSha); |
| 196 | assertThat(linkData).containsEntry("remoteUrl", "https://gerrit.googlesource.com/gitiles"); |
| 197 | assertThat(linkData).containsEntry("httpUrl", "https://gerrit.googlesource.com/gitiles"); |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 200 | @Test |
| 201 | public void blobText() throws Exception { |
| 202 | repo.branch("master").commit().add("foo", "contents").create(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 203 | String text = buildBlob("/repo/+/master/foo", "100644"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 204 | assertThat(text).isEqualTo("contents"); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Test |
| Andrew Bonventre | 199efc4 | 2017-05-10 13:57:39 -0700 | [diff] [blame] | 208 | public void fileJson() throws Exception { |
| 209 | RevBlob blob = repo.blob("contents"); |
| 210 | repo.branch("master").commit().add("path/to/file", blob).create(); |
| 211 | |
| 212 | File file = buildJson(File.class, "/repo/+/master/path/to/file"); |
| 213 | |
| 214 | assertThat(file.id).isEqualTo(blob.name()); |
| 215 | assertThat(file.repo).isEqualTo("repo"); |
| 216 | assertThat(file.revision).isEqualTo("master"); |
| 217 | assertThat(file.path).isEqualTo("path/to/file"); |
| 218 | } |
| 219 | |
| 220 | @Test |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 221 | public void symlinkText() throws Exception { |
| 222 | final RevBlob link = repo.blob("foo"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 223 | repo.branch("master") |
| 224 | .commit() |
| 225 | .edit( |
| 226 | new PathEdit("baz") { |
| 227 | @Override |
| 228 | public void apply(DirCacheEntry ent) { |
| 229 | ent.setFileMode(FileMode.SYMLINK); |
| 230 | ent.setObjectId(link); |
| 231 | } |
| 232 | }) |
| 233 | .create(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 234 | String text = buildBlob("/repo/+/master/baz", "120000"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 235 | assertThat(text).isEqualTo("foo"); |
| Dave Borowitz | 228f357 | 2014-05-02 14:26:25 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void treeText() throws Exception { |
| 240 | RevBlob blob = repo.blob("contents"); |
| 241 | RevTree tree = repo.tree(repo.file("foo/bar", blob)); |
| 242 | repo.branch("master").commit().setTopLevelTree(tree).create(); |
| 243 | |
| 244 | String expected = "040000 tree " + repo.get(tree, "foo").name() + "\tfoo\n"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 245 | assertThat(buildBlob("/repo/+/master/", "040000")).isEqualTo(expected); |
| Dave Borowitz | 228f357 | 2014-05-02 14:26:25 -0700 | [diff] [blame] | 246 | |
| 247 | expected = "100644 blob " + blob.name() + "\tbar\n"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 248 | assertThat(buildBlob("/repo/+/master/foo", "040000")).isEqualTo(expected); |
| 249 | assertThat(buildBlob("/repo/+/master/foo/", "040000")).isEqualTo(expected); |
| Dave Borowitz | 228f357 | 2014-05-02 14:26:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void treeTextEscaped() throws Exception { |
| 254 | RevBlob blob = repo.blob("contents"); |
| 255 | repo.branch("master").commit().add("foo\nbar\rbaz", blob).create(); |
| 256 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 257 | assertThat(buildBlob("/repo/+/master/", "040000")) |
| 258 | .isEqualTo("100644 blob " + blob.name() + "\t\"foo\\nbar\\rbaz\"\n"); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void nonBlobText() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 263 | String gitmodules = |
| 264 | "[submodule \"gitiles\"]\n" |
| 265 | + " path = gitiles\n" |
| 266 | + " url = https://gerrit.googlesource.com/gitiles\n"; |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 267 | final String gitilesSha = "2b2f34bba3c2be7e2506ce6b1f040949da350cf9"; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 268 | repo.branch("master") |
| 269 | .commit() |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 270 | .add("foo/bar", "contents") |
| 271 | .add(".gitmodules", gitmodules) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 272 | .edit( |
| 273 | new PathEdit("gitiles") { |
| 274 | @Override |
| 275 | public void apply(DirCacheEntry ent) { |
| 276 | ent.setFileMode(FileMode.GITLINK); |
| 277 | ent.setObjectId(ObjectId.fromString(gitilesSha)); |
| 278 | } |
| 279 | }) |
| 280 | .create(); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 281 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 282 | assertNotFound("/repo/+/master/nonexistent", "format=text"); |
| 283 | assertNotFound("/repo/+/master/gitiles", "format=text"); |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 286 | @Test |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 287 | public void treeJsonSizes() throws Exception { |
| 288 | RevCommit c = repo.parseBody(repo.branch("master").commit().add("baz", "01234567").create()); |
| 289 | |
| 290 | Tree tree = buildJson(Tree.class, "/repo/+/master/", "long=1"); |
| 291 | |
| 292 | assertThat(tree.id).isEqualTo(c.getTree().name()); |
| 293 | assertThat(tree.entries).hasSize(1); |
| 294 | assertThat(tree.entries.get(0).mode).isEqualTo(0100644); |
| 295 | assertThat(tree.entries.get(0).type).isEqualTo("blob"); |
| 296 | assertThat(tree.entries.get(0).name).isEqualTo("baz"); |
| 297 | assertThat(tree.entries.get(0).size).isEqualTo(8); |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | public void treeJsonLinkTarget() throws Exception { |
| 302 | final ObjectId targetID = repo.blob("target"); |
| 303 | RevCommit c = |
| 304 | repo.parseBody( |
| 305 | repo.branch("master") |
| 306 | .commit() |
| 307 | .edit( |
| 308 | new PathEdit("link") { |
| 309 | @Override |
| 310 | public void apply(DirCacheEntry ent) { |
| 311 | ent.setFileMode(FileMode.SYMLINK); |
| 312 | ent.setObjectId(targetID); |
| 313 | } |
| 314 | }) |
| 315 | .create()); |
| 316 | |
| 317 | Tree tree = buildJson(Tree.class, "/repo/+/master/", "long=1"); |
| 318 | |
| 319 | assertThat(tree.id).isEqualTo(c.getTree().name()); |
| 320 | assertThat(tree.entries).hasSize(1); |
| 321 | |
| 322 | TreeJsonData.Entry e = tree.entries.get(0); |
| 323 | assertThat(e.mode).isEqualTo(0120000); |
| 324 | assertThat(e.type).isEqualTo("blob"); |
| 325 | assertThat(e.name).isEqualTo("link"); |
| 326 | assertThat(e.id).isEqualTo(targetID.name()); |
| 327 | assertThat(e.target).isEqualTo("target"); |
| 328 | } |
| 329 | |
| 330 | @Test |
| Han-Wen Nienhuys | 0dc9387 | 2016-05-03 15:21:42 +0200 | [diff] [blame] | 331 | public void treeJsonRecursive() throws Exception { |
| 332 | RevCommit c = |
| 333 | repo.parseBody( |
| 334 | repo.branch("master") |
| 335 | .commit() |
| 336 | .add("foo/baz/bar/a", "bar contents") |
| 337 | .add("foo/baz/bar/b", "bar contents") |
| 338 | .add("baz", "baz contents") |
| 339 | .create()); |
| 340 | Tree tree = buildJson(Tree.class, "/repo/+/master/", "recursive=1"); |
| 341 | |
| 342 | assertThat(tree.id).isEqualTo(c.getTree().name()); |
| 343 | assertThat(tree.entries).hasSize(3); |
| 344 | |
| 345 | assertThat(tree.entries.get(0).name).isEqualTo("baz"); |
| 346 | assertThat(tree.entries.get(1).name).isEqualTo("foo/baz/bar/a"); |
| 347 | assertThat(tree.entries.get(2).name).isEqualTo("foo/baz/bar/b"); |
| 348 | |
| 349 | tree = buildJson(Tree.class, "/repo/+/master/foo/baz", "recursive=1"); |
| 350 | |
| 351 | assertThat(tree.entries).hasSize(2); |
| 352 | |
| 353 | assertThat(tree.entries.get(0).name).isEqualTo("bar/a"); |
| 354 | assertThat(tree.entries.get(1).name).isEqualTo("bar/b"); |
| 355 | } |
| 356 | |
| 357 | @Test |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 358 | public void treeJson() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 359 | RevCommit c = |
| 360 | repo.parseBody( |
| 361 | repo.branch("master") |
| 362 | .commit() |
| 363 | .add("foo/bar", "bar contents") |
| 364 | .add("baz", "baz contents") |
| 365 | .create()); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 366 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 367 | Tree tree = buildJson(Tree.class, "/repo/+/master/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 368 | assertThat(tree.id).isEqualTo(c.getTree().name()); |
| 369 | assertThat(tree.entries).hasSize(2); |
| 370 | assertThat(tree.entries.get(0).mode).isEqualTo(0100644); |
| 371 | assertThat(tree.entries.get(0).type).isEqualTo("blob"); |
| 372 | assertThat(tree.entries.get(0).id).isEqualTo(repo.get(c.getTree(), "baz").name()); |
| 373 | assertThat(tree.entries.get(0).name).isEqualTo("baz"); |
| 374 | assertThat(tree.entries.get(1).mode).isEqualTo(040000); |
| 375 | assertThat(tree.entries.get(1).type).isEqualTo("tree"); |
| 376 | assertThat(tree.entries.get(1).id).isEqualTo(repo.get(c.getTree(), "foo").name()); |
| 377 | assertThat(tree.entries.get(1).name).isEqualTo("foo"); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 378 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 379 | tree = buildJson(Tree.class, "/repo/+/master/foo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 380 | assertThat(tree.id).isEqualTo(repo.get(c.getTree(), "foo").name()); |
| 381 | assertThat(tree.entries).hasSize(1); |
| 382 | assertThat(tree.entries.get(0).mode).isEqualTo(0100644); |
| 383 | assertThat(tree.entries.get(0).type).isEqualTo("blob"); |
| 384 | assertThat(tree.entries.get(0).id).isEqualTo(repo.get(c.getTree(), "foo/bar").name()); |
| 385 | assertThat(tree.entries.get(0).name).isEqualTo("bar"); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 386 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 387 | tree = buildJson(Tree.class, "/repo/+/master/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 388 | assertThat(tree.id).isEqualTo(repo.get(c.getTree(), "foo").name()); |
| 389 | assertThat(tree.entries).hasSize(1); |
| 390 | assertThat(tree.entries.get(0).mode).isEqualTo(0100644); |
| 391 | assertThat(tree.entries.get(0).type).isEqualTo("blob"); |
| 392 | assertThat(tree.entries.get(0).id).isEqualTo(repo.get(c.getTree(), "foo/bar").name()); |
| 393 | assertThat(tree.entries.get(0).name).isEqualTo("bar"); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 396 | @Test |
| 397 | public void allowOrigin() throws Exception { |
| 398 | repo.branch("master").commit().add("foo", "contents").create(); |
| 399 | FakeHttpServletResponse res = buildText("/repo/+/master/foo"); |
| 400 | assertThat(res.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)) |
| 401 | .isEqualTo("http://localhost"); |
| 402 | } |
| 403 | |
| 404 | @Test |
| 405 | public void rejectOrigin() throws Exception { |
| 406 | repo.branch("master").commit().add("foo", "contents").create(); |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 407 | FakeHttpServletResponse res = |
| 408 | buildResponse("/repo/+/master/foo", "format=text", SC_OK, "http://notlocalhost"); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 409 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain"); |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 410 | assertThat(res.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo(null); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 413 | private Map<String, ?> getBlobData(Map<String, ?> data) { |
| 414 | return ((Map<String, Map<String, ?>>) data).get("data"); |
| 415 | } |
| 416 | |
| 417 | private List<Map<String, ?>> getTreeEntries(Map<String, ?> data) { |
| 418 | return ((Map<String, List<Map<String, ?>>>) data.get("data")).get("entries"); |
| 419 | } |
| 420 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 421 | private String buildBlob(String path, String expectedMode) throws Exception { |
| 422 | FakeHttpServletResponse res = buildText(path); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 423 | assertThat(res.getHeader(PathServlet.MODE_HEADER)).isEqualTo(expectedMode); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 424 | String base64 = res.getActualBodyString(); |
| 425 | return new String(BaseEncoding.base64().decode(base64), UTF_8); |
| Dave Borowitz | 228f357 | 2014-05-02 14:26:25 -0700 | [diff] [blame] | 426 | } |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 427 | } |