001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.ows;
006:
007: import com.mockobjects.servlet.MockHttpServletRequest;
008: import com.mockobjects.servlet.MockHttpServletResponse;
009: import com.mockobjects.servlet.MockServletInputStream;
010: import com.mockobjects.servlet.MockServletOutputStream;
011: import junit.framework.TestCase;
012: import org.geoserver.platform.Service;
013: import org.geotools.util.Version;
014: import org.springframework.context.support.FileSystemXmlApplicationContext;
015: import java.io.BufferedInputStream;
016: import java.io.BufferedReader;
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.io.FileOutputStream;
020: import java.io.InputStreamReader;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: public class DispatcherTest extends TestCase {
026: public void testReadOpContext() throws Exception {
027: MockHttpServletRequest request = new MockHttpServletRequest();
028: request.setupGetContextPath("/geoserver");
029: request.setupGetRequestURI("/geoserver/hello");
030: request.setupGetMethod("get");
031:
032: Dispatcher dispatcher = new Dispatcher();
033: Map map = dispatcher.readOpContext(request);
034:
035: assertEquals("hello", map.get("service"));
036: assertNull(map.get("request"));
037:
038: request = new MockHttpServletRequest();
039: request.setupGetContextPath("/geoserver");
040: request.setupGetRequestURI("/geoserver/hello/Hello");
041: request.setupGetMethod("get");
042: map = dispatcher.readOpContext(request);
043:
044: request = new MockHttpServletRequest();
045: request.setupGetContextPath("/geoserver");
046: request.setupGetRequestURI("/geoserver/hello/Hello/");
047:
048: request.setupGetMethod("get");
049: map = dispatcher.readOpContext(request);
050:
051: assertEquals("hello", map.get("service"));
052: assertEquals("Hello", map.get("request"));
053: }
054:
055: public void testReadOpPost() throws Exception {
056: MockHttpServletRequest request = new MockHttpServletRequest();
057: request.setupGetContextPath("/geoserver");
058: request.setupGetRequestURI("/geoserver/hello");
059: request.setupGetMethod("post");
060:
061: String body = "<Hello service=\"hello\"/>";
062:
063: MockServletInputStream input = new MockServletInputStream();
064: input.setupRead(body.getBytes());
065:
066: request.setupGetInputStream(input);
067:
068: Dispatcher dispatcher = new Dispatcher();
069:
070: BufferedReader buffered = new BufferedReader(
071: new InputStreamReader(input));
072: buffered.mark(2048);
073:
074: Map map = dispatcher.readOpPost(buffered);
075:
076: assertNotNull(map);
077: assertEquals("Hello", map.get("request"));
078: assertEquals("hello", map.get("service"));
079: }
080:
081: public void testParseKVP() throws Exception {
082: URL url = getClass().getResource("applicationContext.xml");
083:
084: FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
085: url.toString());
086:
087: Dispatcher dispatcher = (Dispatcher) context
088: .getBean("dispatcher");
089:
090: MockHttpServletRequest request = new MockHttpServletRequest();
091: request.setupGetContextPath("/geoserver");
092:
093: Map params = new HashMap();
094: params.put("service", "hello");
095: params.put("request", "Hello");
096: params.put("message", "Hello world!");
097:
098: request.setupGetParameterMap(params);
099: request
100: .setupQueryString("service=hello&request=hello&message=Hello World!");
101:
102: Dispatcher.Request req = new Dispatcher.Request();
103: req.httpRequest = request;
104:
105: dispatcher.parseKVP(req);
106:
107: Message message = (Message) dispatcher.parseRequestKVP(
108: Message.class, req);
109: assertEquals(new Message("Hello world!"), message);
110: }
111:
112: public void testParseXML() throws Exception {
113: URL url = getClass().getResource("applicationContext.xml");
114:
115: FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
116: url.toString());
117:
118: Dispatcher dispatcher = (Dispatcher) context
119: .getBean("dispatcher");
120:
121: String body = "<Hello service=\"hello\" message=\"Hello world!\"/>";
122: File file = File.createTempFile("geoserver", "req");
123: file.deleteOnExit();
124:
125: FileOutputStream output = new FileOutputStream(file);
126: output.write(body.getBytes());
127: output.flush();
128: output.close();
129:
130: BufferedReader input = new BufferedReader(
131: new InputStreamReader(new FileInputStream(file)));
132:
133: input.mark(8192);
134:
135: Dispatcher.Request req = new Dispatcher.Request();
136: req.input = input;
137:
138: Object object = dispatcher.parseRequestXML(null, input, req);
139: assertEquals(new Message("Hello world!"), object);
140: }
141:
142: public void testHelloOperationGet() throws Exception {
143: URL url = getClass().getResource("applicationContext.xml");
144:
145: FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
146: url.toString());
147:
148: Dispatcher dispatcher = (Dispatcher) context
149: .getBean("dispatcher");
150:
151: MockHttpServletRequest request = new MockHttpServletRequest() {
152: String encoding;
153:
154: public int getServerPort() {
155: return 8080;
156: }
157:
158: public String getCharacterEncoding() {
159: return encoding;
160: }
161:
162: public void setCharacterEncoding(String encoding) {
163: this .encoding = encoding;
164: }
165: };
166:
167: request.setupScheme("http");
168: request.setupServerName("localhost");
169:
170: request.setupGetContextPath("/geoserver");
171: request.setupGetMethod("GET");
172:
173: MockHttpServletResponse response = new MockHttpServletResponse();
174: response.setupOutputStream(new MockServletOutputStream());
175:
176: Map params = new HashMap();
177: params.put("service", "hello");
178: params.put("request", "Hello");
179: params.put("version", "1.0.0");
180: params.put("message", "Hello world!");
181:
182: request.setupGetParameterMap(params);
183: request.setupGetInputStream(null);
184: request
185: .setupGetRequestURI("http://localhost/geoserver/ows?service=hello&request=hello&message=HelloWorld");
186: request
187: .setupQueryString("service=hello&request=hello&message=HelloWorld");
188: dispatcher.handleRequest(request, response);
189: assertEquals(params.get("message"), response
190: .getOutputStreamContents());
191: }
192:
193: public void testHelloOperationPost() throws Exception {
194: URL url = getClass().getResource("applicationContext.xml");
195:
196: FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
197: url.toString());
198:
199: Dispatcher dispatcher = (Dispatcher) context
200: .getBean("dispatcher");
201:
202: MockHttpServletRequest request = new MockHttpServletRequest() {
203: String encoding;
204:
205: public int getServerPort() {
206: return 8080;
207: }
208:
209: public String getCharacterEncoding() {
210: return encoding;
211: }
212:
213: public void setCharacterEncoding(String encoding) {
214: this .encoding = encoding;
215: }
216: };
217:
218: request.setupScheme("http");
219: request.setupServerName("localhost");
220: request.setupGetContextPath("/geoserver");
221: request.setupGetMethod("POST");
222: request.setupGetRequestURI("http://localhost/geoserver/ows");
223: request.setupGetContentType("application/xml");
224:
225: MockHttpServletResponse response = new MockHttpServletResponse();
226: response.setupOutputStream(new MockServletOutputStream());
227:
228: Map params = new HashMap();
229: request.setupGetParameterMap(params);
230:
231: String body = "<Hello service=\"hello\" message=\"Hello world!\" version=\"1.0.0\" />";
232: MockServletInputStream input = new MockServletInputStream();
233: input.setupRead(body.getBytes());
234:
235: request.setupGetInputStream(input);
236:
237: dispatcher.handleRequest(request, response);
238: assertEquals("Hello world!", response.getOutputStreamContents());
239: }
240: }
|