Add support for documenting request and response cookies
See gh-592
This commit is contained in:
committed by
Andy Wilkinson
parent
15980d7486
commit
f72a9f1067
46
docs/src/test/java/com/example/mockmvc/HttpCookies.java
Normal file
46
docs/src/test/java/com/example/mockmvc/HttpCookies.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2014-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.mockmvc;
|
||||
|
||||
import jakarta.servlet.http.Cookie;
|
||||
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.responseCookies;
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class HttpCookies {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
public void cookies() throws Exception {
|
||||
// tag::cookies[]
|
||||
this.mockMvc.perform(get("/").cookie(new Cookie("JSESSIONID", "ACBCDFD0FF93D5BB"))) // <1>
|
||||
.andExpect(status().isOk()).andDo(document("cookies", requestCookies(// <2>
|
||||
cookieWithName("JSESSIONID").description("Session token")), // <3>
|
||||
responseCookies(// <4>
|
||||
cookieWithName("JSESSIONID").description("Updated session token"),
|
||||
cookieWithName("logged_in")
|
||||
.description("Set to true if the user is currently logged in"))));
|
||||
// end::cookies[]
|
||||
}
|
||||
|
||||
}
|
||||
44
docs/src/test/java/com/example/restassured/HttpCookies.java
Normal file
44
docs/src/test/java/com/example/restassured/HttpCookies.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.restassured;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.responseCookies;
|
||||
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
|
||||
|
||||
public class HttpCookies {
|
||||
|
||||
private RequestSpecification spec;
|
||||
|
||||
public void cookies() throws Exception {
|
||||
// tag::cookies[]
|
||||
RestAssured.given(this.spec).filter(document("cookies", requestCookies(// <1>
|
||||
cookieWithName("JSESSIONID").description("Saved session token")), // <2>
|
||||
responseCookies(// <3>
|
||||
cookieWithName("logged_in").description("If user is logged in"),
|
||||
cookieWithName("JSESSIONID").description("Updated session token"))))
|
||||
.cookie("JSESSIONID", "ACBCDFD0FF93D5BB") // <4>
|
||||
.when().get("/people").then().assertThat().statusCode(is(200));
|
||||
// end::cookies[]
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.webtestclient;
|
||||
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.responseCookies;
|
||||
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
|
||||
|
||||
public class HttpCookies {
|
||||
|
||||
// @formatter:off
|
||||
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
public void cookies() throws Exception {
|
||||
// tag::cookies[]
|
||||
this.webTestClient
|
||||
.get().uri("/people").cookie("JSESSIONID", "ACBCDFD0FF93D5BB=") // <1>
|
||||
.exchange().expectStatus().isOk().expectBody()
|
||||
.consumeWith(document("cookies",
|
||||
requestCookies(// <2>
|
||||
cookieWithName("JSESSIONID").description("Session token")), // <3>
|
||||
responseCookies(// <4>
|
||||
cookieWithName("JSESSIONID")
|
||||
.description("Updated session token"),
|
||||
cookieWithName("logged_in")
|
||||
.description("User is logged in"))));
|
||||
// end::cookies[]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user