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);
}
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);
}
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);
}
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");
}
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;
}
Aggregations