use of com.github.tomakehurst.wiremock.http.Cookie in project spring-cloud-contract by spring-cloud.
the class ContractResultHandler method getWireMockRequest.
@Override
protected Request getWireMockRequest(MvcResult result) {
return new Request() {
@Override
public String getUrl() {
return result.getRequest().getRequestURI();
}
@Override
public String getAbsoluteUrl() {
return result.getRequest().getRequestURI();
}
@Override
public RequestMethod getMethod() {
return RequestMethod.fromString(result.getRequest().getMethod());
}
@Override
public String getScheme() {
return result.getRequest().getScheme();
}
@Override
public String getHost() {
return result.getRequest().getRemoteHost();
}
@Override
public int getPort() {
return result.getRequest().getServerPort();
}
@Override
public String getClientIp() {
return "";
}
@Override
public String getHeader(String key) {
return result.getRequest().getHeader(key);
}
@Override
public HttpHeader header(String key) {
return new HttpHeader(key, getHeader(key));
}
@Override
public ContentTypeHeader contentTypeHeader() {
return new ContentTypeHeader(result.getRequest().getContentType());
}
@Override
public HttpHeaders getHeaders() {
List<HttpHeader> headers = new ArrayList<>();
Enumeration<String> headerNames = result.getRequest().getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = headerNames.nextElement();
String value = getHeader(key);
headers.add(new HttpHeader(key, value));
}
return new HttpHeaders(headers);
}
@Override
public boolean containsHeader(String key) {
Enumeration<String> headerNames = result.getRequest().getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
if (name.equals(key)) {
return true;
}
}
return false;
}
@Override
public Set<String> getAllHeaderKeys() {
return getHeaders().keys();
}
@Override
public Map<String, Cookie> getCookies() {
Map<String, Cookie> nameToCookie = new HashMap<>();
if (result.getRequest().getCookies() == null) {
return nameToCookie;
}
for (jakarta.servlet.http.Cookie cookie : result.getRequest().getCookies()) {
nameToCookie.put(cookie.getName(), new Cookie(cookie.getValue()));
}
return nameToCookie;
}
@Override
public QueryParameter queryParameter(String key) {
return new QueryParameter(key, Collections.singletonList(result.getRequest().getParameter(key)));
}
@Override
public byte[] getBody() {
return result.getRequest().getContentAsByteArray();
}
@Override
public String getBodyAsString() {
try {
return result.getRequest().getContentAsString();
} catch (Exception ex) {
return new String(result.getRequest().getContentAsByteArray());
}
}
@Override
public String getBodyAsBase64() {
return Base64Utils.encodeToString(result.getRequest().getContentAsByteArray());
}
@Override
public boolean isMultipart() {
return StringUtils.hasText(result.getRequest().getHeader("multipart/form-data"));
}
@Override
public Collection<Part> getParts() {
try {
return result.getRequest().getParts().stream().map(part -> new Part() {
@Override
public String getName() {
return part.getName();
}
@Override
public HttpHeader getHeader(String name) {
String header = part.getHeader(name);
return new HttpHeader(name, header);
}
@Override
public HttpHeaders getHeaders() {
List<HttpHeader> headers = new ArrayList<>();
for (String headerName : part.getHeaderNames()) {
headers.add(new HttpHeader(headerName, getHeader(headerName).values()));
}
return new HttpHeaders(headers);
}
@Override
public Body getBody() {
try {
return new Body(IOUtils.toByteArray(part.getInputStream()));
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}).collect(Collectors.toList());
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@Override
public Part getPart(String name) {
return getParts().stream().filter(part -> part.getName().equals(name)).findFirst().orElse(null);
}
@Override
public boolean isBrowserProxyRequest() {
return false;
}
@Override
public Optional<Request> getOriginalRequest() {
return Optional.absent();
}
};
}
Aggregations