use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.
the class HttpCachingClientTest method testIsServingStaleProhibited.
@Test(description = "Test for the isServingStaleProhibited() function which determines whether serving stale " + "responses is prohibited.", enabled = false)
public void testIsServingStaleProhibited() {
RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(REQUEST_CACHE_CONTROL));
ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
BValue[] inputArgs = { requestCacheControl.getStruct(), responseCacheControl.getStruct() };
BValue[] returns;
// No prohibitive directives set
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
// Setting no-store in request
requestCacheControl.setNoStore(true);
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
// Setting no-cache in request
requestCacheControl.setNoStore(false).setNoCache(true);
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
// Setting must-revalidate in response
requestCacheControl.setNoCache(false);
responseCacheControl.setMustRevalidate(true);
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
// Setting must-revalidate in response
responseCacheControl.setMustRevalidate(false).setSMaxAge(10);
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
// Setting must-revalidate in response
responseCacheControl.setSMaxAge(-1).setProxyRevalidate(true);
returns = BRunUtil.invoke(compileResult, "isServingStaleProhibited", inputArgs);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
}
use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.
the class HttpCachingClientTest method initInboundResponse.
private void initInboundResponse(BStruct inResponse, HTTPCarbonMessage inResponseMsg) {
ResponseCacheControlStruct cacheControl = new ResponseCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
initInboundResponse(inResponse, cacheControl, inResponseMsg);
}
use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.
the class ResponseNativeFunctionSuccessTest method testGetHeaders.
@Test(description = "Test GetHeaders function within a function")
public void testGetHeaders() {
BStruct inResponse = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, inResStruct);
HTTPCarbonMessage inResponseMsg = HttpUtil.createHttpCarbonMessage(false);
HttpHeaders headers = inResponseMsg.getHeaders();
headers.set("test-header", APPLICATION_FORM);
headers.add("test-header", TEXT_PLAIN);
inResponseMsg.setProperty(HttpConstants.HTTP_STATUS_CODE, 200);
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);
BString key = new BString("test-header");
BValue[] inputArg = { inResponse, key };
BValue[] returnVals = BRunUtil.invoke(result, "testGetHeaders", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertEquals(((BStringArray) returnVals[0]).get(0), APPLICATION_FORM);
Assert.assertEquals(((BStringArray) returnVals[0]).get(1), TEXT_PLAIN);
}
use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.
the class ResponseNativeFunctionSuccessTest method testGetHeader.
@Test
public void testGetHeader() {
BStruct inResponse = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, inResStruct);
HTTPCarbonMessage inResponseMsg = HttpUtil.createHttpCarbonMessage(false);
inResponseMsg.setHeader(HttpHeaderNames.CONTENT_TYPE.toString(), APPLICATION_FORM);
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);
BString key = new BString(HttpHeaderNames.CONTENT_TYPE.toString());
BValue[] inputArg = { inResponse, key };
BValue[] returnVals = BRunUtil.invoke(result, "testGetHeader", inputArg);
Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
Assert.assertEquals(returnVals[0].stringValue(), APPLICATION_FORM);
}
Aggregations