Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HttpClientTests.java 10KB

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