View Javadoc
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  
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 		// No RegexGroupFilter defaults to first group.
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 }