use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testContentDispositionForFormData.
@Test(description = "Test whether the body part builds the ContentDisposition struct properly for " + "multipart/form-data")
public void testContentDispositionForFormData() {
BStruct bodyPart = Util.getEntityStruct(result);
BStruct contentDispositionStruct = Util.getContentDispositionStruct(result);
MimeUtil.setContentDisposition(contentDispositionStruct, bodyPart, "form-data; name=\"filepart\"; filename=\"file-01.txt\"");
BStruct contentDisposition = (BStruct) bodyPart.getRefField(CONTENT_DISPOSITION_INDEX);
Assert.assertEquals(contentDisposition.getStringField(CONTENT_DISPOSITION_FILENAME_INDEX), "\"file-01.txt\"");
Assert.assertEquals(contentDisposition.getStringField(CONTENT_DISPOSITION_NAME_INDEX), "\"filepart\"");
Assert.assertEquals(contentDisposition.getStringField(DISPOSITION_INDEX), "form-data");
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class MultipartEncoderTest method testNestedParts.
@Test(description = "Test whether the nested body parts within a multipart entity can be properly encoded")
public void testNestedParts() {
BStruct nestedMultipartEntity = Util.getNestedMultipartEntity(result);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
MultipartDataSource multipartDataSource = new MultipartDataSource(nestedMultipartEntity, multipartDataBoundary);
multipartDataSource.serializeData(outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" + multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
for (MIMEPart mimePart : mimeParts) {
testNestedPartContent(mimePart);
}
} catch (MimeTypeParseException | IOException e) {
log.error("Error occurred while testing encoded nested parts", e.getMessage());
}
}
use of org.ballerinalang.model.values.BStruct 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.model.values.BStruct 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");
}
use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.
the class MultipartFormDataDecoderTest method testBinaryBodyPart.
@Test(description = "Test sending a multipart request with a binary body part which is kept in memory")
public void testBinaryBodyPart() {
String path = "/test/binarybodypart";
Map<String, Object> messageMap = Util.createPrerequisiteMessages(path, MULTIPART_FORM_DATA, result);
ArrayList<BStruct> bodyParts = new ArrayList<>();
bodyParts.add(Util.getBinaryBodyPart(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(ResponseReader.getReturnValue(response), "Ballerina binary part");
}
Aggregations