Search in sources :

Example 6 with Cookie

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();
        }
    };
}
Also used : Cookie(com.github.tomakehurst.wiremock.http.Cookie) Base64Utils(org.springframework.util.Base64Utils) Enumeration(java.util.Enumeration) Optional(wiremock.com.google.common.base.Optional) HashMap(java.util.HashMap) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ArrayList(java.util.ArrayList) QueryParameter(com.github.tomakehurst.wiremock.http.QueryParameter) Map(java.util.Map) Request(com.github.tomakehurst.wiremock.http.Request) MvcResult(org.springframework.test.web.servlet.MvcResult) Cookie(com.github.tomakehurst.wiremock.http.Cookie) IOUtils(wiremock.org.apache.commons.io.IOUtils) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) HttpHeaders(com.github.tomakehurst.wiremock.http.HttpHeaders) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Body(com.github.tomakehurst.wiremock.http.Body) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ContentTypeHeader(com.github.tomakehurst.wiremock.http.ContentTypeHeader) List(java.util.List) HttpHeader(com.github.tomakehurst.wiremock.http.HttpHeader) ResultHandler(org.springframework.test.web.servlet.ResultHandler) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) HttpHeaders(com.github.tomakehurst.wiremock.http.HttpHeaders) QueryParameter(com.github.tomakehurst.wiremock.http.QueryParameter) HashMap(java.util.HashMap) ContentTypeHeader(com.github.tomakehurst.wiremock.http.ContentTypeHeader) Request(com.github.tomakehurst.wiremock.http.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpHeader(com.github.tomakehurst.wiremock.http.HttpHeader) Body(com.github.tomakehurst.wiremock.http.Body)

Aggregations

Cookie (com.github.tomakehurst.wiremock.http.Cookie)6 HttpHeaders (com.github.tomakehurst.wiremock.http.HttpHeaders)2 LoggedRequest (com.github.tomakehurst.wiremock.verification.LoggedRequest)2 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 Body (com.github.tomakehurst.wiremock.http.Body)1 ContentTypeHeader (com.github.tomakehurst.wiremock.http.ContentTypeHeader)1 HttpHeader (com.github.tomakehurst.wiremock.http.HttpHeader)1 QueryParameter (com.github.tomakehurst.wiremock.http.QueryParameter)1 Request (com.github.tomakehurst.wiremock.http.Request)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1