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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 java.io.IOException;
  49. import java.util.List;
  50. import java.util.Map;
  51. import javax.servlet.http.HttpServletRequest;
  52. import org.eclipse.jetty.servlet.ServletContextHandler;
  53. import org.eclipse.jetty.servlet.ServletHolder;
  54. import org.eclipse.jgit.errors.NotSupportedException;
  55. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  56. import org.eclipse.jgit.http.server.GitServlet;
  57. import org.eclipse.jgit.http.server.resolver.RepositoryResolver;
  58. import org.eclipse.jgit.http.server.resolver.ServiceNotEnabledException;
  59. import org.eclipse.jgit.http.test.util.AccessEvent;
  60. import org.eclipse.jgit.http.test.util.HttpTestCase;
  61. import org.eclipse.jgit.junit.TestRepository;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.NullProgressMonitor;
  64. import org.eclipse.jgit.lib.Ref;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.revwalk.RevBlob;
  67. import org.eclipse.jgit.revwalk.RevCommit;
  68. import org.eclipse.jgit.transport.FetchConnection;
  69. import org.eclipse.jgit.transport.HttpTransport;
  70. import org.eclipse.jgit.transport.Transport;
  71. import org.eclipse.jgit.transport.TransportHttp;
  72. import org.eclipse.jgit.transport.URIish;
  73. public class DumbClientSmartServerTest extends HttpTestCase {
  74. private Repository remoteRepository;
  75. private URIish remoteURI;
  76. private RevBlob A_txt;
  77. private RevCommit A, B;
  78. protected void setUp() throws Exception {
  79. super.setUp();
  80. final TestRepository src = createTestRepository();
  81. final String srcName = src.getRepository().getDirectory().getName();
  82. ServletContextHandler app = server.addContext("/git");
  83. GitServlet gs = new GitServlet();
  84. gs.setRepositoryResolver(new RepositoryResolver() {
  85. public Repository open(HttpServletRequest req, String name)
  86. throws RepositoryNotFoundException,
  87. ServiceNotEnabledException {
  88. if (!name.equals(srcName))
  89. throw new RepositoryNotFoundException(name);
  90. final Repository db = src.getRepository();
  91. db.incrementOpen();
  92. return db;
  93. }
  94. });
  95. app.addServlet(new ServletHolder(gs), "/*");
  96. server.setUp();
  97. remoteRepository = src.getRepository();
  98. remoteURI = toURIish(app, srcName);
  99. A_txt = src.blob("A");
  100. A = src.commit().add("A_txt", A_txt).create();
  101. B = src.commit().parent(A).add("A_txt", "C").add("B", "B").create();
  102. src.update(master, B);
  103. }
  104. public void testListRemote() throws IOException {
  105. Repository dst = createBareRepository();
  106. assertEquals("http", remoteURI.getScheme());
  107. Map<String, Ref> map;
  108. Transport t = Transport.open(dst, remoteURI);
  109. ((TransportHttp) t).setUseSmartHttp(false);
  110. try {
  111. // I didn't make up these public interface names, I just
  112. // approved them for inclusion into the code base. Sorry.
  113. // --spearce
  114. //
  115. assertTrue("isa TransportHttp", t instanceof TransportHttp);
  116. assertTrue("isa HttpTransport", t instanceof HttpTransport);
  117. FetchConnection c = t.openFetch();
  118. try {
  119. map = c.getRefsMap();
  120. } finally {
  121. c.close();
  122. }
  123. } finally {
  124. t.close();
  125. }
  126. assertNotNull("have map of refs", map);
  127. assertEquals(2, map.size());
  128. assertNotNull("has " + master, map.get(master));
  129. assertEquals(B, map.get(master).getObjectId());
  130. assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
  131. assertEquals(B, map.get(Constants.HEAD).getObjectId());
  132. List<AccessEvent> requests = getRequests();
  133. assertEquals(2, requests.size());
  134. assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());
  135. AccessEvent info = requests.get(0);
  136. assertEquals("GET", info.getMethod());
  137. assertEquals(join(remoteURI, "info/refs"), info.getPath());
  138. assertEquals(0, info.getParameters().size());
  139. assertNull("no service parameter", info.getParameter("service"));
  140. assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
  141. assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
  142. assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT)
  143. .startsWith("JGit/"));
  144. assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
  145. assertEquals(200, info.getStatus());
  146. assertEquals("text/plain;charset=UTF-8", info
  147. .getResponseHeader(HDR_CONTENT_TYPE));
  148. AccessEvent head = requests.get(1);
  149. assertEquals("GET", head.getMethod());
  150. assertEquals(join(remoteURI, "HEAD"), head.getPath());
  151. assertEquals(0, head.getParameters().size());
  152. assertEquals(200, head.getStatus());
  153. assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
  154. }
  155. public void testInitialClone_Small() throws Exception {
  156. Repository dst = createBareRepository();
  157. assertFalse(dst.hasObject(A_txt));
  158. Transport t = Transport.open(dst, remoteURI);
  159. ((TransportHttp) t).setUseSmartHttp(false);
  160. try {
  161. t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
  162. } finally {
  163. t.close();
  164. }
  165. assertTrue(dst.hasObject(A_txt));
  166. assertEquals(B, dst.getRef(master).getObjectId());
  167. fsck(dst, B);
  168. List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
  169. assertEquals(1, loose.size());
  170. assertEquals("GET", loose.get(0).getMethod());
  171. assertEquals(0, loose.get(0).getParameters().size());
  172. assertEquals(200, loose.get(0).getStatus());
  173. assertEquals("application/x-git-loose-object", loose.get(0)
  174. .getResponseHeader(HDR_CONTENT_TYPE));
  175. }
  176. public void testInitialClone_Packed() throws Exception {
  177. new TestRepository(remoteRepository).packAndPrune();
  178. Repository dst = createBareRepository();
  179. assertFalse(dst.hasObject(A_txt));
  180. Transport t = Transport.open(dst, remoteURI);
  181. ((TransportHttp) t).setUseSmartHttp(false);
  182. try {
  183. t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
  184. } finally {
  185. t.close();
  186. }
  187. assertTrue(dst.hasObject(A_txt));
  188. assertEquals(B, dst.getRef(master).getObjectId());
  189. fsck(dst, B);
  190. List<AccessEvent> req;
  191. req = getRequests(loose(remoteURI, B));
  192. assertEquals(1, req.size());
  193. assertEquals("GET", req.get(0).getMethod());
  194. assertEquals(0, req.get(0).getParameters().size());
  195. assertEquals(404, req.get(0).getStatus());
  196. req = getRequests(join(remoteURI, "objects/info/packs"));
  197. assertEquals(1, req.size());
  198. assertEquals("GET", req.get(0).getMethod());
  199. assertEquals(0, req.get(0).getParameters().size());
  200. assertEquals(200, req.get(0).getStatus());
  201. assertEquals("text/plain;charset=UTF-8", req.get(0).getResponseHeader(
  202. HDR_CONTENT_TYPE));
  203. }
  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. Transport t = Transport.open(db, remoteURI);
  209. ((TransportHttp) t).setUseSmartHttp(false);
  210. try {
  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. } finally {
  219. t.close();
  220. }
  221. }
  222. }