com.github.tomakehurst.wiremock.http.HttpHeaders Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.http.HttpHeaders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WireMockTestUtils.java    From junit-servers with MIT License 6 votes vote down vote up
private static void stubRequest(String method, String endpoint, int status, Collection<Pair> headers, String body) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	MappingBuilder request = request(method, urlPattern);

	ResponseDefinitionBuilder response = aResponse().withStatus(status);

	HttpHeaders httpHeaders = new HttpHeaders();

	for (Pair header : headers) {
		String name = header.getO1();
		List<String> values = header.getO2();
		HttpHeader h = new HttpHeader(name, values);
		httpHeaders = httpHeaders.plus(h);
	}

	response.withHeaders(httpHeaders);

	if (body != null) {
		response.withBody(body);
	}

	stubFor(request.willReturn(response));
}
 
Example #2
Source File: WiremockResponseConverterTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void convertsResponseWithBody() {
    ResponseDefinition response = new ResponseDefinition(HTTP_OK, "{ \"count\" : 0, \"requestJournalDisabled\" : false}");
    response.setHeaders(new HttpHeaders(
            httpHeader("Transfer-Encoding", "chunked"),
            httpHeader("Content-Type", "application/json")));

    HttpResponse styxResponse = toStyxResponse(new BasicResponseRenderer().render(response));

    assertThat(styxResponse.status(), is(OK));
    Map<String, String> actual = headersAsMap(styxResponse);

    assertThat(actual, is(ImmutableMap.of(
            "Transfer-Encoding", "chunked",
            "Content-Type", "application/json")));
    assertThat(styxResponse.bodyAs(UTF_8), is("{ \"count\" : 0, \"requestJournalDisabled\" : false}"));
    assertThat(headerCount(styxResponse.headers()), is(2));
}
 
Example #3
Source File: WireMockSnippet.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private MappingBuilder requestHeaders(MappingBuilder request, Operation operation) {
	org.springframework.http.HttpHeaders headers = operation.getRequest()
			.getHeaders();
	// TODO: whitelist headers
	for (String name : headers.keySet()) {
		if (!this.headerBlackList.contains(name.toLowerCase())) {
			if ("content-type".equalsIgnoreCase(name) && this.contentType != null) {
				continue;
			}
			request = request.withHeader(name, equalTo(headers.getFirst(name)));
		}
	}
	if (this.contentType != null) {
		request = request.withHeader("Content-Type",
				matching(Pattern.quote(this.contentType.toString()) + ".*"));
	}
	return request;
}
 
Example #4
Source File: WireMockBase.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
    // if gzipped, ungzip they body and discard the Content-Encoding header
    if (response.getHeaders().getHeader("Content-Encoding").containsValue("gzip")) {
        Iterable<HttpHeader> headers = Iterables.filter(
            response.getHeaders().all(),
            (HttpHeader header) -> header != null && !header.keyEquals("Content-Encoding") && !header.containsValue("gzip")
        );
        return Response.Builder.like(response)
            .but()
            .body(Gzip.unGzip(response.getBody()))
            .headers(new HttpHeaders(headers))
            .build();
    }
    return response;
}
 
Example #5
Source File: CustomVelocityResponseTransformer.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
private void addHeadersToContext(final HttpHeaders headers) {
    context.put("headers", headers.all().stream()
        .collect(Collectors.toMap(
            MultiValue::key,
            h -> h.values().size() != 1 ? h.values() : h.values().get(0))
        ));
}
 
Example #6
Source File: RequestVerifierFilter.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Override
public HttpHeaders getHeaders() {
	List<HttpHeader> headers = new ArrayList<>();
	for (Header header : request.getHeaders()) {
		String value = header.getValue();
		if ("accept".equals(header.getName().toLowerCase()) && "*/*".equals(value)) {
			continue;
		}
		headers.add(new HttpHeader(header.getName(), header.getValue()));
	}
	return new HttpHeaders(headers);
}
 
Example #7
Source File: WebhookDefinition.java    From wiremock-webhooks-extension with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public WebhookDefinition(@JsonProperty("method") RequestMethod method,
                         @JsonProperty("url") URI url,
                         @JsonProperty("headers") HttpHeaders headers,
                         @JsonProperty("body") String body,
                         @JsonProperty("base64Body") String base64Body) {
    this.method = method;
    this.url = url;
    this.headers = newArrayList(headers.all());
    this.body = Body.fromOneOf(null, body, null, base64Body);
}
 
Example #8
Source File: WireMockSnippet.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private HttpHeaders responseHeaders(Operation operation) {
	org.springframework.http.HttpHeaders headers = operation.getResponse()
			.getHeaders();
	HttpHeaders result = new HttpHeaders();
	for (String name : headers.keySet()) {
		if (!this.headerBlackList.contains(name.toLowerCase())) {
			result = result.plus(new HttpHeader(name, headers.get(name)));
		}
	}
	return result;
}
 
Example #9
Source File: SdkHttpResponseTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void stubWithHeaders(Map<String, List<String>> headers) {

        HttpHeaders httpHeaders = new HttpHeaders(headers.entrySet().stream().map(entry -> new HttpHeader(entry.getKey(),
                                                                                                          entry.getValue()))
                                                         .collect(Collectors.toList()));
        stubFor(post(anyUrl()).willReturn(aResponse()
                                              .withStatus(200)
                                              .withStatusMessage(STATUS_TEXT)
                                              .withHeaders(httpHeaders)
                                              .withBody(JSON_BODY)));
    }
 
Example #10
Source File: WiremockStyxRequestAdapter.java    From styx with Apache License 2.0 5 votes vote down vote up
@Override
public HttpHeaders getHeaders() {
    List<HttpHeader> list = stream(styxRequest.headers().spliterator(), false)
            .map(styxHeader -> httpHeader(styxHeader.name(), styxHeader.value()))
            .collect(toList());

    return new HttpHeaders(list);
}
 
Example #11
Source File: AbstractHttpClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public <S> void forEach(String headerName, LoggedRequest loggedRequest, S state, HeaderConsumer<String, S> consumer) {
    HttpHeaders headers = loggedRequest.getHeaders();
    if (headers != null) {
        HttpHeader header = headers.getHeader(headerName);
        if (header != null) {
            List<String> values = header.values();
            for (int i = 0, size = values.size(); i < size; i++) {
                consumer.accept(values.get(i), state);
            }
        }
    }
}
 
Example #12
Source File: AbstractInstancesProxyControllerIntegrationTest.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
    return Response.Builder.like(response)
                           .headers(HttpHeaders.copyOf(response.getHeaders())
                                               .plus(new HttpHeader("Connection", "Close")))
                           .build();
}
 
Example #13
Source File: HeadersAssertion.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void assertDoesNotContainHeaders(HttpHeaders actual) {
    doesNotContain.forEach(headerName -> {
        assertFalse(String.format("Header '%s' was expected to be absent", headerName),
                    actual.getHeader(headerName).isPresent());
    });
}
 
Example #14
Source File: HeadersAssertion.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void assertHeadersContains(HttpHeaders actual) {
    contains.entrySet().forEach(e -> {
        assertEquals(e.getValue(), actual.getHeader(e.getKey()).firstValue());
    });
}
 
Example #15
Source File: WiremockResponseConverterTest.java    From styx with Apache License 2.0 4 votes vote down vote up
private int headerCount(com.hotels.styx.api.HttpHeaders headers) {
    AtomicInteger count = new AtomicInteger();
    headers.forEach(header -> count.incrementAndGet());
    return count.get();
}
 
Example #16
Source File: WebhookDefinition.java    From wiremock-webhooks-extension with Apache License 2.0 4 votes vote down vote up
public HttpHeaders getHeaders() {
    return new HttpHeaders(headers);
}
 
Example #17
Source File: ConnectionCloseExtension.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
	return Response.Builder.like(response)
			.headers(HttpHeaders.copyOf(response.getHeaders()).plus(new HttpHeader("Connection", "Close"))).build();
}