Polish "Add support for configuring default request and response preprocessors"
Closes gh-424
This commit is contained in:
@@ -116,21 +116,25 @@ include::{examples-dir}/com/example/restassured/CustomDefaultSnippets.java[tags=
|
||||
----
|
||||
|
||||
[[configuration-default-preprocessors]]
|
||||
=== Operation preprocessors
|
||||
=== Default operation preprocessors
|
||||
|
||||
You can configure default Request / Response preprocessors during setup using the
|
||||
You can configure default request and response preprocessors during setup using the
|
||||
`RestDocumentationConfigurer` API. For example, to remove the `Foo` headers from all requests
|
||||
and pretty print all responses:
|
||||
|
||||
[source,java,indent=0,role="primary"]
|
||||
.MockMvc
|
||||
----
|
||||
include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-preprocessors]
|
||||
include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
|
||||
----
|
||||
<1> Apply a request preprocessor that will remove the header named `Foo`.
|
||||
<2> Apply a response preprocessor that will pretty print its content.
|
||||
|
||||
[source,java,indent=0,role="secondary"]
|
||||
.REST Assured
|
||||
----
|
||||
include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-preprocessors]
|
||||
include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
|
||||
----
|
||||
<1> Apply a request preprocessor that will remove the header named `Foo`.
|
||||
<2> Apply a response preprocessor that will pretty print its content.
|
||||
|
||||
|
||||
@@ -27,20 +27,25 @@ include::{examples-dir}/com/example/restassured/PerTestPreprocessing.java[tags=p
|
||||
<2> Apply a response preprocessor that will pretty print its content.
|
||||
|
||||
Alternatively, you may want to apply the same preprocessors to every test. You can do
|
||||
so by configuring the preprocessors using the `RestDocumentationConfigurer` API.
|
||||
For example to remove the `Foo` header from all requests and pretty print all responses:
|
||||
so by configuring the preprocessors using the `RestDocumentationConfigurer` API in your
|
||||
`@Before` method. For example to remove the `Foo` header from all requests and pretty print
|
||||
all responses:
|
||||
|
||||
[source,java,indent=0,role="primary"]
|
||||
.MockMvc
|
||||
----
|
||||
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=setup]
|
||||
----
|
||||
<1> Apply a request preprocessor that will remove the header named `Foo`.
|
||||
<2> Apply a response preprocessor that will pretty print its content.
|
||||
|
||||
[source,java,indent=0,role="secondary"]
|
||||
.REST Assured
|
||||
----
|
||||
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=setup]
|
||||
----
|
||||
<1> Apply a request preprocessor that will remove the header named `Foo`.
|
||||
<2> Apply a response preprocessor that will pretty print its content.
|
||||
|
||||
Then, in each test, any configuration specific to that test can be performed. For example:
|
||||
|
||||
|
||||
@@ -35,16 +35,19 @@ public class CustomDefaultOperationPreprocessors {
|
||||
|
||||
private WebApplicationContext context;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
// tag::custom-default-preprocessors[]
|
||||
// tag::custom-default-operation-preprocessors[]
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
|
||||
.withDefaultResponsePreprocessors(prettyPrint()))
|
||||
.apply(documentationConfiguration(this.restDocumentation)
|
||||
.operationPreprocessors()
|
||||
.withRequestDefaults(removeHeaders("Foo")) // <1>
|
||||
.withResponseDefaults(prettyPrint())) // <2>
|
||||
.build();
|
||||
// end::custom-default-preprocessors[]
|
||||
// end::custom-default-operation-preprocessors[]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,19 +45,18 @@ public class EveryTestPreprocessing {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
|
||||
.withDefaultResponsePreprocessors(prettyPrint()))
|
||||
.build();
|
||||
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withRequestDefaults(removeHeaders("Foo")) // <1>
|
||||
.withResponseDefaults(prettyPrint())) // <2>
|
||||
.build();
|
||||
}
|
||||
|
||||
// end::setup[]
|
||||
|
||||
public void use() throws Exception {
|
||||
// tag::use[]
|
||||
this.mockMvc.perform(get("/")) // <1>
|
||||
this.mockMvc.perform(get("/"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("{method-name}",
|
||||
.andDo(document("index",
|
||||
links(linkWithRel("self").description("Canonical self link"))
|
||||
));
|
||||
// end::use[]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,6 @@ package com.example.restassured;
|
||||
|
||||
import io.restassured.builder.RequestSpecBuilder;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
|
||||
@@ -38,12 +37,13 @@ public class CustomDefaultOperationPreprocessors {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
// tag::custom-default-preprocessors[]
|
||||
// tag::custom-default-operation-preprocessors[]
|
||||
this.spec = new RequestSpecBuilder()
|
||||
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
|
||||
.withDefaultResponsePreprocessors(prettyPrint()))
|
||||
.build();
|
||||
// end::custom-default-preprocessors[]
|
||||
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withRequestDefaults(removeHeaders("Foo")) // <1>
|
||||
.withResponseDefaults(prettyPrint())) // <2>
|
||||
.build();
|
||||
// end::custom-default-operation-preprocessors[]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,19 +43,18 @@ public class EveryTestPreprocessing {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.spec = new RequestSpecBuilder()
|
||||
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
|
||||
.withDefaultResponsePreprocessors(prettyPrint()))
|
||||
.build();
|
||||
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
|
||||
.withRequestDefaults(removeHeaders("Foo")) // <1>
|
||||
.withResponseDefaults(prettyPrint())) // <2>
|
||||
.build();
|
||||
}
|
||||
|
||||
// end::setup[]
|
||||
|
||||
public void use() throws Exception {
|
||||
// tag::use[]
|
||||
RestAssured.given(this.spec)
|
||||
.filter(document("{method-name]",
|
||||
links(linkWithRel("self").description("Canonical self link"))))
|
||||
.filter(document("index",
|
||||
links(linkWithRel("self").description("Canonical self link"))))
|
||||
.when().get("/")
|
||||
.then().assertThat().statusCode(is(200));
|
||||
// end::use[]
|
||||
|
||||
Reference in New Issue
Block a user