use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class HTTPSessionSubMethodsTest method testGetCreateTimeFunctionError.
@Test(description = "Test for GetCreateTime Function error")
public void testGetCreateTimeFunctionError() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/new3", "GET");
HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertNotNull(responseMsgPayload);
Assert.assertTrue(responseMsgPayload.contains("No such session in progress"));
}
use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class HTTPSessionSubMethodsTest method testIsNewFunctiontwoAttempts.
@Test(description = "Test for isNew Function two attempts")
public void testIsNewFunctiontwoAttempts() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/new1", "GET");
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, "true");
String cookie = response.getHeader(RESPONSE_COOKIE_HEADER);
String sessionId = CookieUtils.getCookie(cookie).value;
cMsg = MessageUtils.generateHTTPMessage("/sample2/new1", "GET");
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, "false");
}
use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class HTTPSessionSubMethodsTest method testGetLastAccessedTimeFunctionError.
@Test(description = "Test for GetLastAccessed Function error")
public void testGetLastAccessedTimeFunctionError() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/new5", "GET");
HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertNotNull(responseMsgPayload);
Assert.assertTrue(responseMsgPayload.contains("No such session in progress"));
}
use of org.wso2.carbon.apimgt.core.models.Function 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.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class HttpCachingClientTest method testHasAWeakValidator.
@Test(description = "Test for the hasAWeakValidator() function which determines whether a 304 response contains a" + " weak validator", enabled = false)
public void testHasAWeakValidator() {
final String lastModifiedHeader = "Thu, 01 Mar 2018 15:36:34 GMT";
final String etagHeader = "1sps79e:q0efehi8";
BStruct validationResponse = BCompileUtil.createAndGetStruct(compileResult.getProgFile(), PROTOCOL_PACKAGE_HTTP, RESPONSE);
HTTPCarbonMessage validationResponseMsg = HttpUtil.createHttpCarbonMessage(false);
validationResponseMsg.setProperty(HTTP_STATUS_CODE, 200);
validationResponseMsg.setHeader(LAST_MODIFIED, lastModifiedHeader);
validationResponseMsg.setHeader(ETAG, etagHeader);
initInboundResponse(validationResponse, validationResponseMsg);
// Not testing with a null validation response since this function is not called without a null check
BValue[] inputArg = { validationResponse, new BString(etagHeader) };
BValue[] returns = BRunUtil.invoke(compileResult, "hasAWeakValidator", inputArg);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
inputArg[1] = new BString(null);
returns = BRunUtil.invoke(compileResult, "hasAWeakValidator", inputArg);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
validationResponseMsg.removeHeader(LAST_MODIFIED);
initInboundResponse(validationResponse, validationResponseMsg);
inputArg[1] = new BString(etagHeader);
returns = BRunUtil.invoke(compileResult, "hasAWeakValidator", inputArg);
Assert.assertFalse(((BBoolean) returns[0]).booleanValue());
// passing a weak validator
inputArg[1] = new BString("W/" + etagHeader);
returns = BRunUtil.invoke(compileResult, "hasAWeakValidator", inputArg);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue());
}
Aggregations