1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.http.test;
45
46 import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
47 import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
48 import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
49 import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertFalse;
52 import static org.junit.Assert.assertNotNull;
53 import static org.junit.Assert.assertNull;
54 import static org.junit.Assert.assertTrue;
55 import static org.junit.Assert.fail;
56
57 import java.io.IOException;
58 import java.util.Arrays;
59 import java.util.Collection;
60 import java.util.List;
61 import java.util.Map;
62
63 import javax.servlet.http.HttpServletRequest;
64
65 import org.eclipse.jetty.servlet.ServletContextHandler;
66 import org.eclipse.jetty.servlet.ServletHolder;
67 import org.eclipse.jgit.errors.NotSupportedException;
68 import org.eclipse.jgit.errors.RepositoryNotFoundException;
69 import org.eclipse.jgit.http.server.GitServlet;
70 import org.eclipse.jgit.junit.TestRepository;
71 import org.eclipse.jgit.junit.http.AccessEvent;
72 import org.eclipse.jgit.junit.http.HttpTestCase;
73 import org.eclipse.jgit.lib.Constants;
74 import org.eclipse.jgit.lib.NullProgressMonitor;
75 import org.eclipse.jgit.lib.Ref;
76 import org.eclipse.jgit.lib.Repository;
77 import org.eclipse.jgit.revwalk.RevBlob;
78 import org.eclipse.jgit.revwalk.RevCommit;
79 import org.eclipse.jgit.transport.FetchConnection;
80 import org.eclipse.jgit.transport.HttpTransport;
81 import org.eclipse.jgit.transport.Transport;
82 import org.eclipse.jgit.transport.TransportHttp;
83 import org.eclipse.jgit.transport.URIish;
84 import org.eclipse.jgit.transport.http.HttpConnectionFactory;
85 import org.eclipse.jgit.transport.http.JDKHttpConnectionFactory;
86 import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
87 import org.eclipse.jgit.transport.resolver.RepositoryResolver;
88 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
89 import org.junit.Before;
90 import org.junit.Test;
91 import org.junit.runner.RunWith;
92 import org.junit.runners.Parameterized;
93 import org.junit.runners.Parameterized.Parameters;
94
95 @RunWith(Parameterized.class)
96 public class DumbClientSmartServerTest extends HttpTestCase {
97 private Repository remoteRepository;
98
99 private URIish remoteURI;
100
101 private RevBlob A_txt;
102
103 private RevCommit A, B;
104
105 @Parameters
106 public static Collection<Object[]> data() {
107
108 return Arrays.asList(new Object[][] {
109 { new JDKHttpConnectionFactory() },
110 { new HttpClientConnectionFactory() } });
111 }
112
113 public DumbClientSmartServerTest(HttpConnectionFactory cf) {
114 HttpTransport.setConnectionFactory(cf);
115 }
116
117 @Before
118 public void setUp() throws Exception {
119 super.setUp();
120
121 final TestRepository<Repository> src = createTestRepository();
122 final String srcName = src.getRepository().getDirectory().getName();
123
124 ServletContextHandler app = server.addContext("/git");
125 GitServlet gs = new GitServlet();
126 gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
127 public Repository open(HttpServletRequest req, String name)
128 throws RepositoryNotFoundException,
129 ServiceNotEnabledException {
130 if (!name.equals(srcName))
131 throw new RepositoryNotFoundException(name);
132
133 final Repository db = src.getRepository();
134 db.incrementOpen();
135 return db;
136 }
137 });
138 app.addServlet(new ServletHolder(gs), "/*");
139
140 server.setUp();
141
142 remoteRepository = src.getRepository();
143 remoteURI = toURIish(app, srcName);
144
145 A_txt = src.blob("A");
146 A = src.commit().add("A_txt", A_txt).create();
147 B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
148 src.update(master, B);
149 }
150
151 @Test
152 public void testListRemote() throws IOException {
153 Repository dst = createBareRepository();
154
155 assertEquals("http", remoteURI.getScheme());
156
157 Map<String, Ref> map;
158 Transport t = Transport.open(dst, remoteURI);
159 ((TransportHttp) t).setUseSmartHttp(false);
160 try {
161
162
163
164
165 assertTrue("isa TransportHttp", t instanceof TransportHttp);
166 assertTrue("isa HttpTransport", t instanceof HttpTransport);
167
168 FetchConnection c = t.openFetch();
169 try {
170 map = c.getRefsMap();
171 } finally {
172 c.close();
173 }
174 } finally {
175 t.close();
176 }
177
178 assertNotNull("have map of refs", map);
179 assertEquals(2, map.size());
180
181 assertNotNull("has " + master, map.get(master));
182 assertEquals(B, map.get(master).getObjectId());
183
184 assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
185 assertEquals(B, map.get(Constants.HEAD).getObjectId());
186
187 List<AccessEvent> requests = getRequests();
188 assertEquals(2, requests.size());
189 assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
190
191 AccessEvent info = requests.get(0);
192 assertEquals("GET", info.getMethod());
193 assertEquals(join(remoteURI, "info/refs"), info.getPath());
194 assertEquals(0, info.getParameters().size());
195 assertNull("no service parameter", info.getParameter("service"));
196 assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
197 assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
198 assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
199 .startsWith("JGit/"));
200 assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
201 assertEquals(200, info.getStatus());
202 assertEquals("text/plain; charset=UTF-8",
203 info
204 .getResponseHeader(HDR_CONTENT_TYPE));
205
206 AccessEvent head = requests.get(1);
207 assertEquals("GET", head.getMethod());
208 assertEquals(join(remoteURI, "HEAD"), head.getPath());
209 assertEquals(0, head.getParameters().size());
210 assertEquals(200, head.getStatus());
211 assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
212 }
213
214 @Test
215 public void testInitialClone_Small() throws Exception {
216 Repository dst = createBareRepository();
217 assertFalse(dst.hasObject(A_txt));
218
219 Transport t = Transport.open(dst, remoteURI);
220 ((TransportHttp) t).setUseSmartHttp(false);
221 try {
222 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
223 } finally {
224 t.close();
225 }
226
227 assertTrue(dst.hasObject(A_txt));
228 assertEquals(B, dst.exactRef(master).getObjectId());
229 fsck(dst, B);
230
231 List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
232 assertEquals(1, loose.size());
233 assertEquals("GET", loose.get(0).getMethod());
234 assertEquals(0, loose.get(0).getParameters().size());
235 assertEquals(200, loose.get(0).getStatus());
236 assertEquals("application/x-git-loose-object", loose.get(0)
237 .getResponseHeader(HDR_CONTENT_TYPE));
238 }
239
240 @Test
241 public void testInitialClone_Packed() throws Exception {
242 new TestRepository<Repository>(remoteRepository).packAndPrune();
243
244 Repository dst = createBareRepository();
245 assertFalse(dst.hasObject(A_txt));
246
247 Transport t = Transport.open(dst, remoteURI);
248 ((TransportHttp) t).setUseSmartHttp(false);
249 try {
250 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
251 } finally {
252 t.close();
253 }
254
255 assertTrue(dst.hasObject(A_txt));
256 assertEquals(B, dst.exactRef(master).getObjectId());
257 fsck(dst, B);
258
259 List<AccessEvent> req;
260
261 req = getRequests(loose(remoteURI, B));
262 assertEquals(1, req.size());
263 assertEquals("GET", req.get(0).getMethod());
264 assertEquals(0, req.get(0).getParameters().size());
265 assertEquals(404, req.get(0).getStatus());
266
267 req = getRequests(join(remoteURI, "objects/info/packs"));
268 assertEquals(1, req.size());
269 assertEquals("GET", req.get(0).getMethod());
270 assertEquals(0, req.get(0).getParameters().size());
271 assertEquals(200, req.get(0).getStatus());
272 assertEquals("text/plain; charset=UTF-8",
273 req.get(0).getResponseHeader(
274 HDR_CONTENT_TYPE));
275 }
276
277 @Test
278 public void testPushNotSupported() throws Exception {
279 final TestRepository src = createTestRepository();
280 final RevCommit Q = src.commit().create();
281 final Repository db = src.getRepository();
282
283 Transport t = Transport.open(db, remoteURI);
284 ((TransportHttp) t).setUseSmartHttp(false);
285 try {
286 try {
287 t.push(NullProgressMonitor.INSTANCE, push(src, Q));
288 fail("push incorrectly completed against a smart server");
289 } catch (NotSupportedException nse) {
290 String exp = "smart HTTP push disabled";
291 assertEquals(exp, nse.getMessage());
292 }
293 } finally {
294 t.close();
295 }
296 }
297 }