use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testParameterIncompatibilityException1.
@Test
public void testParameterIncompatibilityException1() throws Exception {
HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
// we emulate Jetty that returns empty stream when parameters were parsed before:
when(request.getInputStream()).thenReturn(new ServletInputStream() {
@Override
public int read() {
return -1;
}
@Override
public boolean isFinished() {
return true;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
});
FormDataRequestParser formdata = new FormDataRequestParser(2048);
try {
formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
fail("should throw SolrException");
} catch (SolrException solre) {
assertTrue(solre.getMessage().startsWith("Solr requires that request parameters"));
assertEquals(500, solre.code());
}
verify(request).getInputStream();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testStreamBody.
@Test
public void testStreamBody() throws Exception {
String body1 = "AMANAPLANPANAMA";
String body2 = "qwertasdfgzxcvb";
String body3 = "1234567890";
SolrCore core = h.getCore();
Map<String, String[]> args = new HashMap<>();
args.put(CommonParams.STREAM_BODY, new String[] { body1 });
// Make sure it got a single stream in and out ok
List<ContentStream> streams = new ArrayList<>();
SolrQueryRequest req = parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams);
assertEquals(1, streams.size());
assertEquals(body1, IOUtils.toString(streams.get(0).getReader()));
req.close();
// Now add three and make sure they come out ok
streams = new ArrayList<>();
args.put(CommonParams.STREAM_BODY, new String[] { body1, body2, body3 });
req = parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams);
assertEquals(3, streams.size());
ArrayList<String> input = new ArrayList<>();
ArrayList<String> output = new ArrayList<>();
input.add(body1);
input.add(body2);
input.add(body3);
output.add(IOUtils.toString(streams.get(0).getReader()));
output.add(IOUtils.toString(streams.get(1).getReader()));
output.add(IOUtils.toString(streams.get(2).getReader()));
// sort them so the output is consistent
Collections.sort(input);
Collections.sort(output);
assertEquals(input.toString(), output.toString());
req.close();
// set the contentType and make sure tat gets set
String ctype = "text/xxx";
streams = new ArrayList<>();
args.put(CommonParams.STREAM_CONTENTTYPE, new String[] { ctype });
req = parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams);
for (ContentStream s : streams) {
assertEquals(ctype, s.getContentType());
}
req.close();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testStandardFormdataUploadLimit.
@Test
public void testStandardFormdataUploadLimit() throws Exception {
final int limitKBytes = 128;
final StringBuilder large = new StringBuilder("q=hello");
// grow exponentially to reach 128 KB limit:
while (large.length() <= limitKBytes * 1024) {
large.append('&').append(large);
}
HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", -1);
when(request.getMethod()).thenReturn("POST");
when(request.getInputStream()).thenReturn(new ByteServletInputStream(large.toString().getBytes(StandardCharsets.US_ASCII)));
FormDataRequestParser formdata = new FormDataRequestParser(limitKBytes);
try {
formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
fail("should throw SolrException");
} catch (SolrException solre) {
assertTrue(solre.getMessage().contains("upload limit"));
assertEquals(400, solre.code());
}
verify(request).getInputStream();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrTestCaseJ4 method addDoc.
public static void addDoc(String doc, String updateRequestProcessorChain) throws Exception {
Map<String, String[]> params = new HashMap<>();
MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
params.put(UpdateParams.UPDATE_CHAIN, new String[] { updateRequestProcessorChain });
SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), (SolrParams) mmparams) {
};
UpdateRequestHandler handler = new UpdateRequestHandler();
handler.init(null);
ArrayList<ContentStream> streams = new ArrayList<>(2);
streams.add(new ContentStreamBase.StringStream(doc));
req.setContentStreams(streams);
handler.handleRequestBody(req, new SolrQueryResponse());
req.close();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class DocumentAnalysisRequestHandlerTest method testCharsetOutsideDocument.
// This test should also test charset detection in UpdateRequestHandler,
// but the DocumentAnalysisRequestHandler is simplier to use/check.
@Test
public void testCharsetOutsideDocument() throws Exception {
final byte[] xmlBytes = ("<docs>\r\n" + " <doc>\r\n" + " <field name=\"id\">Müller</field>\r\n" + " </doc>" + "</docs>").getBytes(StandardCharsets.ISO_8859_1);
// we declare a content stream with charset:
final ContentStream cs = new ByteStream(xmlBytes, "application/xml; charset=ISO-8859-1");
ModifiableSolrParams params = new ModifiableSolrParams();
SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {
@Override
public Iterable<ContentStream> getContentStreams() {
return Collections.singleton(cs);
}
};
DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
assertNotNull(request);
final List<SolrInputDocument> documents = request.getDocuments();
assertNotNull(documents);
assertEquals(1, documents.size());
SolrInputDocument doc = documents.get(0);
assertEquals("Müller", doc.getField("id").getValue());
}
Aggregations