Search in sources :

Example 6 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) throws Exception {
    Headers headers = new Headers();
    headers.addHeader("Content-Type", "application/json");
    new Expectations() {

        {
            response.getHeaders();
            result = headers;
            response.getStatusCode();
            result = 123;
            response.getReasonPhrase();
            result = "reason";
            response.getResult();
            result = "ok";
        }
    };
    Buffer buffer = Buffer.buffer();
    responseEx = new MockUp<HttpServletResponseEx>() {

        private Map<String, Object> attributes = new HashMap<>();

        @Mock
        public void setAttribute(String key, Object value) {
            this.attributes.put(key, value);
        }

        @Mock
        public Object getAttribute(String key) {
            return this.attributes.get(key);
        }

        @Mock
        void setBodyBuffer(Buffer bodyBuffer) {
            buffer.appendBuffer(bodyBuffer);
        }
    }.getMockInstance();
    HttpServerFilter filter = new MockUp<HttpServerFilter>() {

        @Mock
        void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
            buffer.appendString("-filter");
        }
    }.getMockInstance();
    initRestInvocation();
    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.loadSortedService(HttpServerFilter.class);
    httpServerFilters.add(filter);
    restInvocation.setHttpServerFilters(httpServerFilters);
    restInvocation.sendResponse(response);
    Assert.assertEquals("\"ok\"-filter", buffer.toString());
}
Also used : Expectations(mockit.Expectations) Buffer(io.vertx.core.buffer.Buffer) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) Invocation(org.apache.servicecomb.core.Invocation) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) MockUp(mockit.MockUp) Mock(mockit.Mock) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method setup.

@Before
public void setup() {
    invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
    initRestInvocation();
    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.getSortedService(HttpServerFilter.class);
    restInvocation.setHttpServerFilters(httpServerFilters);
}
Also used : HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) Invocation(org.apache.servicecomb.core.Invocation) Before(org.junit.Before)

Example 8 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project incubator-servicecomb-java-chassis by apache.

the class AbstractRestInvocation method prepareInvoke.

protected Response prepareInvoke() throws Throwable {
    this.initProduceProcessor();
    this.setContext();
    invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx);
    for (HttpServerFilter filter : httpServerFilters) {
        Response response = filter.afterReceiveRequest(invocation, requestEx);
        if (response != null) {
            return response;
        }
    }
    return null;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter)

Example 9 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method setup.

@Before
public void setup() {
    ConfigUtil.installDynamicConfig();
    scbEngine = SCBBootstrap.createSCBEngineForTest().addProducerMeta("sid1", new TestPathSchema()).run();
    operationMeta = scbEngine.getProducerMicroserviceMeta().operationMetas().get("test.sid1.dynamicId");
    restOperation = RestMetaUtils.getRestOperationMeta(operationMeta);
    new MockUp<System>() {

        @Mock
        long nanoTime() {
            return nanoTime;
        }
    };
    if (responseEx == null) {
        responseEx = new StandardHttpServletResponseEx(servletResponse);
    }
    invocation = new Invocation(endpoint, operationMeta, operationMeta.buildBaseConsumerRuntimeType(), arguments);
    initRestInvocation();
    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.getSortedService(HttpServerFilter.class);
    restInvocation.setHttpServerFilters(httpServerFilters);
}
Also used : HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) Invocation(org.apache.servicecomb.core.Invocation) TestPathSchema(org.apache.servicecomb.common.rest.locator.TestPathSchema) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) MockUp(mockit.MockUp) Before(org.junit.Before)

Aggregations

HttpServerFilter (org.apache.servicecomb.common.rest.filter.HttpServerFilter)9 MockUp (mockit.MockUp)5 Expectations (mockit.Expectations)4 Invocation (org.apache.servicecomb.core.Invocation)4 Test (org.junit.Test)4 Buffer (io.vertx.core.buffer.Buffer)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)2 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)2 StandardHttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx)2 StandardHttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 Before (org.junit.Before)2 MultiMap (io.vertx.core.MultiMap)1 List (java.util.List)1 Mock (mockit.Mock)1 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)1 TestPathSchema (org.apache.servicecomb.common.rest.locator.TestPathSchema)1 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)1