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.

HttpTestCase.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2009-2017, 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.junit.http;
  44. import static org.junit.Assert.fail;
  45. import java.io.IOException;
  46. import java.net.URI;
  47. import java.net.URISyntaxException;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.HashSet;
  51. import java.util.List;
  52. import java.util.Set;
  53. import org.eclipse.jetty.servlet.ServletContextHandler;
  54. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  55. import org.eclipse.jgit.junit.TestRepository;
  56. import org.eclipse.jgit.lib.AnyObjectId;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.revwalk.RevObject;
  62. import org.eclipse.jgit.transport.RefSpec;
  63. import org.eclipse.jgit.transport.RemoteRefUpdate;
  64. import org.eclipse.jgit.transport.URIish;
  65. /**
  66. * Base class for HTTP related transport testing.
  67. */
  68. public abstract class HttpTestCase extends LocalDiskRepositoryTestCase {
  69. /** Constant <code>master="Constants.R_HEADS + Constants.MASTER"</code> */
  70. protected static final String master = Constants.R_HEADS + Constants.MASTER;
  71. /** In-memory application server; subclass must start. */
  72. protected AppServer server;
  73. /** {@inheritDoc} */
  74. @Override
  75. public void setUp() throws Exception {
  76. super.setUp();
  77. server = createServer();
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public void tearDown() throws Exception {
  82. server.tearDown();
  83. super.tearDown();
  84. }
  85. /**
  86. * Create the {@link AppServer}.This default implementation creates a server
  87. * without SSLsupport listening for HTTP connections on a dynamically chosen
  88. * port, which can be gotten once the server has been started via its
  89. * {@link org.eclipse.jgit.junit.http.AppServer#getPort()} method.
  90. * Subclasses may override if they need a more specialized server.
  91. *
  92. * @return the {@link org.eclipse.jgit.junit.http.AppServer}.
  93. * @since 4.9
  94. */
  95. protected AppServer createServer() {
  96. return new AppServer();
  97. }
  98. /**
  99. * Create TestRepository
  100. *
  101. * @return the TestRepository
  102. * @throws IOException
  103. */
  104. protected TestRepository<Repository> createTestRepository()
  105. throws IOException {
  106. return new TestRepository<>(createBareRepository());
  107. }
  108. /**
  109. * Convert path to URIish
  110. *
  111. * @param path
  112. * @return the URIish
  113. * @throws URISyntaxException
  114. */
  115. protected URIish toURIish(String path) throws URISyntaxException {
  116. URI u = server.getURI().resolve(path);
  117. return new URIish(u.toString());
  118. }
  119. /**
  120. * Convert a path relative to the app's context path to a URIish
  121. *
  122. * @param app
  123. * @param name
  124. * @return the warnings (if any) from the last execution
  125. * @throws URISyntaxException
  126. */
  127. protected URIish toURIish(ServletContextHandler app, String name)
  128. throws URISyntaxException {
  129. String p = app.getContextPath();
  130. if (!p.endsWith("/") && !name.startsWith("/"))
  131. p += "/";
  132. p += name;
  133. return toURIish(p);
  134. }
  135. /**
  136. * Get requests.
  137. *
  138. * @return list of events
  139. */
  140. protected List<AccessEvent> getRequests() {
  141. return server.getRequests();
  142. }
  143. /**
  144. * Get requests.
  145. *
  146. * @param base
  147. * @param path
  148. *
  149. * @return list of events
  150. */
  151. protected List<AccessEvent> getRequests(URIish base, String path) {
  152. return server.getRequests(base, path);
  153. }
  154. /**
  155. * Get requests.
  156. *
  157. * @param path
  158. *
  159. * @return list of events
  160. */
  161. protected List<AccessEvent> getRequests(String path) {
  162. return server.getRequests(path);
  163. }
  164. /**
  165. * Run fsck
  166. *
  167. * @param db
  168. * @param tips
  169. * @throws Exception
  170. */
  171. protected static void fsck(Repository db, RevObject... tips)
  172. throws Exception {
  173. try (TestRepository<? extends Repository> tr =
  174. new TestRepository<>(db)) {
  175. tr.fsck(tips);
  176. }
  177. }
  178. /**
  179. * Mirror refs
  180. *
  181. * @param refs
  182. * @return set of RefSpecs
  183. */
  184. protected static Set<RefSpec> mirror(String... refs) {
  185. HashSet<RefSpec> r = new HashSet<>();
  186. for (String name : refs) {
  187. RefSpec rs = new RefSpec(name);
  188. rs = rs.setDestination(name);
  189. rs = rs.setForceUpdate(true);
  190. r.add(rs);
  191. }
  192. return r;
  193. }
  194. /**
  195. * Push a commit
  196. *
  197. * @param from
  198. * @param q
  199. * @return collection of RefUpdates
  200. * @throws IOException
  201. */
  202. protected static Collection<RemoteRefUpdate> push(TestRepository from,
  203. RevCommit q) throws IOException {
  204. final Repository db = from.getRepository();
  205. final String srcExpr = q.name();
  206. final String dstName = master;
  207. final boolean forceUpdate = true;
  208. final String localName = null;
  209. final ObjectId oldId = null;
  210. RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
  211. forceUpdate, localName, oldId);
  212. return Collections.singleton(u);
  213. }
  214. /**
  215. * Create loose object path
  216. *
  217. * @param base
  218. * @param id
  219. * @return path of the loose object
  220. */
  221. public static String loose(URIish base, AnyObjectId id) {
  222. final String objectName = id.name();
  223. final String d = objectName.substring(0, 2);
  224. final String f = objectName.substring(2);
  225. return join(base, "objects/" + d + "/" + f);
  226. }
  227. /**
  228. * Join a base URIish and a path
  229. *
  230. * @param base
  231. * @param path
  232. * a relative path
  233. * @return the joined path
  234. */
  235. public static String join(URIish base, String path) {
  236. if (path.startsWith("/"))
  237. fail("Cannot join absolute path " + path + " to URIish " + base);
  238. String dir = base.getPath();
  239. if (!dir.endsWith("/"))
  240. dir += "/";
  241. return dir + path;
  242. }
  243. /**
  244. * Rewrite a url
  245. *
  246. * @param url
  247. * @param newProtocol
  248. * @param newPort
  249. * @return the rewritten url
  250. */
  251. protected static String rewriteUrl(String url, String newProtocol,
  252. int newPort) {
  253. String newUrl = url;
  254. if (newProtocol != null && !newProtocol.isEmpty()) {
  255. int schemeEnd = newUrl.indexOf("://");
  256. if (schemeEnd >= 0) {
  257. newUrl = newProtocol + newUrl.substring(schemeEnd);
  258. }
  259. }
  260. if (newPort > 0) {
  261. newUrl = newUrl.replaceFirst(":\\d+/", ":" + newPort + "/");
  262. } else {
  263. // Remove the port, if any
  264. newUrl = newUrl.replaceFirst(":\\d+/", "/");
  265. }
  266. return newUrl;
  267. }
  268. /**
  269. * Extend a path
  270. *
  271. * @param uri
  272. * @param pathComponents
  273. * @return the extended URIish
  274. * @throws URISyntaxException
  275. */
  276. protected static URIish extendPath(URIish uri, String pathComponents)
  277. throws URISyntaxException {
  278. String raw = uri.toString();
  279. String newComponents = pathComponents;
  280. if (!newComponents.startsWith("/")) {
  281. newComponents = '/' + newComponents;
  282. }
  283. if (!newComponents.endsWith("/")) {
  284. newComponents += '/';
  285. }
  286. int i = raw.lastIndexOf('/');
  287. raw = raw.substring(0, i) + newComponents + raw.substring(i + 1);
  288. return new URIish(raw);
  289. }
  290. }