Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chamil321 committed Feb 14, 2023
1 parent 80d8d6f commit 914dc6d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.ballerinalang.stdlib.multipart;

import io.netty.handler.codec.http.HttpHeaderNames;
import org.ballerinalang.jvm.values.ObjectValue;
import org.ballerinalang.mime.util.EntityBodyHandler;
import org.ballerinalang.mime.util.MimeUtil;
import org.ballerinalang.mime.util.MultipartDataSource;
import org.ballerinalang.mime.util.MultipartDecoder;
Expand Down Expand Up @@ -94,7 +95,7 @@ public void testMultipartWriterForMixed() {
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" +
multipartDataBoundary, inputStream);
multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
ObjectValue bodyPart = createEntityObject();
validateBodyPartContent(mimeParts, bodyPart);
Expand All @@ -115,7 +116,7 @@ public void testMultipartWriterForNewSubTypes() {
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/new-sub-type; boundary=" +
multipartDataBoundary, inputStream);
multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
ObjectValue bodyPart = createEntityObject();
validateBodyPartContent(mimeParts, bodyPart);
Expand All @@ -136,7 +137,7 @@ public void testNestedParts() {
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" +
multipartDataBoundary, inputStream);
multipartDataBoundary, inputStream);
Assert.assertEquals(mimeParts.size(), 4);
for (MIMEPart mimePart : mimeParts) {
testNestedPartContent(mimePart);
Expand All @@ -155,7 +156,7 @@ public void testNestedParts() {
*/
private void testNestedPartContent(MIMEPart mimePart) throws MimeTypeParseException, IOException {
List<MIMEPart> nestedParts = MultipartDecoder.decodeBodyParts(mimePart.getContentType(),
mimePart.readOnce());
mimePart.readOnce());
Assert.assertEquals(nestedParts.size(), 4);
ObjectValue ballerinaBodyPart = createEntityObject();
validateBodyPartContent(nestedParts, ballerinaBodyPart);
Expand All @@ -167,7 +168,7 @@ public void testContentDispositionForFormData() {
ObjectValue bodyPart = createEntityObject();
ObjectValue contentDispositionStruct = getContentDispositionStruct();
MimeUtil.setContentDisposition(contentDispositionStruct, bodyPart,
"form-data; name=\"filepart\"; filename=\"file-01.txt\"");
"form-data; name=\"filepart\"; filename=\"file-01.txt\"");
ObjectValue contentDisposition =
(ObjectValue) bodyPart.get(CONTENT_DISPOSITION_FIELD);
Assert.assertEquals(contentDisposition.get(CONTENT_DISPOSITION_FILENAME_FIELD).toString(), "file-01.txt");
Expand All @@ -184,7 +185,7 @@ public void testMultipartsInOutResponse() {
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts("multipart/mixed; boundary=" +
"e3a0b9ad7b4e7cdb", inputStream);
"e3a0b9ad7b4e7cdb", inputStream);
Assert.assertEquals(mimeParts.size(), 4);
ObjectValue bodyPart = createEntityObject();
validateBodyPartContent(mimeParts, bodyPart);
Expand All @@ -204,11 +205,11 @@ public void testNestedPartsInOutResponse() {
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts(inRequestMsg.getHeader(HttpHeaderNames.
CONTENT_TYPE.toString()),
inputStream);
CONTENT_TYPE.toString()),
inputStream);
Assert.assertEquals(mimeParts.size(), 2);
List<MIMEPart> childParts = MultipartDecoder.decodeBodyParts(mimeParts.get(1).getContentType(),
mimeParts.get(1).readOnce());
mimeParts.get(1).readOnce());
Assert.assertEquals(childParts.size(), 2);
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
Expand All @@ -223,9 +224,9 @@ public void testBase64EncodeByteChannel() {
Base64ByteChannel base64ByteChannel = new Base64ByteChannel(inputStream);
byteChannelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, new Base64Wrapper(base64ByteChannel));
BValue[] returnValues = BRunUtil.invoke(compileResult, "testBase64EncodeByteChannel",
new Object[]{ byteChannelStruct });
new Object[]{byteChannelStruct});
Assert.assertFalse(returnValues == null || returnValues.length == 0 || returnValues[0] == null,
"Invalid return value");
"Invalid return value");
BMap<String, BValue> decodedByteChannel = (BMap<String, BValue>) returnValues[0];
Channel byteChannel = (Channel) decodedByteChannel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
try {
Expand All @@ -234,4 +235,26 @@ public void testBase64EncodeByteChannel() {
Assert.fail(e.getMessage());
}
}

@Test()
public void testBoundaryWithQuotes() {
String path = "/multipart/boundaryCheck";
HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_GET);
HttpCarbonMessage response = Services.invoke(EP_PORT, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
InputStream inputStream = new HttpMessageDataStreamer(response).getInputStream();
try {
List<MIMEPart> mimeParts = MultipartDecoder.decodeBodyParts(
"multipart/mixed; boundary=\"------=_Part_0_814051860.1675096572056\"", inputStream);
Assert.assertEquals(mimeParts.size(), 1);
ObjectValue bodyPart = createEntityObject();
EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(0));
Object jsonData = EntityBodyHandler.constructJsonDataSource(bodyPart);
Assert.assertNotNull(jsonData);
Assert.assertEquals(org.ballerinalang.jvm.values.utils.StringUtils.getJsonString(jsonData),
"{\"" + "bodyPart" + "\":\"" + "jsonPart" + "\"}");
} catch (MimeTypeParseException e) {
log.error("Error occurred while testing mulitpart/mixed encoding", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ service test on mockEP {
}
checkpanic caller->respond(outResponse);
}

@http:ResourceConfig {
methods:["GET"],
path:"/boundaryCheck"
}
resource function multipartOutResponseWithBoundaryQuotes(http:Caller caller, http:Request request) {
mime:Entity bodyPart1 = new;
bodyPart1.setJson({"bodyPart":"jsonPart"});
mime:Entity[] bodyParts = [bodyPart1];

//Set the body parts to outbound response.
http:Response outResponse = new;
string contentType = mime:MULTIPART_MIXED + "; boundary=\"------=_Part_0_814051860.1675096572056\"";
outResponse.setBodyParts(bodyParts, contentType);
checkpanic caller->respond(outResponse);
}
}

0 comments on commit 914dc6d

Please sign in to comment.