requireNonNull(originalRequest, "An original request is required"); final SimpleRequest.Builder builder = new SimpleRequest.Builder(originalRequest.getMethod(), originalRequest.getDerivedPath()) .withBody(originalRequest.getBody()); if (originalRequest.getHeaders() != null) { originalRequest.getHeaders().forEach(header -> builder.withHeader(header.getName(), header.getValue())); builder.withContentType(originalRequest.getContentType()); .forEach((key, value) -> { if (value instanceof List) { builder.withQueryParam(key, (List) value); } else if (value instanceof String) { builder.withQueryParam(key, (String) value); builder.withQueryParam(key, value); }); return builder.build();
/** * Creates a {@link SimpleRequest.Builder} with the given HTTP {@link Request.Method} and path including * the specification if the query parameters are handled case sensitive or not. * * @param method the HTTP method * @param path the requests path * @param queryParametersCaseSensitive flag if the query parameters are handled case sensitive or not */ public Builder(final Method method, final String path, final boolean queryParametersCaseSensitive) { this.method = requireNonNull(method, "A method is required"); this.path = requireNonNull(path, "A path is required"); headers = multimapBuilder(false /* header are always case insensitive */); queryParams = multimapBuilder(queryParametersCaseSensitive); }
/** * Builds a {@link Request} for the OpenAPI validator out of the * original {@link com.github.tomakehurst.wiremock.http.Request}. * * @param originalRequest the original {@link com.github.tomakehurst.wiremock.http.Request} */ @Nonnull public static Request of(@Nonnull final com.github.tomakehurst.wiremock.http.Request originalRequest) { requireNonNull(originalRequest, "An original request is required"); final URI uri = URI.create(originalRequest.getUrl()); final Map<String, QueryParameter> queryParameterMap = Urls.splitQuery(uri); final SimpleRequest.Builder builder = new SimpleRequest.Builder(originalRequest.getMethod().getName(), uri.getPath()) .withBody(originalRequest.getBodyAsString()); originalRequest.getHeaders().all().forEach(header -> builder.withHeader(header.key(), header.values())); queryParameterMap.forEach((key, value) -> builder.withQueryParam(key, value.values())); return builder.build(); } }
/** * Builds a {@link Request} for the OpenAPI validator out of the * original {@link MockHttpServletRequest}. * * @param originalRequest the original {@link MockHttpServletRequest} */ @Nonnull public static Request of(@Nonnull final MockHttpServletRequest originalRequest) { requireNonNull(originalRequest, "An original request is required"); final SimpleRequest.Builder builder = new SimpleRequest.Builder(originalRequest.getMethod(), originalRequest.getPathInfo()) .withBody(getBody(originalRequest)); list(originalRequest.getHeaderNames()) .forEach(header -> builder.withHeader(header, list(originalRequest.getHeaders(header)))); originalRequest.getParameterMap().forEach((key, value) -> builder.withQueryParam(key, value)); return builder.build(); }
/** * Adds a request header to this builder. If there was already a header with this * name the values will be added. * <p> * Headers are treated case insensitive. * * @param name the header name * @param values the values for this header * @return this builder */ public Builder withHeader(final String name, final String... values) { return withHeader(name, asList(values)); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method TRACE and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder trace(final String path) { return new Builder(Method.TRACE, path); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method OPTIONS and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder options(final String path) { return new Builder(Method.OPTIONS, path); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method HEAD and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder head(final String path) { return new Builder(Method.HEAD, path); }
/** * Adds a query parameter to this request builder. If there was already a query * parameter with this name the values will be added. * <p> * The case sensitivity can be set by this builder's * {@linkplain SimpleRequest.Builder#Builder(String, String, boolean)} constructor. * * @param name the header name * @param values the values for this header * @return this builder */ public Builder withQueryParam(final String name, final String... values) { return withQueryParam(name, asList(values)); }
/** * Sets the authorization header on this builder. * <p> * Equivalent to: <pre>withHeader("Authorization", contentType);</pre> * * @param auth The authorization header to set * * @return this builder */ public Builder withAuthorization(final String auth) { return withHeader(AUTHORIZATION, auth); }
/** * Sets the accept header on this builder. * <p> * Equivalent to: <pre>withHeader("Accept", contentType);</pre> * * @param accept The accept type(s) to set * * @return this builder */ public Builder withAccept(final String... accept) { return withHeader(ACCEPT, accept); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method POST and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder post(final String path) { return new Builder(Method.POST, path); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method GET and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder get(final String path) { return new Builder(Method.GET, path); }
private SimpleRequest buildRequest(HttpValidationResult result) { String relativePath = extractPath(result.getFullUrl()); SimpleRequest.Builder builder = new SimpleRequest.Builder(result.getRequestMethod(), relativePath); if (result.getRequestContent() != null) { builder.withBody(result.getRequestContent()); } if (result.getRequestHeader() != null) { result.getRequestHeader().forEachProperty(builder::withHeader); } extractQueryParams(result.getFullUrl()).forEach(builder::withQueryParam); return builder.build(); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method DELETE and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder delete(final String path) { return new Builder(Method.DELETE, path); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method PUT and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder put(final String path) { return new Builder(Method.PUT, path); }
/** * Creates a {@link SimpleResponse.Builder} with the given HTTP status code. * * @param status the responses HTTP status code */ public Builder(final int status) { this.status = status; headers = SimpleRequest.Builder.multimapBuilder(false /* header are always case insensitive */); }
/** * Sets the content type header on this builder. * <p> * Equivalent to: <pre>withHeader("Content-Type", contentType);</pre> * * @param contentType The content type to set * * @return this builder */ public Builder withContentType(final String contentType) { return withHeader(CONTENT_TYPE, contentType); }
/** * A convenience method for creating a {@link SimpleRequest.Builder} with * HTTP method PATCH and the given path. * * @param path the requests path * @return a prepared {@link SimpleRequest.Builder} */ public static Builder patch(final String path) { return new Builder(Method.PATCH, path); }