Search in sources :

Example 56 with Header

use of org.wso2.carbon.messaging.Header 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 57 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class CompressionConfigurationTest method testAutoCompress.

@Test(description = "Test Compression.AUTO, with no Accept-Encoding header. The response here means the one " + "that should be sent to transport, not to end user.")
public void testAutoCompress() {
    HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage("/autoCompress", HttpConstants.HTTP_METHOD_GET);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertNull(response.getHeaders().get(HttpHeaderNames.CONTENT_ENCODING.toString()), "The content-encoding header should be null and the identity which means no compression " + "should be done to the response");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 58 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class CompressionConfigurationTest method testAlwaysCompress.

@Test(description = "Test Compression.ALWAYS, with no Accept-Encoding header. The response here means the one " + "that should be sent to transport, not to end user.")
public void testAlwaysCompress() {
    HTTPTestRequest inRequestMsg = MessageUtils.generateHTTPMessage("/alwaysCompress", HttpConstants.HTTP_METHOD_GET);
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, inRequestMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(response.getHeader(HttpHeaderNames.CONTENT_ENCODING.toString()), ENCODING_GZIP, "The content-encoding header should be gzip.");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 59 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class HTTPCorsTest method testPreFlightReqwithNoOrigin.

@Test(description = "Test preflight without origin header")
public void testPreFlightReqwithNoOrigin() {
    String path = "/hello1/test1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "OPTIONS", "Hello there");
    cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD.toString(), HttpConstants.HTTP_METHOD_POST);
    cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS.toString(), "X-PINGOTHER");
    HTTPCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);
    Assert.assertNotNull(response);
    assertEqualsCorsResponse(response, 200, null, null, null, null, null);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 60 with Header

use of org.wso2.carbon.messaging.Header in project ballerina by ballerina-lang.

the class HTTPCorsTest method testPreFlightReqwithNoMethod.

@Test(description = "Test preflight without Request Method header")
public void testPreFlightReqwithNoMethod() {
    String path = "/hello1/test1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "OPTIONS", "Hello there");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.wso2.com");
    cMsg.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS.toString(), "X-PINGOTHER");
    HTTPCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);
    Assert.assertNotNull(response);
    assertEqualsCorsResponse(response, 200, null, null, null, null, null);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)103 HashMap (java.util.HashMap)95 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)95 Test (org.testng.annotations.Test)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)35 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)33 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)30 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)28 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 ArrayList (java.util.ArrayList)23 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 CharonException (org.wso2.charon3.core.exceptions.CharonException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)18 Header (org.wso2.carbon.messaging.Header)17 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)16 IOException (java.io.IOException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 Map (java.util.Map)14