use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.
the class HttpCachingClientTest method testIsFreshResponse.
@Test(description = "Test for the isFreshResponse() function which determines whether a cached response is still " + "fresh.", enabled = false)
public void testIsFreshResponse() {
final String dateHeader = "Thu, 01 Mar 2018 15:36:34 GMT";
// 600 second difference
final String expiresHeader = "Thu, 01 Mar 2018 15:46:34 GMT";
BStruct cachedResponse = BCompileUtil.createAndGetStruct(compileResult.getProgFile(), PROTOCOL_PACKAGE_HTTP, RESPONSE);
ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
responseCacheControl.setMaxAge(300).setSMaxAge(300);
HTTPCarbonMessage cachedResponseMsg = HttpUtil.createHttpCarbonMessage(false);
cachedResponseMsg.setProperty(HTTP_STATUS_CODE, 200);
cachedResponseMsg.setHeader(AGE, String.valueOf(200));
cachedResponseMsg.setHeader(DATE, dateHeader);
cachedResponseMsg.setHeader(EXPIRES, expiresHeader);
initInboundResponse(cachedResponse, responseCacheControl, cachedResponseMsg);
HttpHeaders responseHeaders = cachedResponseMsg.getHeaders();
BValue[] inputArgs = { cachedResponse, new BBoolean(true) };
BValue[] returns;
// When all 3 parameters for freshness life time is set
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
responseHeaders.set(AGE, "350");
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
// When only max-age and Expires header are there
responseCacheControl.setSMaxAge(-1);
inputArgs[1] = new BBoolean(false);
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
responseHeaders.set(AGE, "200");
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
// When only Expires header is there
responseCacheControl.setMaxAge(-1);
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
responseHeaders.set(AGE, "650");
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
// When there are none of the above 3
responseHeaders.remove(EXPIRES);
returns = BRunUtil.invoke(compileResult, "isFreshResponse", inputArgs);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
}
use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.
the class CompressionConfigurationTest method testAutoCompress.
@Test(description = "Test Compression.AUTO, with no Accept-Encoding header. The response here means the one " + "that should be sent to transport, not to end user.")
public void testAutoCompress() {
HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage("/autoCompress", HttpConstants.HTTP_METHOD_GET);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
Assert.assertNull(response.getHeaders().get(HttpHeaderNames.CONTENT_ENCODING.toString()), "The content-encoding header should be null and the identity which means no compression " + "should be done to the response");
}
use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.
the class CompressionConfigurationTest method testAlwaysCompress.
@Test(description = "Test Compression.ALWAYS, with no Accept-Encoding header. The response here means the one " + "that should be sent to transport, not to end user.")
public void testAlwaysCompress() {
HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage("/alwaysCompress", HttpConstants.HTTP_METHOD_GET);
HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
Assert.assertNotNull(response, "Response message not found");
Assert.assertEquals(response.getHeader(HttpHeaderNames.CONTENT_ENCODING.toString()), ENCODING_GZIP, "The content-encoding header should be gzip.");
}
use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.
the class HTTPCorsTest method testPreFlightReqwithNoOrigin.
@Test(description = "Test preflight without origin header")
public void testPreFlightReqwithNoOrigin() {
String path = "/hello1/test1";
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "OPTIONS", "Hello there");
cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD.toString(), HttpConstants.HTTP_METHOD_POST);
cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS.toString(), "X-PINGOTHER");
HTTPCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);
Assert.assertNotNull(response);
assertEqualsCorsResponse(response, 200, null, null, null, null, null);
}
use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.
the class HTTPCorsTest method testPreFlightReqwithNoMethod.
@Test(description = "Test preflight without Request Method header")
public void testPreFlightReqwithNoMethod() {
String path = "/hello1/test1";
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "OPTIONS", "Hello there");
cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.wso2.com");
cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS.toString(), "X-PINGOTHER");
HTTPCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);
Assert.assertNotNull(response);
assertEqualsCorsResponse(response, 200, null, null, null, null, null);
}
Aggregations