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_PRAGMA;
48 import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertFalse;
51 import static org.junit.Assert.assertNotNull;
52 import static org.junit.Assert.assertTrue;
53 import static org.junit.Assert.fail;
54
55 import java.io.File;
56 import java.io.IOException;
57 import java.net.URI;
58 import java.util.Arrays;
59 import java.util.Collection;
60 import java.util.List;
61 import java.util.Map;
62
63 import org.eclipse.jetty.servlet.DefaultServlet;
64 import org.eclipse.jetty.servlet.ServletContextHandler;
65 import org.eclipse.jetty.servlet.ServletHolder;
66 import org.eclipse.jgit.errors.NotSupportedException;
67 import org.eclipse.jgit.junit.TestRepository;
68 import org.eclipse.jgit.junit.http.AccessEvent;
69 import org.eclipse.jgit.junit.http.HttpTestCase;
70 import org.eclipse.jgit.lib.Constants;
71 import org.eclipse.jgit.lib.NullProgressMonitor;
72 import org.eclipse.jgit.lib.Ref;
73 import org.eclipse.jgit.lib.Repository;
74 import org.eclipse.jgit.revwalk.RevBlob;
75 import org.eclipse.jgit.revwalk.RevCommit;
76 import org.eclipse.jgit.transport.FetchConnection;
77 import org.eclipse.jgit.transport.HttpTransport;
78 import org.eclipse.jgit.transport.Transport;
79 import org.eclipse.jgit.transport.TransportHttp;
80 import org.eclipse.jgit.transport.URIish;
81 import org.eclipse.jgit.transport.http.HttpConnectionFactory;
82 import org.eclipse.jgit.transport.http.JDKHttpConnectionFactory;
83 import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
84 import org.junit.Before;
85 import org.junit.Test;
86 import org.junit.runner.RunWith;
87 import org.junit.runners.Parameterized;
88 import org.junit.runners.Parameterized.Parameters;
89
90 @RunWith(Parameterized.class)
91 public class DumbClientDumbServerTest extends HttpTestCase {
92 private Repository remoteRepository;
93
94 private URIish remoteURI;
95
96 private RevBlob A_txt;
97
98 private RevCommit A, B;
99
100 @Parameters
101 public static Collection<Object[]> data() {
102
103 return Arrays.asList(new Object[][] {
104 { new JDKHttpConnectionFactory() },
105 { new HttpClientConnectionFactory() } });
106 }
107
108 public DumbClientDumbServerTest(HttpConnectionFactory cf) {
109 HttpTransport.setConnectionFactory(cf);
110 }
111
112 @Before
113 public void setUp() throws Exception {
114 super.setUp();
115
116 final TestRepository<Repository> src = createTestRepository();
117 final File srcGit = src.getRepository().getDirectory();
118 final URI base = srcGit.getParentFile().toURI();
119
120 ServletContextHandler app = server.addContext("/git");
121 app.setResourceBase(base.toString());
122 ServletHolder holder = app.addServlet(DefaultServlet.class, "/");
123
124 holder.setInitParameter("aliases", "true");
125 server.setUp();
126
127 remoteRepository = src.getRepository();
128 remoteURI = toURIish(app, srcGit.getName());
129
130 A_txt = src.blob("A");
131 A = src.commit().add("A_txt", A_txt).create();
132 B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
133 src.update(master, B);
134 }
135
136 @Test
137 public void testListRemote() throws IOException {
138 Repository dst = createBareRepository();
139
140 assertEquals("http", remoteURI.getScheme());
141
142 Map<String, Ref> map;
143 try (Transport t = Transport.open(dst, remoteURI)) {
144
145
146
147
148 assertTrue("isa TransportHttp", t instanceof TransportHttp);
149 assertTrue("isa HttpTransport", t instanceof HttpTransport);
150
151 try (FetchConnection c = t.openFetch()) {
152 map = c.getRefsMap();
153 }
154 }
155
156 assertNotNull("have map of refs", map);
157 assertEquals(2, map.size());
158
159 assertNotNull("has " + master, map.get(master));
160 assertEquals(B, map.get(master).getObjectId());
161
162 assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
163 assertEquals(B, map.get(Constants.HEAD).getObjectId());
164
165 List<AccessEvent> requests = getRequests();
166 assertEquals(2, requests.size());
167 assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
168
169 AccessEvent info = requests.get(0);
170 assertEquals("GET", info.getMethod());
171 assertEquals(join(remoteURI, "info/refs"), info.getPath());
172 assertEquals(1, info.getParameters().size());
173 assertEquals("git-upload-pack", info.getParameter("service"));
174 assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
175 assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
176 assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
177 .startsWith("JGit/"));
178 assertEquals("application/x-git-upload-pack-advertisement, */*", info
179 .getRequestHeader(HDR_ACCEPT));
180 assertEquals(200, info.getStatus());
181
182 AccessEvent head = requests.get(1);
183 assertEquals("GET", head.getMethod());
184 assertEquals(join(remoteURI, "HEAD"), head.getPath());
185 assertEquals(0, head.getParameters().size());
186 assertEquals("no-cache", head.getRequestHeader(HDR_PRAGMA));
187 assertNotNull("has user-agent", head.getRequestHeader(HDR_USER_AGENT));
188 assertTrue("is jgit agent", head.getRequestHeader(HDR_USER_AGENT)
189 .startsWith("JGit/"));
190 assertEquals(200, head.getStatus());
191 }
192
193 @Test
194 public void testInitialClone_Loose() throws Exception {
195 Repository dst = createBareRepository();
196 assertFalse(dst.hasObject(A_txt));
197
198 try (Transport t = Transport.open(dst, remoteURI)) {
199 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
200 }
201
202 assertTrue(dst.hasObject(A_txt));
203 assertEquals(B, dst.exactRef(master).getObjectId());
204 fsck(dst, B);
205
206 List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
207 assertEquals(1, loose.size());
208 assertEquals("GET", loose.get(0).getMethod());
209 assertEquals(0, loose.get(0).getParameters().size());
210 assertEquals(200, loose.get(0).getStatus());
211 }
212
213 @Test
214 public void testInitialClone_Packed() throws Exception {
215 new TestRepository<Repository>(remoteRepository).packAndPrune();
216
217 Repository dst = createBareRepository();
218 assertFalse(dst.hasObject(A_txt));
219
220 try (Transport t = Transport.open(dst, remoteURI)) {
221 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
222 }
223
224 assertTrue(dst.hasObject(A_txt));
225 assertEquals(B, dst.exactRef(master).getObjectId());
226 fsck(dst, B);
227
228 List<AccessEvent> req;
229 AccessEvent event;
230
231 req = getRequests(loose(remoteURI, B));
232 assertEquals(1, req.size());
233 event = req.get(0);
234 assertEquals("GET", event.getMethod());
235 assertEquals(0, event.getParameters().size());
236 assertEquals(404, event.getStatus());
237
238 req = getRequests(join(remoteURI, "objects/info/packs"));
239 assertEquals(1, req.size());
240 event = req.get(0);
241 assertEquals("GET", event.getMethod());
242 assertEquals(0, event.getParameters().size());
243 assertEquals("no-cache", event.getRequestHeader(HDR_PRAGMA));
244 assertNotNull("has user-agent", event.getRequestHeader(HDR_USER_AGENT));
245 assertTrue("is jgit agent", event.getRequestHeader(HDR_USER_AGENT)
246 .startsWith("JGit/"));
247 assertEquals(200, event.getStatus());
248 }
249
250 @Test
251 public void testPushNotSupported() throws Exception {
252 final TestRepository src = createTestRepository();
253 final RevCommit Q = src.commit().create();
254 final Repository db = src.getRepository();
255
256 try (Transport t = Transport.open(db, remoteURI)) {
257 try {
258 t.push(NullProgressMonitor.INSTANCE, push(src, Q));
259 fail("push incorrectly completed against a dumb server");
260 } catch (NotSupportedException nse) {
261 String exp = "remote does not support smart HTTP push";
262 assertEquals(exp, nse.getMessage());
263 }
264 }
265 }
266 }