Test that Branch Redirect supports having short ref names in gitiles url. PiperOrigin-RevId: 372370695 Change-Id: Ia3be25cbec3297cca78729834562464d2f2fb9ab
diff --git a/javatests/com/google/gitiles/BranchRedirectFilterTest.java b/javatests/com/google/gitiles/BranchRedirectFilterTest.java index fbf0ff4..76c6198 100644 --- a/javatests/com/google/gitiles/BranchRedirectFilterTest.java +++ b/javatests/com/google/gitiles/BranchRedirectFilterTest.java
@@ -27,6 +27,7 @@ import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Before; @@ -57,10 +58,10 @@ new BranchRedirectFilter() { @Override protected Optional<String> getRedirectBranch(Repository repo, String sourceBranch) { - if (MASTER.equals(sourceBranch)) { + if (MASTER.equals(toFullBranchName(sourceBranch))) { return Optional.of(MAIN); } - if (FOO.equals(sourceBranch)) { + if (FOO.equals(toFullBranchName(sourceBranch))) { return Optional.of(BAR); } return Optional.empty(); @@ -109,6 +110,20 @@ } @Test + public void show_withRedirect_usingShortRefInUrl() throws Exception { + repo.branch(MASTER).commit().add("foo", "contents").create(); + + String path = "/repo/+/master/foo"; + FakeHttpServletRequest req = newHttpRequest(path, ORIGIN, QUERY_STRING_HTML); + FakeHttpServletResponse res = new FakeHttpServletResponse(); + + servlet.service(req, res); + assertThat(res.getStatus()).isEqualTo(SC_MOVED_PERMANENTLY); + assertThat(res.getHeader(HttpHeaders.LOCATION)) + .isEqualTo("/b/repo/+/refs/heads/main/foo?format=html"); + } + + @Test public void show_onAutomationRequest() throws Exception { repo.branch(MASTER).commit().add("foo", "contents").create(); @@ -249,6 +264,13 @@ .isEqualTo("/b/repo/+/refs/heads/main%7E2..refs/heads/main/?format=html"); } + private static String toFullBranchName(String sourceBranch) { + if (sourceBranch.startsWith(Constants.R_REFS)) { + return sourceBranch; + } + return Constants.R_HEADS + sourceBranch; + } + private static FakeHttpServletRequest newHttpRequest( String path, String origin, @Nullable String queryString) { FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();