Search in sources :

Example 6 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstSpringmvcBase method cseResponse.

public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) User(org.apache.servicecomb.demo.server.User) HttpHeaders(org.springframework.http.HttpHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 7 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers 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 8 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method testDoSendResponseHeaderNull.

@Test
public void testDoSendResponseHeaderNull(@Mocked Response response) throws Exception {
    Headers headers = new Headers();
    new Expectations() {

        {
            response.getResult();
            result = new Error("stop");
            response.getHeaders();
            result = headers;
        }
    };
    Headers resultHeaders = new Headers();
    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 addHeader(String name, String value) {
            resultHeaders.addHeader(name, value);
        }
    }.getMockInstance();
    initRestInvocation();
    try {
        restInvocation.sendResponse(response);
        Assert.fail("must throw exception");
    } catch (Error e) {
        Assert.assertEquals(null, resultHeaders.getHeaderMap());
    }
}
Also used : Expectations(mockit.Expectations) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) MockUp(mockit.MockUp) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with Headers

use of org.apache.servicecomb.swagger.invocation.response.Headers in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstJaxrs method cseResponse.

// public Response getUserResponse() {
// 
// }
@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@Path("/cseResponse")
@GET
public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE).toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ApiResponse(io.swagger.annotations.ApiResponse) User(org.apache.servicecomb.demo.server.User) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse)

Aggregations

Headers (org.apache.servicecomb.swagger.invocation.response.Headers)9 Response (org.apache.servicecomb.swagger.invocation.Response)5 User (org.apache.servicecomb.demo.server.User)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ApiResponse (io.swagger.annotations.ApiResponse)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Expectations (mockit.Expectations)3 MockUp (mockit.MockUp)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)3 Test (org.junit.Test)3 List (java.util.List)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Buffer (io.vertx.core.buffer.Buffer)1 StatusType (javax.ws.rs.core.Response.StatusType)1 Mock (mockit.Mock)1 HttpServerFilter (org.apache.servicecomb.common.rest.filter.HttpServerFilter)1 Invocation (org.apache.servicecomb.core.Invocation)1