Skip to content

Commit

Permalink
Merge pull request #392 from fehguy/issue-391
Browse files Browse the repository at this point in the history
Added cookie support
  • Loading branch information
gracekarina authored Jun 4, 2021
2 parents 7698918 + 5ba3569 commit 35b8d38
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/swagger/oas/inflector/models/ResponseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> headers = new MultivaluedHashMap<String, String>();
private List<NewCookie> cookies = new ArrayList<>();
private MediaType contentType;
private int status = Status.OK.getStatusCode();
private Object entity;
Expand All @@ -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;
Expand Down Expand Up @@ -65,6 +74,14 @@ public void setHeaders(MultivaluedMap<String, String> headers) {
this.headers = headers;
}

public List<NewCookie> getCookies() {
return cookies;
}

public void setCookies(List<NewCookie> cookies) {
this.cookies = cookies;
}

public MediaType getContentType() {
return contentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/io/swagger/oas/test/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,73 @@ public String invokeAPI(String path, String method, Map<String, String> queryPar
}
}

public Response getResponse(String path, String method, Map<String, String> queryParams, Object body, Map<String, String> 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.
*
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/io/swagger/oas/test/integration/RequestTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +44,19 @@

public class RequestTestIT {
ApiClient client = new ApiClient();

@Test
public void verifyUpdatePet() throws Exception {
String path = "/pets";
Map<String,String> body = new HashMap<>();
body.put("pet_type","Cat");
Response response = client.getResponse(path, "POST", new HashMap<String, String>(), body, new HashMap<String, String>(), 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 {
Expand Down

0 comments on commit 35b8d38

Please sign in to comment.