Search in sources :

Example 31 with IApimanBuffer

use of io.apiman.gateway.engine.io.IApimanBuffer 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

IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)31 ByteBuffer (io.apiman.gateway.engine.io.ByteBuffer)12 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)11 Test (org.junit.Test)11 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)7 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)7 AbstractStream (io.apiman.gateway.engine.io.AbstractStream)6 IAsyncHandler (io.apiman.gateway.engine.async.IAsyncHandler)5 IEngineResult (io.apiman.gateway.engine.IEngineResult)4 IBufferFactoryComponent (io.apiman.gateway.engine.components.IBufferFactoryComponent)4 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)4 IApiRequestExecutor (io.apiman.gateway.engine.IApiRequestExecutor)3 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)3 CacheEntry (io.apiman.gateway.engine.storage.model.CacheEntry)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 IApiConnector (io.apiman.gateway.engine.IApiConnector)2 IConnectorConfig (io.apiman.gateway.engine.IConnectorConfig)2 IEngine (io.apiman.gateway.engine.IEngine)2 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)2