001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.servlet;
017:
018: import static org.easymock.EasyMock.createMock;
019: import static org.easymock.EasyMock.expect;
020: import static org.easymock.EasyMock.replay;
021:
022: import java.net.URL;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030:
031: import org.apache.commons.io.IOUtils;
032: import org.apache.solr.core.SolrConfig;
033: import org.apache.solr.core.SolrCore;
034: import org.apache.solr.request.MultiMapSolrParams;
035: import org.apache.solr.request.SolrParams;
036: import org.apache.solr.util.AbstractSolrTestCase;
037: import org.apache.solr.util.ContentStream;
038:
039: public class SolrRequestParserTest extends AbstractSolrTestCase {
040:
041: public String getSchemaFile() {
042: return "schema.xml";
043: }
044:
045: public String getSolrConfigFile() {
046: return "solrconfig.xml";
047: }
048:
049: SolrRequestParsers parser;
050:
051: public void setUp() throws Exception {
052: super .setUp();
053: parser = new SolrRequestParsers(SolrCore.getSolrCore(),
054: SolrConfig.config);
055: }
056:
057: public void testStreamBody() throws Exception {
058: String body1 = "AMANAPLANPANAMA";
059: String body2 = "qwertasdfgzxcvb";
060: String body3 = "1234567890";
061:
062: Map<String, String[]> args = new HashMap<String, String[]>();
063: args.put(SolrParams.STREAM_BODY, new String[] { body1 });
064:
065: // Make sure it got a single stream in and out ok
066: List<ContentStream> streams = new ArrayList<ContentStream>();
067: parser.buildRequestFrom(new MultiMapSolrParams(args), streams);
068: assertEquals(1, streams.size());
069: assertEquals(body1, IOUtils
070: .toString(streams.get(0).getStream()));
071:
072: // Now add three and make sure they come out ok
073: streams = new ArrayList<ContentStream>();
074: args.put(SolrParams.STREAM_BODY, new String[] { body1, body2,
075: body3 });
076: parser.buildRequestFrom(new MultiMapSolrParams(args), streams);
077: assertEquals(3, streams.size());
078: ArrayList<String> input = new ArrayList<String>();
079: ArrayList<String> output = new ArrayList<String>();
080: input.add(body1);
081: input.add(body2);
082: input.add(body3);
083: output.add(IOUtils.toString(streams.get(0).getStream()));
084: output.add(IOUtils.toString(streams.get(1).getStream()));
085: output.add(IOUtils.toString(streams.get(2).getStream()));
086: // sort them so the output is consistent
087: Collections.sort(input);
088: Collections.sort(output);
089: assertEquals(input.toString(), output.toString());
090:
091: // set the contentType and make sure tat gets set
092: String ctype = "text/xxx";
093: streams = new ArrayList<ContentStream>();
094: args.put(SolrParams.STREAM_CONTENTTYPE, new String[] { ctype });
095: parser.buildRequestFrom(new MultiMapSolrParams(args), streams);
096: for (ContentStream s : streams) {
097: assertEquals(ctype, s.getContentType());
098: }
099: }
100:
101: public void testStreamURL() throws Exception {
102: boolean ok = false;
103: String url = "http://svn.apache.org/repos/asf/lucene/solr/trunk/";
104: String txt = null;
105: try {
106: txt = IOUtils.toString(new URL(url).openStream());
107: } catch (Exception ex) {
108: // TODO - should it fail/skip?
109: fail("this test only works if you have a network connection.");
110: return;
111: }
112:
113: Map<String, String[]> args = new HashMap<String, String[]>();
114: args.put(SolrParams.STREAM_URL, new String[] { url });
115:
116: // Make sure it got a single stream in and out ok
117: List<ContentStream> streams = new ArrayList<ContentStream>();
118: parser.buildRequestFrom(new MultiMapSolrParams(args), streams);
119: assertEquals(1, streams.size());
120: assertEquals(txt, IOUtils.toString(streams.get(0).getStream()));
121: }
122:
123: public void testUrlParamParsing() {
124: String[][] teststr = new String[][] {
125: { "this is simple", "this%20is%20simple" },
126: { "this is simple", "this+is+simple" },
127: { "\u00FC", "%C3%BC" }, // lower-case "u" with diaeresis/umlaut
128: { "\u0026", "%26" }, // &
129: { "\u20AC", "%E2%82%AC" } // euro
130: };
131:
132: for (String[] tst : teststr) {
133: MultiMapSolrParams params = SolrRequestParsers
134: .parseQueryString("val=" + tst[1]);
135: assertEquals(tst[0], params.get("val"));
136: }
137: }
138:
139: public void testStandardParseParamsAndFillStreams()
140: throws Exception {
141: ArrayList<ContentStream> streams = new ArrayList<ContentStream>();
142: Map<String, String[]> params = new HashMap<String, String[]>();
143: params.put("q", new String[] { "hello" });
144:
145: // Set up the expected behavior
146: String[] ct = new String[] {
147: "application/x-www-form-urlencoded",
148: "Application/x-www-form-urlencoded",
149: "application/x-www-form-urlencoded; charset=utf-8",
150: "application/x-www-form-urlencoded;" };
151:
152: for (String contentType : ct) {
153: HttpServletRequest request = createMock(HttpServletRequest.class);
154: expect(request.getMethod()).andReturn("POST").anyTimes();
155: expect(request.getContentType()).andReturn(contentType)
156: .anyTimes();
157: expect(request.getParameterMap()).andReturn(params)
158: .anyTimes();
159: replay(request);
160:
161: MultipartRequestParser multipart = new MultipartRequestParser(
162: 1000000);
163: RawRequestParser raw = new RawRequestParser();
164: StandardRequestParser standard = new StandardRequestParser(
165: multipart, raw);
166:
167: SolrParams p = standard.parseParamsAndFillStreams(request,
168: streams);
169:
170: assertEquals("contentType: " + contentType, "hello", p
171: .get("q"));
172: }
173: }
174: }
|