Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RegexPipelineTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. @Override
  86. @Before
  87. public void setUp() throws Exception {
  88. server = new AppServer();
  89. ctx = server.addContext("/");
  90. }
  91. @Override
  92. @After
  93. public void tearDown() throws Exception {
  94. server.tearDown();
  95. }
  96. @Test
  97. public void testSimpleRegex() throws Exception {
  98. MetaServlet s = new MetaServlet();
  99. s.serveRegex("^(/a|/b)$").with(new Servlet("test"));
  100. ctx.addServlet(new ServletHolder(s), "/*");
  101. server.setUp();
  102. final URI uri = server.getURI();
  103. HttpURLConnection c;
  104. BufferedReader r;
  105. c = ((HttpURLConnection) uri.resolve("/a").toURL()
  106. .openConnection());
  107. assertEquals(200, c.getResponseCode());
  108. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  109. assertEquals("test", r.readLine());
  110. assertEquals("", r.readLine());
  111. assertEquals("/a", r.readLine());
  112. assertEquals(null, r.readLine());
  113. c = ((HttpURLConnection) uri.resolve("/b").toURL()
  114. .openConnection());
  115. assertEquals(200, c.getResponseCode());
  116. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  117. assertEquals("test", r.readLine());
  118. assertEquals("", r.readLine());
  119. assertEquals("/b", r.readLine());
  120. assertEquals(null, r.readLine());
  121. c = ((HttpURLConnection) uri.resolve("/c").toURL()
  122. .openConnection());
  123. assertEquals(404, c.getResponseCode());
  124. }
  125. @Test
  126. public void testServeOrdering() throws Exception {
  127. MetaServlet s = new MetaServlet();
  128. s.serveRegex("^(/a)$").with(new Servlet("test1"));
  129. s.serveRegex("^(/a+)$").with(new Servlet("test2"));
  130. ctx.addServlet(new ServletHolder(s), "/*");
  131. server.setUp();
  132. final URI uri = server.getURI();
  133. HttpURLConnection c;
  134. BufferedReader r;
  135. c = ((HttpURLConnection) uri.resolve("/a").toURL()
  136. .openConnection());
  137. assertEquals(200, c.getResponseCode());
  138. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  139. assertEquals("test1", r.readLine());
  140. assertEquals("", r.readLine());
  141. assertEquals("/a", r.readLine());
  142. assertEquals(null, r.readLine());
  143. }
  144. @Test
  145. public void testRegexGroupFilter() throws Exception {
  146. MetaServlet s = new MetaServlet();
  147. s.serveRegex("^(/a)(/b)$")
  148. .with(new Servlet("test1"));
  149. s.serveRegex("^(/c)(/d)$")
  150. .through(new RegexGroupFilter(1))
  151. .with(new Servlet("test2"));
  152. s.serveRegex("^(/e)/f(/g)$")
  153. .through(new RegexGroupFilter(2))
  154. .with(new Servlet("test3"));
  155. ctx.addServlet(new ServletHolder(s), "/*");
  156. server.setUp();
  157. final URI uri = server.getURI();
  158. HttpURLConnection c;
  159. BufferedReader r;
  160. c = ((HttpURLConnection) uri.resolve("/a/b").toURL()
  161. .openConnection());
  162. assertEquals(200, c.getResponseCode());
  163. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  164. assertEquals("test1", r.readLine());
  165. assertEquals("", r.readLine());
  166. // No RegexGroupFilter defaults to first group.
  167. assertEquals("/a", r.readLine());
  168. assertEquals(null, r.readLine());
  169. c = ((HttpURLConnection) uri.resolve("/c/d").toURL()
  170. .openConnection());
  171. assertEquals(200, c.getResponseCode());
  172. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  173. assertEquals("test2", r.readLine());
  174. assertEquals("", r.readLine());
  175. assertEquals("/c", r.readLine());
  176. assertEquals(null, r.readLine());
  177. c = ((HttpURLConnection) uri.resolve("/e/f/g").toURL()
  178. .openConnection());
  179. assertEquals(200, c.getResponseCode());
  180. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  181. assertEquals("test3", r.readLine());
  182. assertEquals("/e/f", r.readLine());
  183. assertEquals("/g", r.readLine());
  184. assertEquals(null, r.readLine());
  185. }
  186. }