1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.test;
12
13 import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
14 import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
15 import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
16 import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.jetty.servlet.ServletContextHandler;
29 import org.eclipse.jetty.servlet.ServletHolder;
30 import org.eclipse.jgit.errors.NotSupportedException;
31 import org.eclipse.jgit.http.server.GitServlet;
32 import org.eclipse.jgit.junit.TestRepository;
33 import org.eclipse.jgit.junit.http.AccessEvent;
34 import org.eclipse.jgit.lib.Constants;
35 import org.eclipse.jgit.lib.NullProgressMonitor;
36 import org.eclipse.jgit.lib.Ref;
37 import org.eclipse.jgit.lib.Repository;
38 import org.eclipse.jgit.revwalk.RevBlob;
39 import org.eclipse.jgit.revwalk.RevCommit;
40 import org.eclipse.jgit.transport.FetchConnection;
41 import org.eclipse.jgit.transport.HttpTransport;
42 import org.eclipse.jgit.transport.Transport;
43 import org.eclipse.jgit.transport.TransportHttp;
44 import org.eclipse.jgit.transport.URIish;
45 import org.eclipse.jgit.transport.http.HttpConnectionFactory;
46 import org.junit.Before;
47 import org.junit.Test;
48
49 public class DumbClientSmartServerTest extends AllFactoriesHttpTestCase {
50 private Repository remoteRepository;
51
52 private URIish remoteURI;
53
54 private RevBlob A_txt;
55
56 private RevCommit A, B;
57
58 public DumbClientSmartServerTest(HttpConnectionFactory cf) {
59 super(cf);
60 }
61
62 @Override
63 @Before
64 public void setUp() throws Exception {
65 super.setUp();
66
67 final TestRepository<Repository> src = createTestRepository();
68 final String srcName = src.getRepository().getDirectory().getName();
69
70 ServletContextHandler app = server.addContext("/git");
71 GitServlet gs = new GitServlet();
72 gs.setRepositoryResolver(new TestRepositoryResolver(src, srcName));
73 app.addServlet(new ServletHolder(gs), "/*");
74
75 server.setUp();
76
77 remoteRepository = src.getRepository();
78 remoteURI = toURIish(app, srcName);
79
80 A_txt = src.blob("A");
81 A = src.commit().add("A_txt", A_txt).create();
82 B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
83 src.update(master, B);
84 }
85
86 @Test
87 public void testListRemote() throws IOException {
88 Repository dst = createBareRepository();
89
90 assertEquals("http", remoteURI.getScheme());
91
92 Map<String, Ref> map;
93 try (Transport t = Transport.open(dst, remoteURI)) {
94 ((TransportHttp) t).setUseSmartHttp(false);
95
96
97
98
99 assertTrue("isa TransportHttp", t instanceof TransportHttp);
100 assertTrue("isa HttpTransport", t instanceof HttpTransport);
101
102 try (FetchConnection c = t.openFetch()) {
103 map = c.getRefsMap();
104 }
105 }
106
107 assertNotNull("have map of refs", map);
108 assertEquals(2, map.size());
109
110 assertNotNull("has " + master, map.get(master));
111 assertEquals(B, map.get(master).getObjectId());
112
113 assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
114 assertEquals(B, map.get(Constants.HEAD).getObjectId());
115
116 List<AccessEvent> requests = getRequests();
117 assertEquals(2, requests.size());
118 assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
119
120 AccessEvent info = requests.get(0);
121 assertEquals("GET", info.getMethod());
122 assertEquals(join(remoteURI, "info/refs"), info.getPath());
123 assertEquals(0, info.getParameters().size());
124 assertNull("no service parameter", info.getParameter("service"));
125 assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
126 assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
127 assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
128 .startsWith("JGit/"));
129 assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
130 assertEquals(200, info.getStatus());
131 assertEquals("text/plain;charset=utf-8",
132 info
133 .getResponseHeader(HDR_CONTENT_TYPE));
134
135 AccessEvent head = requests.get(1);
136 assertEquals("GET", head.getMethod());
137 assertEquals(join(remoteURI, "HEAD"), head.getPath());
138 assertEquals(0, head.getParameters().size());
139 assertEquals(200, head.getStatus());
140 assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
141 }
142
143 @Test
144 public void testInitialClone_Small() throws Exception {
145 Repository dst = createBareRepository();
146 assertFalse(dst.getObjectDatabase().has(A_txt));
147
148 try (Transport t = Transport.open(dst, remoteURI)) {
149 ((TransportHttp) t).setUseSmartHttp(false);
150 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
151 }
152
153 assertTrue(dst.getObjectDatabase().has(A_txt));
154 assertEquals(B, dst.exactRef(master).getObjectId());
155 fsck(dst, B);
156
157 List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
158 assertEquals(1, loose.size());
159 assertEquals("GET", loose.get(0).getMethod());
160 assertEquals(0, loose.get(0).getParameters().size());
161 assertEquals(200, loose.get(0).getStatus());
162 assertEquals("application/x-git-loose-object", loose.get(0)
163 .getResponseHeader(HDR_CONTENT_TYPE));
164 }
165
166 @Test
167 public void testInitialClone_Packed() throws Exception {
168 try (TestRepository<Repository> tr = new TestRepository<>(
169 remoteRepository)) {
170 tr.packAndPrune();
171 }
172
173 Repository dst = createBareRepository();
174 assertFalse(dst.getObjectDatabase().has(A_txt));
175
176 try (Transport t = Transport.open(dst, remoteURI)) {
177 ((TransportHttp) t).setUseSmartHttp(false);
178 t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
179 }
180
181 assertTrue(dst.getObjectDatabase().has(A_txt));
182 assertEquals(B, dst.exactRef(master).getObjectId());
183 fsck(dst, B);
184
185 List<AccessEvent> req;
186
187 req = getRequests(loose(remoteURI, B));
188 assertEquals(1, req.size());
189 assertEquals("GET", req.get(0).getMethod());
190 assertEquals(0, req.get(0).getParameters().size());
191 assertEquals(404, req.get(0).getStatus());
192
193 req = getRequests(join(remoteURI, "objects/info/packs"));
194 assertEquals(1, req.size());
195 assertEquals("GET", req.get(0).getMethod());
196 assertEquals(0, req.get(0).getParameters().size());
197 assertEquals(200, req.get(0).getStatus());
198 assertEquals("text/plain;charset=utf-8",
199 req.get(0).getResponseHeader(
200 HDR_CONTENT_TYPE));
201 }
202
203 @Test
204 public void testPushNotSupported() throws Exception {
205 final TestRepository src = createTestRepository();
206 final RevCommit Q = src.commit().create();
207 final Repository db = src.getRepository();
208
209 try (Transport t = Transport.open(db, remoteURI)) {
210 ((TransportHttp) t).setUseSmartHttp(false);
211 try {
212 t.push(NullProgressMonitor.INSTANCE, push(src, Q));
213 fail("push incorrectly completed against a smart server");
214 } catch (NotSupportedException nse) {
215 String exp = "smart HTTP push disabled";
216 assertEquals(exp, nse.getMessage());
217 }
218 }
219 }
220 }