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 org.eclipse.jetty.servlet.ServletContextHandler;
64 import org.eclipse.jetty.servlet.ServletHolder;
65 import org.eclipse.jgit.errors.NotSupportedException;
66 import org.eclipse.jgit.http.server.GitServlet;
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 DumbClientSmartServerTest 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 DumbClientSmartServerTest(HttpConnectionFactory cf) {
109 HttpTransport.setConnectionFactory(cf);
110 }
111
112 @Override
113 @Before
114 public void setUp() throws Exception {
115 super.setUp();
116
117 final TestRepository<Repository> src = createTestRepository();
118 final String srcName = src.getRepository().getDirectory().getName();
119
120 ServletContextHandler app = server.addContext("/git");
121 GitServlet gs = new GitServlet();
122 gs.setRepositoryResolver(new TestRepositoryResolver(src, srcName));
123 app.addServlet(new ServletHolder(gs), "/*");
124
125 server.setUp();
126
127 remoteRepository = src.getRepository();
128 remoteURI = toURIish(app, srcName);
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 ((TransportHttp) t).setUseSmartHttp(false);
145
146
147
148
149 assertTrue("isa TransportHttp", t instanceof TransportHttp);
150 assertTrue("isa HttpTransport", t instanceof HttpTransport);
151
152 try (FetchConnection c = t.openFetch()) {
153 map = c.getRefsMap();
154 }
155 }
156
157 assertNotNull("have map of refs", map);
158 assertEquals(2, map.size());
159
160 assertNotNull("has " + master, map.get(master));
161 assertEquals(B, map.get(master).getObjectId());
162
163 assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
164 assertEquals(B, map.get(Constants.HEAD).getObjectId());
165
166 List<AccessEvent> requests = getRequests();
167 assertEquals(2, requests.size());
168 assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
169
170 AccessEvent info = requests.get(0);
171 assertEquals("GET", info.getMethod());
172 assertEquals(join(remoteURI, "info/refs"), info.getPath());
173 assertEquals(0, info.getParameters().size());
174 assertNull("no service parameter", info.getParameter("service"));
175 assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
176 assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
177 assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
178 .startsWith("JGit/"));
179 assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
180 assertEquals(200, info.getStatus());
181 assertEquals("text/plain;charset=utf-8",
182 info
183 .getResponseHeader(HDR_CONTENT_TYPE));
184
185 AccessEvent head = requests.get(1);
186 assertEquals("GET", head.getMethod());
187 assertEquals(join(remoteURI, "HEAD"), head.getPath());
188 assertEquals(0, head.getParameters().size());
189 assertEquals(200, head.getStatus());
190 assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
191 }
192
193 @Test
194 public void testInitialClone_Small() throws Exception {
195 Repository dst = createBareRepository();
196 assertFalse(dst.hasObject(A_txt));
197
198 try (Transport t = Transport.open(dst, remoteURI)) {
199 ((TransportHttp) t).setUseSmartHttp(false);
200 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
201 }
202
203 assertTrue(dst.hasObject(A_txt));
204 assertEquals(B, dst.exactRef(master).getObjectId());
205 fsck(dst, B);
206
207 List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
208 assertEquals(1, loose.size());
209 assertEquals("GET", loose.get(0).getMethod());
210 assertEquals(0, loose.get(0).getParameters().size());
211 assertEquals(200, loose.get(0).getStatus());
212 assertEquals("application/x-git-loose-object", loose.get(0)
213 .getResponseHeader(HDR_CONTENT_TYPE));
214 }
215
216 @Test
217 public void testInitialClone_Packed() throws Exception {
218 new TestRepository<>(remoteRepository).packAndPrune();
219
220 Repository dst = createBareRepository();
221 assertFalse(dst.hasObject(A_txt));
222
223 try (Transport t = Transport.open(dst, remoteURI)) {
224 ((TransportHttp) t).setUseSmartHttp(false);
225 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
226 }
227
228 assertTrue(dst.hasObject(A_txt));
229 assertEquals(B, dst.exactRef(master).getObjectId());
230 fsck(dst, B);
231
232 List<AccessEvent> req;
233
234 req = getRequests(loose(remoteURI, B));
235 assertEquals(1, req.size());
236 assertEquals("GET", req.get(0).getMethod());
237 assertEquals(0, req.get(0).getParameters().size());
238 assertEquals(404, req.get(0).getStatus());
239
240 req = getRequests(join(remoteURI, "objects/info/packs"));
241 assertEquals(1, req.size());
242 assertEquals("GET", req.get(0).getMethod());
243 assertEquals(0, req.get(0).getParameters().size());
244 assertEquals(200, req.get(0).getStatus());
245 assertEquals("text/plain;charset=utf-8",
246 req.get(0).getResponseHeader(
247 HDR_CONTENT_TYPE));
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 ((TransportHttp) t).setUseSmartHttp(false);
258 try {
259 t.push(NullProgressMonitor.INSTANCE, push(src, Q));
260 fail("push incorrectly completed against a smart server");
261 } catch (NotSupportedException nse) {
262 String exp = "smart HTTP push disabled";
263 assertEquals(exp, nse.getMessage());
264 }
265 }
266 }
267 }