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