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