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.

HttpClientTests.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2009-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 java.io.File;
  45. import java.net.URI;
  46. import java.util.List;
  47. import javax.servlet.http.HttpServletRequest;
  48. import org.eclipse.jetty.servlet.DefaultServlet;
  49. import org.eclipse.jetty.servlet.ServletContextHandler;
  50. import org.eclipse.jetty.servlet.ServletHolder;
  51. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  52. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.http.server.GitServlet;
  55. import org.eclipse.jgit.http.server.resolver.RepositoryResolver;
  56. import org.eclipse.jgit.http.server.resolver.ServiceNotEnabledException;
  57. import org.eclipse.jgit.http.test.util.AccessEvent;
  58. import org.eclipse.jgit.http.test.util.HttpTestCase;
  59. import org.eclipse.jgit.junit.TestRepository;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.Ref;
  62. import org.eclipse.jgit.lib.RefUpdate;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.revwalk.RevCommit;
  65. import org.eclipse.jgit.transport.FetchConnection;
  66. import org.eclipse.jgit.transport.Transport;
  67. import org.eclipse.jgit.transport.URIish;
  68. public class HttpClientTests extends HttpTestCase {
  69. private TestRepository remoteRepository;
  70. private URIish dumbAuthNoneURI;
  71. private URIish dumbAuthBasicURI;
  72. private URIish smartAuthNoneURI;
  73. private URIish smartAuthBasicURI;
  74. protected void setUp() throws Exception {
  75. super.setUp();
  76. remoteRepository = createTestRepository();
  77. remoteRepository.update(master, remoteRepository.commit().create());
  78. ServletContextHandler dNone = dumb("/dnone");
  79. ServletContextHandler dBasic = server.authBasic(dumb("/dbasic"));
  80. ServletContextHandler sNone = smart("/snone");
  81. ServletContextHandler sBasic = server.authBasic(smart("/sbasic"));
  82. server.setUp();
  83. final String srcName = nameOf(remoteRepository);
  84. dumbAuthNoneURI = toURIish(dNone, srcName);
  85. dumbAuthBasicURI = toURIish(dBasic, srcName);
  86. smartAuthNoneURI = toURIish(sNone, srcName);
  87. smartAuthBasicURI = toURIish(sBasic, srcName);
  88. }
  89. private ServletContextHandler dumb(final String path) {
  90. final File srcGit = remoteRepository.getRepository().getDirectory();
  91. final URI base = srcGit.getParentFile().toURI();
  92. ServletContextHandler ctx = server.addContext(path);
  93. ctx.setResourceBase(base.toString());
  94. ctx.addServlet(DefaultServlet.class, "/");
  95. return ctx;
  96. }
  97. private ServletContextHandler smart(final String path) {
  98. GitServlet gs = new GitServlet();
  99. gs.setRepositoryResolver(new RepositoryResolver() {
  100. public Repository open(HttpServletRequest req, String name)
  101. throws RepositoryNotFoundException,
  102. ServiceNotEnabledException {
  103. if (!name.equals(nameOf(remoteRepository)))
  104. throw new RepositoryNotFoundException(name);
  105. final Repository db = remoteRepository.getRepository();
  106. db.incrementOpen();
  107. return db;
  108. }
  109. });
  110. ServletContextHandler ctx = server.addContext(path);
  111. ctx.addServlet(new ServletHolder(gs), "/*");
  112. return ctx;
  113. }
  114. private static String nameOf(final TestRepository db) {
  115. return db.getRepository().getDirectory().getName();
  116. }
  117. public void testRepositoryNotFound_Dumb() throws Exception {
  118. URIish uri = toURIish("/dumb.none/not-found");
  119. Repository dst = createBareRepository();
  120. Transport t = Transport.open(dst, uri);
  121. try {
  122. try {
  123. t.openFetch();
  124. fail("connection opened to not found repository");
  125. } catch (NoRemoteRepositoryException err) {
  126. String exp = uri + ": " + uri
  127. + "/info/refs?service=git-upload-pack not found";
  128. assertEquals(exp, err.getMessage());
  129. }
  130. } finally {
  131. t.close();
  132. }
  133. }
  134. public void testRepositoryNotFound_Smart() throws Exception {
  135. URIish uri = toURIish("/smart.none/not-found");
  136. Repository dst = createBareRepository();
  137. Transport t = Transport.open(dst, uri);
  138. try {
  139. try {
  140. t.openFetch();
  141. fail("connection opened to not found repository");
  142. } catch (NoRemoteRepositoryException err) {
  143. String exp = uri + ": " + uri
  144. + "/info/refs?service=git-upload-pack not found";
  145. assertEquals(exp, err.getMessage());
  146. }
  147. } finally {
  148. t.close();
  149. }
  150. }
  151. public void testListRemote_Dumb_DetachedHEAD() throws Exception {
  152. Repository src = remoteRepository.getRepository();
  153. RefUpdate u = src.updateRef(Constants.HEAD, true);
  154. RevCommit Q = remoteRepository.commit().message("Q").create();
  155. u.setNewObjectId(Q);
  156. assertEquals(RefUpdate.Result.FORCED, u.forceUpdate());
  157. Repository dst = createBareRepository();
  158. Ref head;
  159. Transport t = Transport.open(dst, dumbAuthNoneURI);
  160. try {
  161. FetchConnection c = t.openFetch();
  162. try {
  163. head = c.getRef(Constants.HEAD);
  164. } finally {
  165. c.close();
  166. }
  167. } finally {
  168. t.close();
  169. }
  170. assertNotNull("has " + Constants.HEAD, head);
  171. assertEquals(Q, head.getObjectId());
  172. }
  173. public void testListRemote_Dumb_NoHEAD() throws Exception {
  174. Repository src = remoteRepository.getRepository();
  175. File headref = new File(src.getDirectory(), Constants.HEAD);
  176. assertTrue("HEAD used to be present", headref.delete());
  177. assertFalse("HEAD is gone", headref.exists());
  178. Repository dst = createBareRepository();
  179. Ref head;
  180. Transport t = Transport.open(dst, dumbAuthNoneURI);
  181. try {
  182. FetchConnection c = t.openFetch();
  183. try {
  184. head = c.getRef(Constants.HEAD);
  185. } finally {
  186. c.close();
  187. }
  188. } finally {
  189. t.close();
  190. }
  191. assertNull("has no " + Constants.HEAD, head);
  192. }
  193. public void testListRemote_Smart_DetachedHEAD() throws Exception {
  194. Repository src = remoteRepository.getRepository();
  195. RefUpdate u = src.updateRef(Constants.HEAD, true);
  196. RevCommit Q = remoteRepository.commit().message("Q").create();
  197. u.setNewObjectId(Q);
  198. assertEquals(RefUpdate.Result.FORCED, u.forceUpdate());
  199. Repository dst = createBareRepository();
  200. Ref head;
  201. Transport t = Transport.open(dst, smartAuthNoneURI);
  202. try {
  203. FetchConnection c = t.openFetch();
  204. try {
  205. head = c.getRef(Constants.HEAD);
  206. } finally {
  207. c.close();
  208. }
  209. } finally {
  210. t.close();
  211. }
  212. assertNotNull("has " + Constants.HEAD, head);
  213. assertEquals(Q, head.getObjectId());
  214. }
  215. public void testListRemote_Smart_WithQueryParameters() throws Exception {
  216. URIish myURI = toURIish("/snone/do?r=1&p=test.git");
  217. Repository dst = createBareRepository();
  218. Transport t = Transport.open(dst, myURI);
  219. try {
  220. try {
  221. t.openFetch();
  222. fail("test did not fail to find repository as expected");
  223. } catch (NoRemoteRepositoryException err) {
  224. // expected
  225. }
  226. } finally {
  227. t.close();
  228. }
  229. List<AccessEvent> requests = getRequests();
  230. assertEquals(1, requests.size());
  231. AccessEvent info = requests.get(0);
  232. assertEquals("GET", info.getMethod());
  233. assertEquals("/snone/do", info.getPath());
  234. assertEquals(3, info.getParameters().size());
  235. assertEquals("1", info.getParameter("r"));
  236. assertEquals("test.git/info/refs", info.getParameter("p"));
  237. assertEquals("git-upload-pack", info.getParameter("service"));
  238. assertEquals(404, info.getStatus());
  239. }
  240. public void testListRemote_Dumb_NeedsAuth() throws Exception {
  241. Repository dst = createBareRepository();
  242. Transport t = Transport.open(dst, dumbAuthBasicURI);
  243. try {
  244. try {
  245. t.openFetch();
  246. fail("connection opened even info/refs needs auth basic");
  247. } catch (TransportException err) {
  248. String status = "401 Unauthorized";
  249. String exp = dumbAuthBasicURI + ": " + status;
  250. assertEquals(exp, err.getMessage());
  251. }
  252. } finally {
  253. t.close();
  254. }
  255. }
  256. public void testListRemote_Smart_UploadPackNeedsAuth() throws Exception {
  257. Repository dst = createBareRepository();
  258. Transport t = Transport.open(dst, smartAuthBasicURI);
  259. try {
  260. try {
  261. t.openFetch();
  262. fail("connection opened even though service disabled");
  263. } catch (TransportException err) {
  264. String status = "401 Unauthorized";
  265. String exp = smartAuthBasicURI + ": " + status;
  266. assertEquals(exp, err.getMessage());
  267. }
  268. } finally {
  269. t.close();
  270. }
  271. }
  272. public void testListRemote_Smart_UploadPackDisabled() throws Exception {
  273. Repository src = remoteRepository.getRepository();
  274. src.getConfig().setBoolean("http", null, "uploadpack", false);
  275. src.getConfig().save();
  276. Repository dst = createBareRepository();
  277. Transport t = Transport.open(dst, smartAuthNoneURI);
  278. try {
  279. try {
  280. t.openFetch();
  281. fail("connection opened even though service disabled");
  282. } catch (TransportException err) {
  283. String exp = smartAuthNoneURI
  284. + ": git-upload-pack not permitted";
  285. assertEquals(exp, err.getMessage());
  286. }
  287. } finally {
  288. t.close();
  289. }
  290. }
  291. }