Search in sources :

Example 16 with HTTPTestRequest

use of org.ballerinalang.test.services.testutils.HTTPTestRequest 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)

Example 17 with HTTPTestRequest

use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.

the class MultipartFormDataDecoderTest method testTextBodyPartAsFileUpload.

@Test(description = "Test sending a multipart request with a text body part where the content is kept in a file")
public void testTextBodyPartAsFileUpload() {
    String path = "/test/textbodypart";
    Map<String, Object> messageMap = Util.createPrerequisiteMessages(path, MULTIPART_FORM_DATA, result);
    ArrayList<BStruct> bodyParts = new ArrayList<>();
    bodyParts.add(Util.getTextFilePart(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 text as a file 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)

Example 18 with HTTPTestRequest

use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.

the class MultipartFormDataDecoderTest method testTextBodyPartWith8BitEncoding.

@Test(description = "Test sending a multipart request with a text body part that has 8 bit transfer encoding")
public void testTextBodyPartWith8BitEncoding() {
    String path = "/test/textbodypart";
    Map<String, Object> messageMap = Util.createPrerequisiteMessages(path, MULTIPART_FORM_DATA, result);
    ArrayList<BStruct> bodyParts = new ArrayList<>();
    bodyParts.add(Util.getTextFilePartWithEncoding(CONTENT_TRANSFER_ENCODING_8_BIT, "èlllll", 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), "èlllll");
}
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)

Example 19 with HTTPTestRequest

use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.

the class Util method createPrerequisiteMessages.

/**
 * Create prerequisite messages that are needed to proceed with the test cases.
 *
 * @param path                Represent path to the ballerina resource
 * @param topLevelContentType Content type that needs to be set to the top level message
 * @param result              Result of ballerina file compilation
 * @return A map of relevant messages
 */
static Map<String, Object> createPrerequisiteMessages(String path, String topLevelContentType, CompileResult result) {
    Map<String, Object> messageMap = new HashMap<>();
    BStruct request = getRequestStruct(result);
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessageForMultiparts(path, HttpConstants.HTTP_METHOD_POST);
    HttpUtil.addCarbonMsg(request, cMsg);
    BStruct entity = getEntityStruct(result);
    MimeUtil.setContentType(getMediaTypeStruct(result), entity, topLevelContentType);
    messageMap.put(CARBON_MESSAGE, cMsg);
    messageMap.put(BALLERINA_REQUEST, request);
    messageMap.put(MULTIPART_ENTITY, entity);
    return messageMap;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BString(org.ballerinalang.model.values.BString)

Example 20 with HTTPTestRequest

use of org.ballerinalang.test.services.testutils.HTTPTestRequest in project ballerina by ballerina-lang.

the class IdentifierLiteralServiceTest method testIdentifierLiteralsInPayload.

@Test(description = "Test identifier literals payload", enabled = false)
public void testIdentifierLiteralsInPayload() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource2", "GET");
    HTTPCarbonMessage response = Services.invokeNew(application, cMsg);
    Assert.assertNotNull(response);
    String payload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(payload, "hello");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Aggregations

HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)243 Test (org.testng.annotations.Test)241 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)233 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)172 BJSON (org.ballerinalang.model.values.BJSON)108 BString (org.ballerinalang.model.values.BString)32 ArrayList (java.util.ArrayList)26 BStruct (org.ballerinalang.model.values.BStruct)16 Header (org.wso2.carbon.messaging.Header)15 InputStream (java.io.InputStream)4 ExecutorService (java.util.concurrent.ExecutorService)4 Semaphore (java.util.concurrent.Semaphore)4 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 MimeTypeParseException (javax.activation.MimeTypeParseException)2 BValue (org.ballerinalang.model.values.BValue)2 BXMLItem (org.ballerinalang.model.values.BXMLItem)2 RequestCacheControlStruct (org.ballerinalang.net.http.caching.RequestCacheControlStruct)2 StringDataSource (org.ballerinalang.runtime.message.StringDataSource)2