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(")")));
}
Aggregations