1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.http.test;
45
46 import static java.nio.charset.StandardCharsets.UTF_8;
47 import static org.junit.Assert.assertEquals;
48
49 import java.io.BufferedReader;
50 import java.io.BufferedWriter;
51 import java.io.IOException;
52 import java.io.InputStreamReader;
53 import java.io.OutputStreamWriter;
54 import java.io.PrintWriter;
55 import java.net.HttpURLConnection;
56 import java.net.URI;
57
58 import javax.servlet.http.HttpServlet;
59 import javax.servlet.http.HttpServletRequest;
60 import javax.servlet.http.HttpServletResponse;
61
62 import org.eclipse.jetty.servlet.ServletContextHandler;
63 import org.eclipse.jetty.servlet.ServletHolder;
64 import org.eclipse.jgit.http.server.glue.MetaServlet;
65 import org.eclipse.jgit.http.server.glue.RegexGroupFilter;
66 import org.eclipse.jgit.junit.http.AppServer;
67 import org.eclipse.jgit.junit.http.HttpTestCase;
68 import org.junit.After;
69 import org.junit.Before;
70 import org.junit.Test;
71
72 public class RegexPipelineTest extends HttpTestCase {
73 private ServletContextHandler ctx;
74
75 private static class Servlet extends HttpServlet {
76 private static final long serialVersionUID = 1L;
77
78 private final String name;
79
80 private Servlet(String name) {
81 this.name = name;
82 }
83
84 @Override
85 protected void doGet(HttpServletRequest req, HttpServletResponse res)
86 throws IOException {
87 res.setStatus(200);
88 PrintWriter out = new PrintWriter(new BufferedWriter(
89 new OutputStreamWriter(res.getOutputStream(), UTF_8)));
90 out.write(name);
91 out.write("\n");
92 out.write(String.valueOf(req.getServletPath()));
93 out.write("\n");
94 out.write(String.valueOf(req.getPathInfo()));
95 out.write("\n");
96 out.flush();
97 }
98 }
99
100 @Override
101 @Before
102 public void setUp() throws Exception {
103 server = new AppServer();
104 ctx = server.addContext("/");
105 }
106
107 @Override
108 @After
109 public void tearDown() throws Exception {
110 server.tearDown();
111 }
112
113 @Test
114 public void testSimpleRegex() throws Exception {
115 MetaServlet s = new MetaServlet();
116 s.serveRegex("^(/a|/b)$").with(new Servlet("test"));
117 ctx.addServlet(new ServletHolder(s), "/*");
118 server.setUp();
119
120 final URI uri = server.getURI();
121 HttpURLConnection c;
122 BufferedReader r;
123
124 c = ((HttpURLConnection) uri.resolve("/a").toURL()
125 .openConnection());
126 assertEquals(200, c.getResponseCode());
127 r = new BufferedReader(
128 new InputStreamReader(c.getInputStream(), UTF_8));
129 assertEquals("test", r.readLine());
130 assertEquals("", r.readLine());
131 assertEquals("/a", r.readLine());
132 assertEquals(null, r.readLine());
133
134 c = ((HttpURLConnection) uri.resolve("/b").toURL()
135 .openConnection());
136 assertEquals(200, c.getResponseCode());
137 r = new BufferedReader(
138 new InputStreamReader(c.getInputStream(), UTF_8));
139 assertEquals("test", r.readLine());
140 assertEquals("", r.readLine());
141 assertEquals("/b", r.readLine());
142 assertEquals(null, r.readLine());
143
144 c = ((HttpURLConnection) uri.resolve("/c").toURL()
145 .openConnection());
146 assertEquals(404, c.getResponseCode());
147 }
148
149 @Test
150 public void testServeOrdering() throws Exception {
151 MetaServlet s = new MetaServlet();
152 s.serveRegex("^(/a)$").with(new Servlet("test1"));
153 s.serveRegex("^(/a+)$").with(new Servlet("test2"));
154 ctx.addServlet(new ServletHolder(s), "/*");
155 server.setUp();
156
157 final URI uri = server.getURI();
158 HttpURLConnection c;
159 BufferedReader r;
160
161 c = ((HttpURLConnection) uri.resolve("/a").toURL()
162 .openConnection());
163 assertEquals(200, c.getResponseCode());
164 r = new BufferedReader(
165 new InputStreamReader(c.getInputStream(), UTF_8));
166 assertEquals("test1", r.readLine());
167 assertEquals("", r.readLine());
168 assertEquals("/a", r.readLine());
169 assertEquals(null, r.readLine());
170 }
171
172 @Test
173 public void testRegexGroupFilter() throws Exception {
174 MetaServlet s = new MetaServlet();
175 s.serveRegex("^(/a)(/b)$")
176 .with(new Servlet("test1"));
177 s.serveRegex("^(/c)(/d)$")
178 .through(new RegexGroupFilter(1))
179 .with(new Servlet("test2"));
180 s.serveRegex("^(/e)/f(/g)$")
181 .through(new RegexGroupFilter(2))
182 .with(new Servlet("test3"));
183 ctx.addServlet(new ServletHolder(s), "/*");
184 server.setUp();
185
186 final URI uri = server.getURI();
187 HttpURLConnection c;
188 BufferedReader r;
189
190 c = ((HttpURLConnection) uri.resolve("/a/b").toURL()
191 .openConnection());
192 assertEquals(200, c.getResponseCode());
193 r = new BufferedReader(
194 new InputStreamReader(c.getInputStream(), UTF_8));
195 assertEquals("test1", r.readLine());
196 assertEquals("", r.readLine());
197
198 assertEquals("/a", r.readLine());
199 assertEquals(null, r.readLine());
200
201 c = ((HttpURLConnection) uri.resolve("/c/d").toURL()
202 .openConnection());
203 assertEquals(200, c.getResponseCode());
204 r = new BufferedReader(
205 new InputStreamReader(c.getInputStream(), UTF_8));
206 assertEquals("test2", r.readLine());
207 assertEquals("", r.readLine());
208 assertEquals("/c", r.readLine());
209 assertEquals(null, r.readLine());
210
211 c = ((HttpURLConnection) uri.resolve("/e/f/g").toURL()
212 .openConnection());
213 assertEquals(200, c.getResponseCode());
214 r = new BufferedReader(
215 new InputStreamReader(c.getInputStream(), UTF_8));
216 assertEquals("test3", r.readLine());
217 assertEquals("/e/f", r.readLine());
218 assertEquals("/g", r.readLine());
219 assertEquals(null, r.readLine());
220 }
221 }