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.

DumbClientDumbServerTest.java 9.4KB

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