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