Search in sources :

Example 1 with JsonpConfigBean

use of io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean in project apiman-plugins by apiman.

the class JsonpPolicyTest method testParseEmptyConfiguration.

@Test
public void testParseEmptyConfiguration() {
    // given
    String config = "{}";
    // when
    JsonpConfigBean jsonpConfig = jsonpPolicy.parseConfiguration(config);
    // then
    assertNull(jsonpConfig.getCallbackParamName());
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) Test(org.junit.Test)

Example 2 with JsonpConfigBean

use of io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean in project apiman-plugins by apiman.

the class JsonpPolicyTest method testParseRealConfiguration.

@Test
public void testParseRealConfiguration() {
    // given
    String parameterName = "jsonp";
    String config = "{\"callbackParamName\":\"" + parameterName + "\"}";
    // when
    JsonpConfigBean jsonpConfig = jsonpPolicy.parseConfiguration(config);
    // then
    assertEquals(parameterName, jsonpConfig.getCallbackParamName());
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) Test(org.junit.Test)

Example 3 with JsonpConfigBean

use of io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean in project apiman-plugins by apiman.

the class JsonpPolicyTest method shouldNotSaveCallbackFunctionNameInContextWhenNotPresent.

@Test
public void shouldNotSaveCallbackFunctionNameInContextWhenNotPresent() throws Exception {
    // given
    JsonpConfigBean config = new JsonpConfigBean();
    config.setCallbackParamName("testParam");
    QueryMap queryParams = new QueryMap();
    ApiRequest request = new ApiRequest();
    request.setQueryParams(queryParams);
    IPolicyChain<ApiRequest> chain = mock(IPolicyChain.class);
    // when
    jsonpPolicy.doApply(request, sContext, config, chain);
    // then
    assertNull(sContext.getAttribute("callbackFunctionName", null));
    verify(chain).doApply(request);
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Test(org.junit.Test)

Example 4 with JsonpConfigBean

use of io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean in project apiman-plugins by apiman.

the class JsonpPolicyTest method shouldRemoveCallbackParamNameFromRequest.

@Test
public void shouldRemoveCallbackParamNameFromRequest() throws Exception {
    // given
    JsonpConfigBean config = new JsonpConfigBean();
    config.setCallbackParamName("testParam");
    QueryMap queryParams = new QueryMap();
    queryParams.put("testParam", "testFunction");
    ApiRequest request = new ApiRequest();
    request.setQueryParams(queryParams);
    IPolicyChain<ApiRequest> chain = mock(IPolicyChain.class);
    // when
    jsonpPolicy.doApply(request, sContext, config, chain);
    // then
    assertNull(request.getQueryParams().get("testParam"));
    verify(chain).doApply(request);
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) QueryMap(io.apiman.gateway.engine.beans.util.QueryMap) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Test(org.junit.Test)

Example 5 with JsonpConfigBean

use of io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean in project apiman-plugins by apiman.

the class JsonpPolicyTest method changeResponseWhenCallbackParamNameIsSavedInContext.

@Test
public void changeResponseWhenCallbackParamNameIsSavedInContext() throws Exception {
    JsonpConfigBean config = new JsonpConfigBean();
    // given
    String functionName = "testFunction";
    sContext.setAttribute(JsonpPolicy.CALLBACK_FUNCTION_NAME, functionName);
    ApiResponse response = new ApiResponse();
    String json = "{\"name\": \"test\"}";
    IApimanBuffer chunk = new ByteBuffer(json.getBytes().length);
    chunk.append(json);
    IAsyncHandler<IApimanBuffer> bodyHandler = mock(IAsyncHandler.class);
    // when
    IReadWriteStream<ApiResponse> responseDataHandler = jsonpPolicy.getResponseDataHandler(response, sContext, config);
    ApiResponse head = responseDataHandler.getHead();
    responseDataHandler.bodyHandler(bodyHandler);
    responseDataHandler.write(chunk);
    responseDataHandler.end();
    // then
    assertSame(response, head);
    verify(bodyHandler).handle(refEq(new ByteBuffer("testFunction(")));
    verify(bodyHandler).handle(refEq(new ByteBuffer(json)));
    verify(bodyHandler).handle(refEq(new ByteBuffer(")")));
}
Also used : JsonpConfigBean(io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ByteBuffer(io.apiman.gateway.engine.io.ByteBuffer) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Test(org.junit.Test)

Aggregations

JsonpConfigBean (io.apiman.plugins.jsonp_policy.beans.JsonpConfigBean)7 Test (org.junit.Test)7 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)3 QueryMap (io.apiman.gateway.engine.beans.util.QueryMap)3 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)2 ByteBuffer (io.apiman.gateway.engine.io.ByteBuffer)1 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)1