Search in sources :

Example 1 with ResponseCacheControlStruct

use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.

the class AbstractHTTPAction method createResponseStruct.

/**
 * Creates InResponse using the native {@code HTTPCarbonMessage}.
 *
 * @param context           ballerina context
 * @param httpCarbonMessage the HTTPCarbonMessage
 * @return the Response struct
 */
BStruct createResponseStruct(Context context, HTTPCarbonMessage httpCarbonMessage) {
    BStruct responseStruct = createStruct(context, HttpConstants.RESPONSE, HttpConstants.PROTOCOL_PACKAGE_HTTP);
    BStruct entity = createStruct(context, HttpConstants.ENTITY, PROTOCOL_PACKAGE_MIME);
    BStruct mediaType = createStruct(context, MEDIA_TYPE, PROTOCOL_PACKAGE_MIME);
    ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(context.getProgramFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
    HttpUtil.populateInboundResponse(responseStruct, entity, mediaType, responseCacheControl, httpCarbonMessage);
    return responseStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct)

Example 2 with ResponseCacheControlStruct

use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.

the class ResponseNativeFunctionSuccessTest method testGetContentLength.

@Test
public void testGetContentLength() {
    BStruct inResponse = BCompileUtil.createAndGetStruct(result.getProgFile(), protocolPackageHttp, inResStruct);
    HTTPCarbonMessage inResponseMsg = HttpUtil.createHttpCarbonMessage(false);
    String payload = "ballerina";
    inResponseMsg.setHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), String.valueOf(payload.length()));
    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);
    BValue[] inputArg = { inResponse };
    BValue[] returnVals = BRunUtil.invoke(result, "testGetContentLength", inputArg);
    Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
    Assert.assertEquals(String.valueOf(payload.length()), returnVals[0].stringValue());
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Example 3 with ResponseCacheControlStruct

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

Example 4 with ResponseCacheControlStruct

use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.

the class HttpCachingClientTest method testIsStaleResponseAccepted.

@Test(description = "Test for the isStaleResponseAccepted() function which determines whether stale responses are" + " acceptable", enabled = false)
public void testIsStaleResponseAccepted() {
    boolean isSharedCache = false;
    RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(REQUEST_CACHE_CONTROL));
    requestCacheControl.setMaxStale(Long.MAX_VALUE);
    BStruct cachedResponse = BCompileUtil.createAndGetStruct(compileResult.getProgFile(), PROTOCOL_PACKAGE_HTTP, RESPONSE);
    ResponseCacheControlStruct responseCacheControl = new ResponseCacheControlStruct(compileResult.getProgFile().getPackageInfo(PROTOCOL_PACKAGE_HTTP).getStructInfo(RESPONSE_CACHE_CONTROL));
    HTTPCarbonMessage cachedResponseMsg = HttpUtil.createHttpCarbonMessage(false);
    cachedResponseMsg.setProperty(HTTP_STATUS_CODE, 200);
    cachedResponseMsg.setHeader(AGE, "10");
    ;
    initInboundResponse(cachedResponse, responseCacheControl, cachedResponseMsg);
    BValue[] inputArgs = { requestCacheControl.getStruct(), cachedResponse, new BBoolean(isSharedCache) };
    BValue[] returns = BRunUtil.invoke(compileResult, "isStaleResponseAccepted", inputArgs);
    Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
    requestCacheControl.setMaxStale(-1);
    returns = BRunUtil.invoke(compileResult, "isStaleResponseAccepted", inputArgs);
    Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
    requestCacheControl.setMaxStale(7);
    responseCacheControl.setMaxAge(5);
    returns = BRunUtil.invoke(compileResult, "isStaleResponseAccepted", inputArgs);
    Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
    requestCacheControl.setMaxStale(7);
    responseCacheControl.setMaxAge(3);
    returns = BRunUtil.invoke(compileResult, "isStaleResponseAccepted", inputArgs);
    Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) RequestCacheControlStruct(org.ballerinalang.net.http.caching.RequestCacheControlStruct) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Test(org.testng.annotations.Test)

Example 5 with ResponseCacheControlStruct

use of org.ballerinalang.net.http.caching.ResponseCacheControlStruct in project ballerina by ballerina-lang.

the class HttpCachingClientTest method testGetFreshnessLifetime.

// To ensure freshness calculation adheres to https://tools.ietf.org/html/rfc7234#section-4.2.1
@Test(description = "Test for the determination of freshness lifetime of a cached response", enabled = false)
public void testGetFreshnessLifetime() {
    // First, set all potential fields that can be used for freshness calculation so that the test can determine
    // if the correct precedence order is followed.
    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.setSMaxAge(20).setMaxAge(15);
    HTTPCarbonMessage cachedResponseMsg = HttpUtil.createHttpCarbonMessage(false);
    cachedResponseMsg.setProperty(HTTP_STATUS_CODE, 200);
    cachedResponseMsg.setHeader(DATE, dateHeader);
    cachedResponseMsg.setHeader(EXPIRES, expiresHeader);
    initInboundResponse(cachedResponse, responseCacheControl, cachedResponseMsg);
    // Parameters: cached response and whether the cache is a shared cache
    BValue[] inputArgs = { cachedResponse, new BBoolean(true) };
    BValue[] returns;
    // If the cache is shared and the s-maxage directive is present, that's the freshness lifetime
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 20);
    // If the cache is not shared, s-maxage cannot be used
    inputArgs[1] = new BBoolean(false);
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 15);
    // If the cache is shared, but s-maxage is not set
    inputArgs[1] = new BBoolean(true);
    responseCacheControl.setSMaxAge(-1);
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 15);
    // When both max-age and s-maxage are not applicable
    inputArgs[1] = new BBoolean(false);
    responseCacheControl.setMaxAge(-1);
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 600);
    // When there are multiple values for the Date header
    HttpHeaders responseHeaders = cachedResponseMsg.getHeaders();
    responseHeaders.add(DATE, "Thu, 01 Mar 2018 15:37:34 GMT");
    responseHeaders.add(DATE, "Thu, 01 Mar 2018 15:56:34 GMT");
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 0);
    // When there are multiple values for the Expires header
    responseHeaders.set(DATE, dateHeader);
    responseHeaders.add(EXPIRES, "Thu, 01 Mar 2018 15:37:34 GMT");
    responseHeaders.add(EXPIRES, "Thu, 01 Mar 2018 15:56:34 GMT");
    returns = BRunUtil.invoke(compileResult, "getFreshnessLifetime", inputArgs);
    Assert.assertEquals(((BInteger) returns[0]).intValue(), 0);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BValue(org.ballerinalang.model.values.BValue) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) 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