io.restassured.specification.FilterableResponseSpecification Java Examples

The following examples show how to use io.restassured.specification.FilterableResponseSpecification. 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: YamlToJsonFilter.java    From microprofile-open-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
    try {
        Response response = ctx.next(requestSpec, responseSpec);

        ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
        Object obj = yamlReader.readValue(response.getBody().asString(), Object.class);

        ObjectMapper jsonWriter = new ObjectMapper();
        String json = jsonWriter.writeValueAsString(obj);

        ResponseBuilder builder = new ResponseBuilder();
        builder.clone(response);
        builder.setBody(json);
        builder.setContentType(ContentType.JSON);

        return builder.build();
    }
    catch (Exception e) {
        throw new IllegalStateException("Failed to convert the request: " + ExceptionUtils.getMessage(e), e);
    }
}
 
Example #2
Source File: RequestVerifierFilter.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@Override
public Response filter(FilterableRequestSpecification requestSpec,
		FilterableResponseSpecification responseSpec, FilterContext context) {
	Map<String, Object> configuration = getConfiguration(requestSpec, context);
	configuration.put("contract.jsonPaths", this.jsonPaths.keySet());
	Response response = context.next(requestSpec, responseSpec);
	if (requestSpec.getBody() != null && !this.jsonPaths.isEmpty()) {
		String actual = new String((byte[]) requestSpec.getBody());
		for (JsonPath jsonPath : this.jsonPaths.values()) {
			new JsonPathValue(jsonPath, actual).assertHasValue(Object.class,
					"an object");
		}
	}
	if (this.builder != null) {
		this.builder.willReturn(getResponseDefinition(response));
		StubMapping stubMapping = this.builder.build();
		MatchResult match = stubMapping.getRequest()
				.match(new WireMockRestAssuredRequestAdapter(requestSpec));
		assertThat(match.isExactMatch()).as("wiremock did not match request")
				.isTrue();
		configuration.put("contract.stubMapping", stubMapping);
	}
	return response;
}
 
Example #3
Source File: RestAssuredEdgeGridFilterTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test
public void replacesProvidedHostHeader() throws URISyntaxException, IOException,
        RequestSigningException {


    RestAssured.given()
            .relaxedHTTPSValidation()
            .header("Host", "ignored-hostname.com")
            .filter(new RestAssuredEdgeGridFilter(credential))
            .filter(new Filter() {
                @Override
                public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                    MatcherAssert.assertThat(requestSpec.getHeaders().getList("Host").size(),
                            CoreMatchers.equalTo(1));
                    MatcherAssert.assertThat(requestSpec.getHeaders().get("Host").getValue(),
                            CoreMatchers.equalTo(credential.getHost()));

                    return ctx.next(requestSpec, responseSpec);
                }
            })
            .get();


}
 
Example #4
Source File: RestAssuredEdgeGridFilterIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test
public void replacesProvidedHostHeader() throws URISyntaxException, IOException,
        RequestSigningException {


    RestAssured.given()
            .relaxedHTTPSValidation()
            .header("Host", "ignored-hostname.com")
            .filter(new RestAssuredEdgeGridFilter(credential))
            .filter(new Filter() {
                @Override
                public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                    MatcherAssert.assertThat(requestSpec.getHeaders().getList("Host").size(),
                            CoreMatchers.equalTo(1));
                    MatcherAssert.assertThat(requestSpec.getHeaders().get("Host").getValue(),
                            CoreMatchers.equalTo(credential.getHost()));

                    return ctx.next(requestSpec, responseSpec);
                }
            })
            .get();


}
 
Example #5
Source File: AllureRestAssured.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public Response filter(final FilterableRequestSpecification requestSpec,
                       final FilterableResponseSpecification responseSpec,
                       final FilterContext filterContext) {
    final Prettifier prettifier = new Prettifier();


    final HttpRequestAttachment.Builder requestAttachmentBuilder = create("Request", requestSpec.getURI())
            .setMethod(requestSpec.getMethod())
            .setHeaders(toMapConverter(requestSpec.getHeaders()))
            .setCookies(toMapConverter(requestSpec.getCookies()));

    if (Objects.nonNull(requestSpec.getBody())) {
        requestAttachmentBuilder.setBody(prettifier.getPrettifiedBodyIfPossible(requestSpec));
    }

    final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();

    new DefaultAttachmentProcessor().addAttachment(
            requestAttachment,
            new FreemarkerAttachmentRenderer(requestTemplatePath)
    );

    final Response response = filterContext.next(requestSpec, responseSpec);
    final HttpResponseAttachment responseAttachment = create(response.getStatusLine())
            .setResponseCode(response.getStatusCode())
            .setHeaders(toMapConverter(response.getHeaders()))
            .setBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))
            .build();

    new DefaultAttachmentProcessor().addAttachment(
            responseAttachment,
            new FreemarkerAttachmentRenderer(responseTemplatePath)
    );

    return response;
}
 
Example #6
Source File: AuthFilter.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Response filter( FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,
    FilterContext ctx )
{
    if ( requestSpec.getAuthenticationScheme() instanceof NoAuthScheme )
    {
        if ( hasSessionCookie( requestSpec ) )
        {
            requestSpec.removeCookies();
        }

        lastLoggedInUser = "";
        lastLoggedInUserPsw = "";
    }

    if ( requestSpec.getAuthenticationScheme() instanceof PreemptiveBasicAuthScheme && (
        ((PreemptiveBasicAuthScheme) requestSpec.getAuthenticationScheme()).getUserName() != lastLoggedInUser ||
        ((PreemptiveBasicAuthScheme) requestSpec.getAuthenticationScheme()).getPassword() != lastLoggedInUserPsw ) )
    {
        if ( hasSessionCookie( requestSpec ) )
        {
            requestSpec.removeCookies();
        }

        lastLoggedInUser = ((PreemptiveBasicAuthScheme) requestSpec.getAuthenticationScheme()).getUserName();
        lastLoggedInUserPsw = ((PreemptiveBasicAuthScheme) requestSpec.getAuthenticationScheme()).getPassword();
    }

    final Response response = ctx.next( requestSpec, responseSpec );
    return response;
}
 
Example #7
Source File: LoadRunnerFilter.java    From cukes with Apache License 2.0 5 votes vote down vote up
@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,
                       FilterContext ctx) {
    WebCustomRequest request = mapper.map(requestSpec);
    trx.addFunction(request);
    boolean blockRequests = globalWorldFacade.getBoolean(CukesOptions.LOADRUNNER_FILTER_BLOCKS_REQUESTS);
    if (blockRequests) {
        Response response = Mockito.mock(Response.class);
        Mockito.when(response.then()).thenReturn(Mockito.mock(ValidatableResponse.class));
        return response;
    }
    return ctx.next(requestSpec, responseSpec);
}
 
Example #8
Source File: RamlValidationFilter.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,
                       FilterContext filterContext) {
    final Response response = filterContext.next(requestSpec, responseSpec);
    final RamlReport report = ramlChecker.check(new RestAssuredRamlRequest(requestSpec), new RestAssuredRamlResponse(response));
    reportStore.storeReport(report);
    return response;

}
 
Example #9
Source File: RestAssuredIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
public void replaceProvidedHostHeaderOnlyInApacheClient() throws URISyntaxException, IOException,
        RequestSigningException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .willReturn(aResponse()
                    .withStatus(200)));

    getBaseRequestSpecification()
            .header("Host", "ignored-hostname.com")
            .filter(new Filter() {
                @Override
                public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                    MatcherAssert.assertThat(requestSpec.getHeaders().getList("Host").size(),
                            CoreMatchers.equalTo(1));
                    MatcherAssert.assertThat(requestSpec.getHeaders().get("Host").getValue(),
                            CoreMatchers.equalTo("ignored-hostname.com"));
                    return ctx.next(requestSpec, responseSpec);
                }
            })
            .get("/billing-usage/v1/reportSources");


    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Host"),
            CoreMatchers.equalTo(SERVICE_MOCK));

}
 
Example #10
Source File: RestAssuredEdgeGridFilter.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
    try {
        binding.sign(requestSpec, requestSpec);
    } catch (RequestSigningException e) {
        throw new RuntimeException(e);
    }
    return ctx.next(requestSpec, responseSpec);
}