You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DumbClientSmartServerTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.http.test;
  44. import static org.eclipse.jgit.util.HttpSupport.HDR_ACCEPT;
  45. import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_TYPE;
  46. import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
  47. import static org.eclipse.jgit.util.HttpSupport.HDR_USER_AGENT;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertFalse;
  50. import static org.junit.Assert.assertNotNull;
  51. import static org.junit.Assert.assertNull;
  52. import static org.junit.Assert.assertTrue;
  53. import static org.junit.Assert.fail;
  54. import java.io.IOException;
  55. import java.util.Arrays;
  56. import java.util.Collection;
  57. import java.util.List;
  58. import java.util.Map;
  59. import javax.servlet.http.HttpServletRequest;
  60. import org.eclipse.jetty.servlet.ServletContextHandler;
  61. import org.eclipse.jetty.servlet.ServletHolder;
  62. import org.eclipse.jgit.errors.NotSupportedException;
  63. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  64. import org.eclipse.jgit.http.server.GitServlet;
  65. import org.eclipse.jgit.junit.TestRepository;
  66. import org.eclipse.jgit.junit.http.AccessEvent;
  67. import org.eclipse.jgit.junit.http.HttpTestCase;
  68. import org.eclipse.jgit.lib.Constants;
  69. import org.eclipse.jgit.lib.NullProgressMonitor;
  70. import org.eclipse.jgit.lib.Ref;
  71. import org.eclipse.jgit.lib.Repository;
  72. import org.eclipse.jgit.revwalk.RevBlob;
  73. import org.eclipse.jgit.revwalk.RevCommit;
  74. import org.eclipse.jgit.transport.FetchConnection;
  75. import org.eclipse.jgit.transport.HttpTransport;
  76. import org.eclipse.jgit.transport.Transport;
  77. import org.eclipse.jgit.transport.TransportHttp;
  78. import org.eclipse.jgit.transport.URIish;
  79. import org.eclipse.jgit.transport.http.HttpConnectionFactory;
  80. import org.eclipse.jgit.transport.http.JDKHttpConnectionFactory;
  81. import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
  82. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  83. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  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. @RunWith(Parameterized.class)
  90. public class DumbClientSmartServerTest extends HttpTestCase {
  91. private Repository remoteRepository;
  92. private URIish remoteURI;
  93. private RevBlob A_txt;
  94. private RevCommit A, B;
  95. @Parameters
  96. public static Collection<Object[]> data() {
  97. // run all tests with both connection factories we have
  98. return Arrays.asList(new Object[][] {
  99. { new JDKHttpConnectionFactory() },
  100. { new HttpClientConnectionFactory() } });
  101. }
  102. public DumbClientSmartServerTest(HttpConnectionFactory cf) {
  103. HttpTransport.setConnectionFactory(cf);
  104. }
  105. @Before
  106. public void setUp() throws Exception {
  107. super.setUp();
  108. final TestRepository<Repository> src = createTestRepository();
  109. final String srcName = src.getRepository().getDirectory().getName();
  110. ServletContextHandler app = server.addContext("/git");
  111. GitServlet gs = new GitServlet();
  112. gs.setRepositoryResolver(new RepositoryResolver<HttpServletRequest>() {
  113. public Repository open(HttpServletRequest req, String name)
  114. throws RepositoryNotFoundException,
  115. ServiceNotEnabledException {
  116. if (!name.equals(srcName))
  117. throw new RepositoryNotFoundException(name);
  118. final Repository db = src.getRepository();
  119. db.incrementOpen();
  120. return db;
  121. }
  122. });
  123. app.addServlet(new ServletHolder(gs), "/*");
  124. server.setUp();
  125. remoteRepository = src.getRepository();
  126. remoteURI = toURIish(app, srcName);
  127. A_txt = src.blob("A");
  128. A = src.commit().add("A_txt", A_txt).create();
  129. B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
  130. src.update(master, B);
  131. }
  132. @Test
  133. public void testListRemote() throws IOException {
  134. Repository dst = createBareRepository();
  135. assertEquals("http", remoteURI.getScheme());
  136. Map<String, Ref> map;
  137. Transport t = Transport.open(dst, remoteURI);
  138. ((TransportHttp) t).setUseSmartHttp(false);
  139. try {
  140. // I didn't make up these public interface names, I just
  141. // approved them for inclusion into the code base. Sorry.
  142. // --spearce
  143. //
  144. assertTrue("isa TransportHttp", t instanceof TransportHttp);
  145. assertTrue("isa HttpTransport", t instanceof HttpTransport);
  146. FetchConnection c = t.openFetch();
  147. try {
  148. map = c.getRefsMap();
  149. } finally {
  150. c.close();
  151. }
  152. } finally {
  153. t.close();
  154. }
  155. assertNotNull("have map of refs", map);
  156. assertEquals(2, map.size());
  157. assertNotNull("has " + master, map.get(master));
  158. assertEquals(B, map.get(master).getObjectId());
  159. assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
  160. assertEquals(B, map.get(Constants.HEAD).getObjectId());
  161. List<AccessEvent> requests = getRequests();
  162. assertEquals(2, requests.size());
  163. assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
  164. AccessEvent info = requests.get(0);
  165. assertEquals("GET", info.getMethod());
  166. assertEquals(join(remoteURI, "info/refs"), info.getPath());
  167. assertEquals(0, info.getParameters().size());
  168. assertNull("no service parameter", info.getParameter("service"));
  169. assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
  170. assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
  171. assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
  172. .startsWith("JGit/"));
  173. assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
  174. assertEquals(200, info.getStatus());
  175. assertEquals("text/plain;charset=utf-8",
  176. info
  177. .getResponseHeader(HDR_CONTENT_TYPE));
  178. AccessEvent head = requests.get(1);
  179. assertEquals("GET", head.getMethod());
  180. assertEquals(join(remoteURI, "HEAD"), head.getPath());
  181. assertEquals(0, head.getParameters().size());
  182. assertEquals(200, head.getStatus());
  183. assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
  184. }
  185. @Test
  186. public void testInitialClone_Small() throws Exception {
  187. Repository dst = createBareRepository();
  188. assertFalse(dst.hasObject(A_txt));
  189. Transport t = Transport.open(dst, remoteURI);
  190. ((TransportHttp) t).setUseSmartHttp(false);
  191. try {
  192. t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
  193. } finally {
  194. t.close();
  195. }
  196. assertTrue(dst.hasObject(A_txt));
  197. assertEquals(B, dst.exactRef(master).getObjectId());
  198. fsck(dst, B);
  199. List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
  200. assertEquals(1, loose.size());
  201. assertEquals("GET", loose.get(0).getMethod());
  202. assertEquals(0, loose.get(0).getParameters().size());
  203. assertEquals(200, loose.get(0).getStatus());
  204. assertEquals("application/x-git-loose-object", loose.get(0)
  205. .getResponseHeader(HDR_CONTENT_TYPE));
  206. }
  207. @Test
  208. public void testInitialClone_Packed() throws Exception {
  209. new TestRepository<Repository>(remoteRepository).packAndPrune();
  210. Repository dst = createBareRepository();
  211. assertFalse(dst.hasObject(A_txt));
  212. Transport t = Transport.open(dst, remoteURI);
  213. ((TransportHttp) t).setUseSmartHttp(false);
  214. try {
  215. t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
  216. } finally {
  217. t.close();
  218. }
  219. assertTrue(dst.hasObject(A_txt));
  220. assertEquals(B, dst.exactRef(master).getObjectId());
  221. fsck(dst, B);
  222. List<AccessEvent> req;
  223. req = getRequests(loose(remoteURI, B));
  224. assertEquals(1, req.size());
  225. assertEquals("GET", req.get(0).getMethod());
  226. assertEquals(0, req.get(0).getParameters().size());
  227. assertEquals(404, req.get(0).getStatus());
  228. req = getRequests(join(remoteURI, "objects/info/packs"));
  229. assertEquals(1, req.size());
  230. assertEquals("GET", req.get(0).getMethod());
  231. assertEquals(0, req.get(0).getParameters().size());
  232. assertEquals(200, req.get(0).getStatus());
  233. assertEquals("text/plain;charset=utf-8",
  234. req.get(0).getResponseHeader(
  235. HDR_CONTENT_TYPE));
  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. Transport t = Transport.open(db, remoteURI);
  243. ((TransportHttp) t).setUseSmartHttp(false);
  244. try {
  245. try {
  246. t.push(NullProgressMonitor.INSTANCE, push(src, Q));
  247. fail("push incorrectly completed against a smart server");
  248. } catch (NotSupportedException nse) {
  249. String exp = "smart HTTP push disabled";
  250. assertEquals(exp, nse.getMessage());
  251. }
  252. } finally {
  253. t.close();
  254. }
  255. }
  256. }