Search in sources :

Example 96 with Function

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"));
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 97 with Function

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");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 98 with Function

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"));
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 99 with Function

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());
}
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 100 with Function

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());
}
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) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)94 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)49 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)44 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)39 ArrayList (java.util.ArrayList)37 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)35 BString (org.ballerinalang.model.values.BString)30 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)29 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)20 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)20 BJSON (org.ballerinalang.model.values.BJSON)19 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)19 List (java.util.List)18 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)18 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)16 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)15 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)14 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14