Search in sources :

Example 6 with RequestMethod

use of com.github.tomakehurst.wiremock.http.RequestMethod in project junit-servers by mjeanroy.

the class WireMockTestUtils method assertUploadRequest.

/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 */
static void assertUploadRequest(String endpoint, HttpMethod method, File file) {
    UrlPattern urlPattern = urlEqualTo(endpoint);
    RequestMethod rqMethod = new RequestMethod(method.name());
    RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern).withAllRequestBodyParts(new MultipartValuePatternBuilder().withName(file.getName()).withBody(new BinaryEqualToPattern(TestUtils.readFile(file))));
    WireMock.verify(1, rq);
}
Also used : MultipartValuePatternBuilder(com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) BinaryEqualToPattern(com.github.tomakehurst.wiremock.matching.BinaryEqualToPattern) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern)

Example 7 with RequestMethod

use of com.github.tomakehurst.wiremock.http.RequestMethod in project junit-servers by mjeanroy.

the class WireMockTestUtils method assertRequest.

/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 */
static void assertRequest(String endpoint, HttpMethod method) {
    UrlPattern urlPattern = urlEqualTo(endpoint);
    RequestMethod rqMethod = new RequestMethod(method.name());
    RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
    WireMock.verify(1, rq);
}
Also used : RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern)

Example 8 with RequestMethod

use of com.github.tomakehurst.wiremock.http.RequestMethod in project spring-cloud-contract by spring-cloud.

the class WireMockRequestStubStrategy method appendMethodAndUrl.

private RequestPatternBuilder appendMethodAndUrl() {
    if (request.getMethod() == null) {
        return null;
    }
    RequestMethod requestMethod = RequestMethod.fromString(Optional.ofNullable(request.getMethod().getClientValue()).map(c -> c.toString()).orElse(null));
    UrlPattern urlPattern = urlPattern();
    return RequestPatternBuilder.newRequestPattern(requestMethod, urlPattern);
}
Also used : RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern)

Example 9 with RequestMethod

use of com.github.tomakehurst.wiremock.http.RequestMethod 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

RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)9 RequestPatternBuilder (com.github.tomakehurst.wiremock.matching.RequestPatternBuilder)7 UrlPattern (com.github.tomakehurst.wiremock.matching.UrlPattern)6 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)1 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 Body (com.github.tomakehurst.wiremock.http.Body)1 ContentTypeHeader (com.github.tomakehurst.wiremock.http.ContentTypeHeader)1 Cookie (com.github.tomakehurst.wiremock.http.Cookie)1 HttpHeader (com.github.tomakehurst.wiremock.http.HttpHeader)1 HttpHeaders (com.github.tomakehurst.wiremock.http.HttpHeaders)1 QueryParameter (com.github.tomakehurst.wiremock.http.QueryParameter)1 Request (com.github.tomakehurst.wiremock.http.Request)1 BinaryEqualToPattern (com.github.tomakehurst.wiremock.matching.BinaryEqualToPattern)1 MultipartValuePatternBuilder (com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder)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 Enumeration (java.util.Enumeration)1