Search in sources :

Example 96 with BStruct

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");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Test(org.testng.annotations.Test)

Example 97 with BStruct

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());
    }
}
Also used : MultipartDataSource(org.ballerinalang.mime.util.MultipartDataSource) BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 98 with BStruct

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());
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MimeTypeParseException(javax.activation.MimeTypeParseException) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) IOException(java.io.IOException) MIMEPart(org.jvnet.mimepull.MIMEPart) Test(org.testng.annotations.Test)

Example 99 with BStruct

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");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 100 with BStruct

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");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16