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