Search in sources :

Example 51 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class PushPromiseNativeFunctionTest method testGetHeaders.

@Test(description = "Test getHeaders function of PushPromise")
public void testGetHeaders() {
    BStruct promise = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, promiseStruct);
    Http2PushPromise http2PushPromise = new Http2PushPromise(HttpConstants.HTTP_METHOD_GET, HttpConstants.DEFAULT_BASE_PATH);
    String headerName = "header";
    String headerValue1 = "value1";
    String headerValue2 = "value2";
    http2PushPromise.addHeader(headerName, headerValue1);
    http2PushPromise.addHeader(headerName, headerValue2);
    HttpUtil.populatePushPromiseStruct(promise, http2PushPromise);
    BString key = new BString(headerName);
    BValue[] inputArg = { promise, key };
    BValue[] returnVal = BRunUtil.invoke(result, "testGetHeaders", inputArg);
    Assert.assertFalse(returnVal == null || returnVal.length == 0 || returnVal[0] == null, "Invalid Return Values.");
    Assert.assertEquals(((BStringArray) returnVal[0]).get(0), headerValue1);
    Assert.assertEquals(((BStringArray) returnVal[0]).get(1), headerValue2);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 52 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class RequestNativeFunctionSuccessTest method testServiceGetStringPayload.

@Test(description = "Test GetStringPayload function within a service")
public void testServiceGetStringPayload() {
    String value = "ballerina";
    String path = "/hello/GetStringPayload";
    List<Header> headers = new ArrayList<>();
    headers.add(new Header(HttpHeaderNames.CONTENT_TYPE.toString(), TEXT_PLAIN));
    HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_POST, headers, value);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(ResponseReader.getReturnValue(response), value);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Header(org.wso2.carbon.messaging.Header) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 53 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class RequestNativeFunctionSuccessTest method testServiceGetJsonPayload.

@Test(description = "Test GetJsonPayload function within a service")
public void testServiceGetJsonPayload() {
    String key = "lang";
    String value = "ballerina";
    String path = "/hello/getJsonPayload";
    String jsonString = "{\"" + key + "\":\"" + value + "\"}";
    List<Header> headers = new ArrayList<>();
    headers.add(new Header(HttpHeaderNames.CONTENT_TYPE.toString(), APPLICATION_JSON));
    HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_POST, headers, jsonString);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(new BJSON(ResponseReader.getReturnValue(response)).value().stringValue(), value);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Header(org.wso2.carbon.messaging.Header) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 54 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class HTTPSessionEssentialMethodsTest method testSessionForStructAttribute.

@Test(description = "Test for struct attribute")
public void testSessionForStructAttribute() {
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header(HttpHeaderNames.CONTENT_TYPE.toString(), TEXT_PLAIN));
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/myStruct", "POST", headers, "wso2");
    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("/sample2/myStruct", "POST", headers, "chamil");
    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.assertEquals(responseMsgPayload, "wso2");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Header(org.wso2.carbon.messaging.Header) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 55 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class MessageUtils method generateHTTPMessage.

public static HTTPTestRequest generateHTTPMessage(String path, String method, List<Header> headers, String payload) {
    HTTPTestRequest carbonMessage = getHttpTestRequest(path, method);
    HttpHeaders httpHeaders = carbonMessage.getHeaders();
    if (headers != null) {
        for (Header header : headers) {
            httpHeaders.set(header.getName(), header.getValue());
        }
    }
    if (payload != null) {
        carbonMessage.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(payload.getBytes())));
    } else {
        carbonMessage.addHttpContent(new DefaultLastHttpContent());
    }
    return carbonMessage;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Header(org.wso2.carbon.messaging.Header) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)103 HashMap (java.util.HashMap)95 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)95 Test (org.testng.annotations.Test)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)35 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)33 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)30 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)28 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 ArrayList (java.util.ArrayList)23 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 CharonException (org.wso2.charon3.core.exceptions.CharonException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)18 Header (org.wso2.carbon.messaging.Header)17 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)16 IOException (java.io.IOException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 Map (java.util.Map)14