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.

RegexPipelineTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2012, 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.junit.Assert.assertEquals;
  45. import java.io.BufferedReader;
  46. import java.io.IOException;
  47. import java.io.InputStreamReader;
  48. import java.io.PrintWriter;
  49. import java.net.HttpURLConnection;
  50. import java.net.URI;
  51. import javax.servlet.http.HttpServlet;
  52. import javax.servlet.http.HttpServletRequest;
  53. import javax.servlet.http.HttpServletResponse;
  54. import org.eclipse.jetty.servlet.ServletContextHandler;
  55. import org.eclipse.jetty.servlet.ServletHolder;
  56. import org.eclipse.jgit.http.server.glue.MetaServlet;
  57. import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
  58. import org.eclipse.jgit.junit.http.AppServer;
  59. import org.eclipse.jgit.junit.http.HttpTestCase;
  60. import org.junit.After;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. public class RegexPipelineTest extends HttpTestCase {
  64. private ServletContextHandler ctx;
  65. private static class Servlet extends HttpServlet {
  66. private static final long serialVersionUID = 1L;
  67. private final String name;
  68. private Servlet(String name) {
  69. this.name = name;
  70. }
  71. @Override
  72. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  73. throws IOException {
  74. res.setStatus(200);
  75. PrintWriter out = new PrintWriter(res.getOutputStream());
  76. out.write(name);
  77. out.write("\n");
  78. out.write(String.valueOf(req.getServletPath()));
  79. out.write("\n");
  80. out.write(String.valueOf(req.getPathInfo()));
  81. out.write("\n");
  82. out.flush();
  83. }
  84. }
  85. @Before
  86. public void setUp() throws Exception {
  87. server = new AppServer();
  88. ctx = server.addContext("/");
  89. }
  90. @After
  91. public void tearDown() throws Exception {
  92. server.tearDown();
  93. }
  94. @Test
  95. public void testSimpleRegex() throws Exception {
  96. MetaServlet s = new MetaServlet();
  97. s.serveRegex("^(/a|/b)$").with(new Servlet("test"));
  98. ctx.addServlet(new ServletHolder(s), "/*");
  99. server.setUp();
  100. final URI uri = server.getURI();
  101. HttpURLConnection c;
  102. BufferedReader r;
  103. c = ((HttpURLConnection) uri.resolve("/a").toURL()
  104. .openConnection());
  105. assertEquals(200, c.getResponseCode());
  106. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  107. assertEquals("test", r.readLine());
  108. assertEquals("", r.readLine());
  109. assertEquals("/a", r.readLine());
  110. assertEquals(null, r.readLine());
  111. c = ((HttpURLConnection) uri.resolve("/b").toURL()
  112. .openConnection());
  113. assertEquals(200, c.getResponseCode());
  114. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  115. assertEquals("test", r.readLine());
  116. assertEquals("", r.readLine());
  117. assertEquals("/b", r.readLine());
  118. assertEquals(null, r.readLine());
  119. c = ((HttpURLConnection) uri.resolve("/c").toURL()
  120. .openConnection());
  121. assertEquals(404, c.getResponseCode());
  122. }
  123. @Test
  124. public void testServeOrdering() throws Exception {
  125. MetaServlet s = new MetaServlet();
  126. s.serveRegex("^(/a)$").with(new Servlet("test1"));
  127. s.serveRegex("^(/a+)$").with(new Servlet("test2"));
  128. ctx.addServlet(new ServletHolder(s), "/*");
  129. server.setUp();
  130. final URI uri = server.getURI();
  131. HttpURLConnection c;
  132. BufferedReader r;
  133. c = ((HttpURLConnection) uri.resolve("/a").toURL()
  134. .openConnection());
  135. assertEquals(200, c.getResponseCode());
  136. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  137. assertEquals("test1", r.readLine());
  138. assertEquals("", r.readLine());
  139. assertEquals("/a", r.readLine());
  140. assertEquals(null, r.readLine());
  141. }
  142. @Test
  143. public void testRegexGroupFilter() throws Exception {
  144. MetaServlet s = new MetaServlet();
  145. s.serveRegex("^(/a)(/b)$")
  146. .with(new Servlet("test1"));
  147. s.serveRegex("^(/c)(/d)$")
  148. .through(new RegexGroupFilter(1))
  149. .with(new Servlet("test2"));
  150. s.serveRegex("^(/e)/f(/g)$")
  151. .through(new RegexGroupFilter(2))
  152. .with(new Servlet("test3"));
  153. ctx.addServlet(new ServletHolder(s), "/*");
  154. server.setUp();
  155. final URI uri = server.getURI();
  156. HttpURLConnection c;
  157. BufferedReader r;
  158. c = ((HttpURLConnection) uri.resolve("/a/b").toURL()
  159. .openConnection());
  160. assertEquals(200, c.getResponseCode());
  161. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  162. assertEquals("test1", r.readLine());
  163. assertEquals("", r.readLine());
  164. // No RegexGroupFilter defaults to first group.
  165. assertEquals("/a", r.readLine());
  166. assertEquals(null, r.readLine());
  167. c = ((HttpURLConnection) uri.resolve("/c/d").toURL()
  168. .openConnection());
  169. assertEquals(200, c.getResponseCode());
  170. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  171. assertEquals("test2", r.readLine());
  172. assertEquals("", r.readLine());
  173. assertEquals("/c", r.readLine());
  174. assertEquals(null, r.readLine());
  175. c = ((HttpURLConnection) uri.resolve("/e/f/g").toURL()
  176. .openConnection());
  177. assertEquals(200, c.getResponseCode());
  178. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  179. assertEquals("test3", r.readLine());
  180. assertEquals("/e/f", r.readLine());
  181. assertEquals("/g", r.readLine());
  182. assertEquals(null, r.readLine());
  183. }
  184. }