Search in sources :

Example 91 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project ballerina by ballerina-lang.

the class ResponseNativeFunctionSuccessTest method testGetContentLength.

@Test
public void testGetContentLength() {
    BStruct inResponse = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, inResStruct);
    HTTPCarbonMessage inResponseMsg = HttpUtil.createHttpCarbonMessage(false);
    String payload = "ballerina";
    inResponseMsg.setHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), String.valueOf(payload.length()));
    inResponseMsg.setProperty(HttpConstants.HTTP_STATUS_CODE, 200);
    HttpUtil.addCarbonMsg(inResponse, inResponseMsg);
    BStruct entity = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageMime, entityStruct);
    BStruct mediaType = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageMime, mediaTypeStruct);
    BStruct cacheControl = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, resCacheControlStruct);
    ResponseCacheControlStruct cacheControlStruct = new ResponseCacheControlStruct(cacheControl);
    HttpUtil.populateInboundResponse(inResponse, entity, mediaType, cacheControlStruct, inResponseMsg);
    BValue[] inputArg = { inResponse };
    BValue[] returnVals = BRunUtil.invoke(result, "testGetContentLength", inputArg);
    Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
    Assert.assertEquals(String.valueOf(payload.length()), returnVals[0].stringValue());
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Example 92 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project ballerina by ballerina-lang.

the class ResponseNativeFunctionSuccessTest method testServiceGetStringPayload.

@Test(description = "Test GetStringPayload function within a service")
public void testServiceGetStringPayload() {
    String value = "ballerina";
    String path = "/hello/GetStringPayload/" + value;
    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");
    Assert.assertEquals(StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream()), value);
}
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) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 93 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project ballerina by ballerina-lang.

the class ResponseNativeFunctionSuccessTest method testForwardMethod.

@Test
public void testForwardMethod() {
    String path = "/hello/11";
    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");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 94 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project ballerina by ballerina-lang.

the class HTTPSessionEssentialMethodsTest method testCheckPathValiditygetSession.

@Test(description = "Test for path limitation with getSession")
public void testCheckPathValiditygetSession() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/echoName", "GET");
    HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
    Assert.assertNotNull(response);
    String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    ;
    Assert.assertNotNull(responseMsgPayload);
    Assert.assertEquals(responseMsgPayload, "wso2");
    String cookie = response.getHeader(RESPONSE_COOKIE_HEADER);
    String sessionId = CookieUtils.getCookie(cookie).value;
    cMsg = MessageUtils.generateHTTPMessage("/counter/echo2", "GET");
    cMsg.setHeader(COOKIE_HEADER, SESSION_ID + sessionId);
    response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
    Assert.assertNotNull(response);
    responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertNotNull(responseMsgPayload);
    Assert.assertTrue(responseMsgPayload.contains("failed to get session: " + "/counter is not an allowed path"));
}
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)

Example 95 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project ballerina by ballerina-lang.

the class HTTPSessionEssentialMethodsTest method testHttpOnlyFlagInSessionCookie.

@Test(description = "Test for HttpOnly flag in session cookie")
public void testHttpOnlyFlagInSessionCookie() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample/test1", "GET");
    HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
    Assert.assertNotNull(responseMsg);
    String cookie = responseMsg.getHeader(RESPONSE_COOKIE_HEADER);
    Assert.assertNotNull(cookie);
    Assert.assertTrue(CookieUtils.getCookie(cookie).httpOnly);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Aggregations

HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)308 Test (org.testng.annotations.Test)247 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)233 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)179 BJSON (org.ballerinalang.model.values.BJSON)108 BString (org.ballerinalang.model.values.BString)45 BStruct (org.ballerinalang.model.values.BStruct)43 Request (org.wso2.msf4j.Request)34 ArrayList (java.util.ArrayList)25 BValue (org.ballerinalang.model.values.BValue)19 Header (org.wso2.carbon.messaging.Header)14 Tracer (org.ballerinalang.util.tracer.Tracer)11 HttpCarbonMessage (org.wso2.transport.http.netty.message.HttpCarbonMessage)10 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)9 IOException (java.io.IOException)8 ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)7 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)5 Semaphore (java.util.concurrent.Semaphore)5 APIMgtSecurityException (org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException)5 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)4