Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DumbClientDumbServerTest.java 8.3KB

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