From 8374ab6458822c5d97cb4f010ba547d0fcf033c7 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 3 Jun 2021 16:47:08 -0700 Subject: [PATCH 1/5] added cookie in ResponseContext --- .../oas/inflector/models/ResponseContext.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java b/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java index 252935ac..6a639acb 100644 --- a/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java +++ b/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java @@ -19,10 +19,14 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response.Status; +import java.util.ArrayList; +import java.util.List; public class ResponseContext { private MultivaluedMap headers = new MultivaluedHashMap(); + private List cookies = new ArrayList<>(); private MediaType contentType; private int status = Status.OK.getStatusCode(); private Object entity; @@ -32,6 +36,11 @@ public ResponseContext header(String key, String value) { return this; } + public ResponseContext cookie(NewCookie cookie) { + this.cookies.add(cookie); + return this; + } + public ResponseContext contentType(MediaType contentType) { this.contentType = contentType; return this; @@ -65,6 +74,14 @@ public void setHeaders(MultivaluedMap headers) { this.headers = headers; } + public List getCookies() { + return cookies; + } + + public void setCookies(List cookies) { + this.cookies = cookies; + } + public MediaType getContentType() { return contentType; } From 8ea42f107f54f827657b5c62e26843e7b650cd14 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 3 Jun 2021 16:47:51 -0700 Subject: [PATCH 2/5] set cookies --- .../inflector/controllers/OpenAPIOperationController.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java b/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java index 46f5348a..34384aa8 100644 --- a/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java +++ b/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java @@ -56,6 +56,7 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.UriInfo; @@ -440,6 +441,10 @@ public Response apply(ContainerRequestContext ctx) { } } + for (NewCookie cookie : wrapper.getCookies()) { + builder.cookie(cookie); + } + // entity if (wrapper.getEntity() != null) { builder.entity(wrapper.getEntity()); From 2dc885885a813ed7927b83f59cbde23e542fbf51 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 09:23:15 -0700 Subject: [PATCH 3/5] set cookies in example response --- .../java/io/swagger/oas/sample/controllers/TestController.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/io/swagger/oas/sample/controllers/TestController.java b/src/test/java/io/swagger/oas/sample/controllers/TestController.java index 04edc0e4..5021ebf7 100644 --- a/src/test/java/io/swagger/oas/sample/controllers/TestController.java +++ b/src/test/java/io/swagger/oas/sample/controllers/TestController.java @@ -29,6 +29,7 @@ import io.swagger.oas.test.models.User; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import java.io.File; @@ -73,7 +74,9 @@ public ResponseContext inlineRequiredBody(RequestContext request, com.fasterxml. } public ResponseContext updatePet(RequestContext request, com.fasterxml.jackson.databind.JsonNode petType) { + NewCookie cookie = new NewCookie("type", "chocolate"); return new ResponseContext() + .cookie(cookie) .status(200) .entity("OK!"); } From 293f6f5cbd435b5d745f8c1af0098dda222d4ea3 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 09:23:29 -0700 Subject: [PATCH 4/5] add raw response for cookie inspection --- .../io/swagger/oas/test/client/ApiClient.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/test/java/io/swagger/oas/test/client/ApiClient.java b/src/test/java/io/swagger/oas/test/client/ApiClient.java index 703fd506..fcc1f09e 100644 --- a/src/test/java/io/swagger/oas/test/client/ApiClient.java +++ b/src/test/java/io/swagger/oas/test/client/ApiClient.java @@ -458,6 +458,73 @@ public String invokeAPI(String path, String method, Map queryPar } } + public Response getResponse(String path, String method, Map queryParams, Object body, Map headerParams, Entity formParams, String accept, String contentType, String[] authNames) throws ApiException { + updateParamsForAuth(authNames, queryParams, headerParams); + + final ClientConfig clientConfig = new ClientConfig(); + clientConfig.register(MultiPartFeature.class); + if (debugging) { + clientConfig.register(LoggingFeature.class); + } + Client client = ClientBuilder.newClient(clientConfig); + + WebTarget target = client.target(this.basePath).path(path); + + for (String key : queryParams.keySet()) { + String value = queryParams.get(key); + if (value != null) { + target = target.queryParam(key, value); + } + } + + Invocation.Builder invocationBuilder = target.request(contentType); + + for (String key : headerParams.keySet()) { + String value = headerParams.get(key); + if (value != null) { + invocationBuilder = invocationBuilder.header(key, value); + } + } + + for (String key : defaultHeaderMap.keySet()) { + if (!headerParams.containsKey(key)) { + String value = defaultHeaderMap.get(key); + if (value != null) { + invocationBuilder = invocationBuilder.header(key, value); + } + } + } + Response response = null; + + invocationBuilder = invocationBuilder.accept(accept); + + if ("GET".equals(method)) { + response = invocationBuilder.get(); + } else if ("POST".equals(method)) { + if (formParams != null) { + response = invocationBuilder.post(formParams); + } else if (body == null) { + response = invocationBuilder.post(null); + } else { + response = invocationBuilder.post(serialize(body, contentType)); + } + } else if ("PUT".equals(method)) { + if (formParams != null) { + response = invocationBuilder.put(formParams); + } else if (body == null) { + response = invocationBuilder.put(null); + } else { + response = invocationBuilder.put(serialize(body, contentType)); + } + } else if ("DELETE".equals(method)) { + response = invocationBuilder.delete(); + } else { + throw new ApiException(500, "unknown method type " + method); + } + + return response; + } + /** * Update query and header parameters based on authentication settings. * From 5ba3569c7e2cb726c656a7d56e7107d8ca875c9f Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 09:23:35 -0700 Subject: [PATCH 5/5] verify cookie name, value --- .../oas/test/integration/RequestTestIT.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java b/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java index e2c0b0ae..2799fe18 100644 --- a/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java +++ b/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java @@ -27,9 +27,7 @@ import org.testng.annotations.Test; import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.*; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; @@ -46,6 +44,19 @@ public class RequestTestIT { ApiClient client = new ApiClient(); + + @Test + public void verifyUpdatePet() throws Exception { + String path = "/pets"; + Map body = new HashMap<>(); + body.put("pet_type","Cat"); + Response response = client.getResponse(path, "POST", new HashMap(), body, new HashMap(), null, "application/json", null, new String[0]); + assertNotNull(response.getCookies()); + assertEquals(1, response.getCookies().size()); + NewCookie cookie = response.getCookies().get("type"); + assertEquals("type", cookie.getName()); + assertEquals("chocolate", cookie.getValue()); + } @Test public void verifyValidDateTimeInput() throws Exception {