blob: 9e8bac492df5191dec4af5f4d37b64358e7235d8 [file] [log] [blame]
Dave Borowitzd0b7e182013-01-11 15:55:09 -08001// Copyright 2012 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
17import junit.framework.TestCase;
18
19import org.eclipse.jgit.junit.TestRepository;
20import org.eclipse.jgit.lib.ObjectId;
21import org.eclipse.jgit.lib.Repository;
22import org.eclipse.jgit.revwalk.RevCommit;
23import org.eclipse.jgit.revwalk.RevTag;
24import org.eclipse.jgit.storage.dfs.DfsRepository;
25import org.eclipse.jgit.storage.dfs.DfsRepositoryDescription;
26import org.eclipse.jgit.storage.dfs.InMemoryRepository;
27
28import java.io.IOException;
29
30/** Tests for {@link Linkifier}. */
31public class RefServletTest extends TestCase {
32 private TestRepository<DfsRepository> repo;;
33 private GitilesServlet servlet;
34
35 @Override
36 protected void setUp() throws Exception {
37 DfsRepository r = new InMemoryRepository(new DfsRepositoryDescription("test"));
38 repo = new TestRepository<DfsRepository>(r);
39
40 RevCommit commit = repo.branch("refs/heads/master").commit().create();
41 repo.update("refs/heads/branch", commit);
42 repo.update("refs/tags/ctag", commit);
43 RevTag tag = repo.tag("atag", commit);
44 repo.update("refs/tags/atag", tag);
45 r.updateRef("HEAD").link("refs/heads/master");
46
47 servlet = TestGitilesServlet.create(repo);
48 }
49
50 private String id(String refName) throws IOException {
51 return ObjectId.toString(repo.getRepository().getRef(refName).getObjectId());
52 }
53
54 private String peeled(String refName) throws IOException {
55 return ObjectId.toString(repo.getRepository().peel(
56 repo.getRepository().getRef(refName)).getPeeledObjectId());
57 }
58
59 public void testEvilRefName() throws Exception {
60 String evilRefName = "refs/evil/<script>window.close();</script>/&foo";
61 assertTrue(Repository.isValidRefName(evilRefName));
62 repo.branch(evilRefName).commit().create();
63
64 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
65 req.setPathInfo("/test/+refs/evil");
66 req.setQueryString("format=TEXT");
67 FakeHttpServletResponse res = new FakeHttpServletResponse();
68 servlet.service(req, res);
69
70 assertEquals(
71 id(evilRefName) + " refs/evil/&lt;script&gt;window.close();&lt;/script&gt;/&amp;foo\n",
72 res.getActualBodyString());
73 }
74
75 public void testGetRefsTextAll() throws Exception {
76 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
77 req.setPathInfo("/test/+refs");
78 req.setQueryString("format=TEXT");
79 FakeHttpServletResponse res = new FakeHttpServletResponse();
80 servlet.service(req, res);
81
82 assertEquals(200, res.getStatus());
83 assertEquals(
84 id("HEAD") + " HEAD\n"
85 + id("refs/heads/branch") + " refs/heads/branch\n"
86 + id("refs/heads/master") + " refs/heads/master\n"
87 + id("refs/tags/atag") + " refs/tags/atag\n"
88 + peeled("refs/tags/atag") + " refs/tags/atag^{}\n"
89 + id("refs/tags/ctag") + " refs/tags/ctag\n",
90 res.getActualBodyString());
91 }
92
93 public void testGetRefsTextAllTrailingSlash() throws Exception {
94 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
95 req.setPathInfo("/test/+refs");
96 req.setQueryString("format=TEXT");
97 FakeHttpServletResponse res = new FakeHttpServletResponse();
98 servlet.service(req, res);
99
100 assertEquals(200, res.getStatus());
101 assertEquals(
102 id("HEAD") + " HEAD\n"
103 + id("refs/heads/branch") + " refs/heads/branch\n"
104 + id("refs/heads/master") + " refs/heads/master\n"
105 + id("refs/tags/atag") + " refs/tags/atag\n"
106 + peeled("refs/tags/atag") + " refs/tags/atag^{}\n"
107 + id("refs/tags/ctag") + " refs/tags/ctag\n",
108 res.getActualBodyString());
109 }
110
111 public void testGetRefsHeadsText() throws Exception {
112 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
113 req.setPathInfo("/test/+refs/heads");
114 req.setQueryString("format=TEXT");
115 FakeHttpServletResponse res = new FakeHttpServletResponse();
116 servlet.service(req, res);
117
118 assertEquals(200, res.getStatus());
119 assertEquals(
120 id("refs/heads/branch") + " refs/heads/branch\n"
121 + id("refs/heads/master") + " refs/heads/master\n",
122 res.getActualBodyString());
123 }
124
125 public void testGetRefsHeadsTextTrailingSlash() throws Exception {
126 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
127 req.setPathInfo("/test/+refs/heads/");
128 req.setQueryString("format=TEXT");
129 FakeHttpServletResponse res = new FakeHttpServletResponse();
130 servlet.service(req, res);
131
132 assertEquals(200, res.getStatus());
133 assertEquals(
134 id("refs/heads/branch") + " refs/heads/branch\n"
135 + id("refs/heads/master") + " refs/heads/master\n",
136 res.getActualBodyString());
137 }
138
139 public void testNoHeadText() throws Exception {
140 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
141 req.setPathInfo("/test/+refs/HEAD");
142 req.setQueryString("format=TEXT");
143 FakeHttpServletResponse res = new FakeHttpServletResponse();
144 servlet.service(req, res);
145
146 assertEquals(200, res.getStatus());
147 // /+refs/foo means refs/foo(/*), so this is empty.
148 assertEquals("", res.getActualBodyString());
149 }
150
151 public void testSingleHeadText() throws Exception {
152 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
153 req.setPathInfo("/test/+refs/heads/master");
154 req.setQueryString("format=TEXT");
155 FakeHttpServletResponse res = new FakeHttpServletResponse();
156 servlet.service(req, res);
157
158 assertEquals(200, res.getStatus());
159 assertEquals(
160 id("refs/heads/master") + " refs/heads/master\n",
161 res.getActualBodyString());
162 }
163
164 public void testSinglePeeledTagText() throws Exception {
165 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
166 req.setPathInfo("/test/+refs/tags/atag");
167 req.setQueryString("format=TEXT");
168 FakeHttpServletResponse res = new FakeHttpServletResponse();
169 servlet.service(req, res);
170
171 assertEquals(200, res.getStatus());
172 assertEquals(
173 id("refs/tags/atag") + " refs/tags/atag\n"
174 + peeled("refs/tags/atag") + " refs/tags/atag^{}\n",
175 res.getActualBodyString());
176 }
177}