use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.
the class MimeUtilityFunctionTest method testLargePayload.
@Test(description = "When the payload exceeds 2MB check whether the response received back matches " + "the original content length")
public void testLargePayload() {
String path = "/test/largepayload";
try {
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/fileThatExceeds2MB.txt");
Channel channel = new MockByteChannel(byteChannel, 10);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
String responseValue = characterChannel.readAll();
characterChannel.close();
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", responseValue);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, "mockEP", cMsg);
Assert.assertNotNull(response, "Response message not found");
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
Assert.assertNotNull(inputStream, "Inputstream is null");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MimeUtil.writeInputToOutputStream(inputStream, outputStream);
Assert.assertEquals(outputStream.size(), 2323779);
} catch (IOException | URISyntaxException e) {
log.error("Error occurred in testLargePayload", e.getMessage());
}
}
use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.
the class MultipartDecoderTest method testMultiplePartsForFormData.
@Test(description = "Test sending a multipart request as multipart/form-data with multiple body parts")
public void testMultiplePartsForFormData() {
String path = "/test/multipleparts";
List<Header> headers = new ArrayList<>();
String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
headers.add(new Header(HttpHeaderNames.CONTENT_TYPE.toString(), "multipart/form-data; boundary=" + multipartDataBoundary));
String multipartBody = "--" + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"foo\"" + "\r\n" + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Part1" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"filepart\"; filename=\"file-01.txt\"" + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Part2" + StringUtil.NEWLINE + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_POST, headers, multipartBody);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
Assert.assertEquals(ResponseReader.getReturnValue(response), " -- Part1 -- Part2" + StringUtil.NEWLINE);
}
use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testNestedPartsInOutResponse.
@Test(description = "Retrieve body parts from the Request and send it across Response")
public void testNestedPartsInOutResponse() {
String path = "/multipart/nested_parts_in_outresponse";
HTTPTestRequest inRequestMsg = Util.createNestedPartRequest(path);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts(inRequestMsg.getHeader(HttpHeaderNames.CONTENT_TYPE.toString()), inputStream);
Assert.assertEquals(mimeParts.size(), 2);
List<MIMEPart> childParts = MultipartDecoder.decodeBodyParts(mimeParts.get(1).getContentType(), mimeParts.get(1).readOnce());
Assert.assertEquals(childParts.size(), 2);
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
}
}
use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testMultipartsInOutResponse.
@Test(description = "Test whether the encoded body parts can be sent through Response, with a given boundary")
public void testMultipartsInOutResponse() {
String path = "/multipart/encode_out_response";
HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_GET);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + "e3a0b9ad7b4e7cdb", inputStream);
Assert.assertEquals(mimeParts.size(), 4);
BStruct bodyPart = Util.getEntityStruct(result);
validateBodyPartContent(mimeParts, bodyPart);
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
} catch (IOException e) {
log.error("Error occurred while decoding binary part", e.getMessage());
}
}
use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.
the class MultipartFormDataDecoderTest method testXmlBodyPartAsFileUpload.
@Test(description = "Test sending a multipart request with a json body part where the content is kept in a file")
public void testXmlBodyPartAsFileUpload() {
String path = "/test/xmlbodypart";
Map<String, Object> messageMap = Util.createPrerequisiteMessages(path, MULTIPART_FORM_DATA, result);
ArrayList<BStruct> bodyParts = new ArrayList<>();
bodyParts.add(Util.getXmlFilePart(result));
HTTPTestRequest cMsg = Util.getCarbonMessageWithBodyParts(messageMap, Util.getArrayOfBodyParts(bodyParts));
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response, "Response message not found");
Assert.assertEquals(new BXMLItem(ResponseReader.getReturnValue(response)).getTextValue().stringValue(), "Ballerina" + " xml file part");
}
Aggregations