Search in sources :

Example 6 with ResponseCacheControlStruct

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());
}
Also used : BValue(org.ballerinalang.model.values.BValue) RequestCacheControlStruct(org.ballerinalang.net.http.caching.RequestCacheControlStruct) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Example 7 with ResponseCacheControlStruct

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);
}
Also used : ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct)

Example 8 with ResponseCacheControlStruct

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);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Example 9 with ResponseCacheControlStruct

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);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Aggregations

ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)9 BStruct (org.ballerinalang.model.values.BStruct)7 BValue (org.ballerinalang.model.values.BValue)7 Test (org.testng.annotations.Test)7 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)6 BString (org.ballerinalang.model.values.BString)5 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 BBoolean (org.ballerinalang.model.values.BBoolean)3 RequestCacheControlStruct (org.ballerinalang.net.http.caching.RequestCacheControlStruct)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1