Search in sources :

Example 1 with HttpServerFilterBaseForTest

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

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.set("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 HttpServerFilterBaseForTest() {

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

Example 2 with HttpServerFilterBaseForTest

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

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.set("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 HttpServerFilterBaseForTest() {

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

Example 3 with HttpServerFilterBaseForTest

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

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.set("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 HttpServerFilterBaseForTest() {

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

Aggregations

MultiMap (io.vertx.core.MultiMap)3 Buffer (io.vertx.core.buffer.Buffer)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Expectations (mockit.Expectations)3 MockUp (mockit.MockUp)3 HttpServerFilter (org.apache.servicecomb.common.rest.filter.HttpServerFilter)3 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)3 Invocation (org.apache.servicecomb.core.Invocation)3 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)3 StandardHttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx)3 Test (org.junit.Test)3