Polish "Add support for documenting request and response cookies"
See gh-592
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.restdocs.cookies;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Rule;
|
||||
@@ -32,8 +31,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
* Tests for failures when rendering {@link RequestCookiesSnippet} due to missing or
|
||||
* undocumented cookies.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Clyde Stubbs
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class RequestCookiesSnippetFailureTests {
|
||||
|
||||
@@ -41,20 +40,20 @@ public class RequestCookiesSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
|
||||
|
||||
@Test
|
||||
public void missingRequestCookie() throws IOException {
|
||||
public void missingRequestCookie() {
|
||||
assertThatExceptionOfType(SnippetException.class)
|
||||
.isThrownBy(() -> new RequestCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("Accept").description("one")))
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("JSESSIONID").description("one")))
|
||||
.document(this.operationBuilder.request("http://localhost").build()))
|
||||
.withMessage("Cookies with the following names were not found" + " in the request: [Accept]");
|
||||
.withMessage("Cookies with the following names were not found in the request: [JSESSIONID]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void undocumentedRequestCookieAndMissingRequestHeader() throws IOException {
|
||||
assertThatExceptionOfType(SnippetException.class).isThrownBy(() -> new RequestCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("Accept").description("one")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("X-Test", "test").build()))
|
||||
.withMessageEndingWith("Cookies with the following names were not found" + " in the request: [Accept]");
|
||||
public void undocumentedRequestCookie() {
|
||||
assertThatExceptionOfType(SnippetException.class)
|
||||
.isThrownBy(() -> new RequestCookiesSnippet(Collections.emptyList()).document(this.operationBuilder
|
||||
.request("http://localhost").cookie("JSESSIONID", "1234abcd5678efgh").build()))
|
||||
.withMessageEndingWith("Cookies with the following names were not documented: [JSESSIONID]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -32,15 +32,15 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
import static org.springframework.restdocs.snippet.Attributes.attributes;
|
||||
import static org.springframework.restdocs.snippet.Attributes.key;
|
||||
|
||||
/**
|
||||
* Tests for {@link RequestCookiesSnippet}.
|
||||
*
|
||||
* @author Andreas Evers
|
||||
* @author Andy Wilkinson
|
||||
* @author Clyde Stubbs
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class RequestCookiesSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@@ -50,27 +50,42 @@ public class RequestCookiesSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void requestWithCookies() throws IOException {
|
||||
new RequestCookiesSnippet(Arrays.asList(CookieDocumentation.cookieWithName("Session").description("one"),
|
||||
CookieDocumentation.cookieWithName("User").description("two"),
|
||||
CookieDocumentation.cookieWithName("Timeout").description("three"),
|
||||
CookieDocumentation.cookieWithName("Preference").description("four"),
|
||||
CookieDocumentation.cookieWithName("Connection").description("five")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("Session", "test")
|
||||
.cookie("User", "nobody").cookie("Timeout", "3600").cookie("Preference", "inverse")
|
||||
.cookie("Connection", "secure").build());
|
||||
new RequestCookiesSnippet(
|
||||
Arrays.asList(cookieWithName("tz").description("one"), cookieWithName("logged_in").description("two")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").build());
|
||||
assertThat(this.generatedSnippets.requestCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`Session`", "one").row("`User`", "two")
|
||||
.row("`Timeout`", "three").row("`Preference`", "four").row("`Connection`", "five"));
|
||||
.is(tableWithHeader("Name", "Description").row("`tz`", "one").row("`logged_in`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void undocumentedRequestCookie() throws IOException {
|
||||
public void ignoredRequestCookie() throws IOException {
|
||||
new RequestCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("X-Test").description("one")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("X-Test", "test")
|
||||
.cookie("Second", "*/*").build());
|
||||
Arrays.asList(cookieWithName("tz").ignored(), cookieWithName("logged_in").description("two")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").build());
|
||||
assertThat(this.generatedSnippets.requestCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
.is(tableWithHeader("Name", "Description").row("`logged_in`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allUndocumentedCookiesCanBeIgnored() throws IOException {
|
||||
new RequestCookiesSnippet(
|
||||
Arrays.asList(cookieWithName("tz").description("one"), cookieWithName("logged_in").description("two")),
|
||||
true).document(
|
||||
this.operationBuilder.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").cookie("user_session", "abcd1234efgh5678").build());
|
||||
assertThat(this.generatedSnippets.requestCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`tz`", "one").row("`logged_in`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingOptionalCookie() throws IOException {
|
||||
new RequestCookiesSnippet(Arrays.asList(cookieWithName("tz").description("one").optional(),
|
||||
cookieWithName("logged_in").description("two"))).document(
|
||||
this.operationBuilder.request("http://localhost").cookie("logged_in", "true").build());
|
||||
assertThat(this.generatedSnippets.requestCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`tz`", "one").row("`logged_in`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,12 +93,11 @@ public class RequestCookiesSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("request-cookies"))
|
||||
.willReturn(snippetResource("request-cookies-with-title"));
|
||||
new RequestCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("X-Test").description("one")),
|
||||
new RequestCookiesSnippet(Collections.singletonList(cookieWithName("tz").description("one")),
|
||||
attributes(key("title").value("Custom title")))
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver))
|
||||
.request("http://localhost").cookie("X-Test", "test").build());
|
||||
.request("http://localhost").cookie("tz", "Europe%2FLondon").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).contains("Custom title");
|
||||
}
|
||||
|
||||
@@ -92,44 +106,45 @@ public class RequestCookiesSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("request-cookies"))
|
||||
.willReturn(snippetResource("request-cookies-with-extra-column"));
|
||||
new RequestCookiesSnippet(Arrays.asList(
|
||||
CookieDocumentation.cookieWithName("X-Test").description("one").attributes(key("foo").value("alpha")),
|
||||
CookieDocumentation.cookieWithName("Accept-Encoding").description("two")
|
||||
.attributes(key("foo").value("bravo")),
|
||||
CookieDocumentation.cookieWithName("Accept").description("three")
|
||||
.attributes(key("foo").value("charlie"))))
|
||||
new RequestCookiesSnippet(
|
||||
Arrays.asList(cookieWithName("tz").description("one").attributes(key("foo").value("alpha")),
|
||||
cookieWithName("logged_in").description("two").attributes(key("foo").value("bravo"))))
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver))
|
||||
.request("http://localhost").cookie("X-Test", "test")
|
||||
.cookie("Accept-Encoding", "gzip, deflate").cookie("Accept", "*/*").build());
|
||||
.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).is(//
|
||||
tableWithHeader("Name", "Description", "Foo").row("X-Test", "one", "alpha")
|
||||
.row("Accept-Encoding", "two", "bravo").row("Accept", "three", "charlie"));
|
||||
tableWithHeader("Name", "Description", "Foo").row("tz", "one", "alpha").row("logged_in", "two",
|
||||
"bravo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
CookieDocumentation
|
||||
.requestCookies(CookieDocumentation.cookieWithName("X-Test").description("one"),
|
||||
CookieDocumentation.cookieWithName("Accept").description("two"),
|
||||
CookieDocumentation.cookieWithName("Accept-Encoding").description("three"),
|
||||
CookieDocumentation.cookieWithName("Accept-Language").description("four"))
|
||||
.and(CookieDocumentation.cookieWithName("Cache-Control").description("five"),
|
||||
CookieDocumentation.cookieWithName("Connection").description("six"))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("X-Test", "test")
|
||||
.cookie("Accept", "*/*").cookie("Accept-Encoding", "gzip, deflate")
|
||||
.cookie("Accept-Language", "en-US,en;q=0.5").cookie("Cache-Control", "max-age=0")
|
||||
.cookie("Connection", "keep-alive").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row("`X-Test`", "one").row("`Accept`", "two").row("`Accept-Encoding`", "three")
|
||||
.row("`Accept-Language`", "four").row("`Cache-Control`", "five").row("`Connection`", "six"));
|
||||
new RequestCookiesSnippet(
|
||||
Arrays.asList(cookieWithName("tz").description("one"), cookieWithName("logged_in").description("two")))
|
||||
.and(cookieWithName("user_session").description("three"))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").cookie("user_session", "abcd1234efgh5678").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).is(tableWithHeader("Name", "Description").row("`tz`", "one")
|
||||
.row("`logged_in`", "two").row("`user_session`", "three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptorsWithRelaxedRequestCookies() throws IOException {
|
||||
new RequestCookiesSnippet(
|
||||
Arrays.asList(cookieWithName("tz").description("one"), cookieWithName("logged_in").description("two")),
|
||||
true).and(cookieWithName("user_session").description("three"))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("tz", "Europe%2FLondon")
|
||||
.cookie("logged_in", "true").cookie("user_session", "abcd1234efgh5678")
|
||||
.cookie("color_theme", "light").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).is(tableWithHeader("Name", "Description").row("`tz`", "one")
|
||||
.row("`logged_in`", "two").row("`user_session`", "three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
|
||||
new RequestCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("Foo|Bar").description("one|two")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("Foo|Bar", "baz").build());
|
||||
new RequestCookiesSnippet(Collections.singletonList(cookieWithName("Foo|Bar").description("one|two")))
|
||||
.document(this.operationBuilder.request("http://localhost").cookie("Foo|Bar", "baz").build());
|
||||
assertThat(this.generatedSnippets.requestCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row(escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.restdocs.cookies;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Rule;
|
||||
@@ -27,13 +26,14 @@ import org.springframework.restdocs.templates.TemplateFormats;
|
||||
import org.springframework.restdocs.testfixtures.OperationBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
|
||||
/**
|
||||
* Tests for failures when rendering {@link ResponseCookiesSnippet} due to missing or
|
||||
* undocumented cookies.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Clyde Stubbs
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ResponseCookiesSnippetFailureTests {
|
||||
|
||||
@@ -41,22 +41,20 @@ public class ResponseCookiesSnippetFailureTests {
|
||||
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
|
||||
|
||||
@Test
|
||||
public void missingResponseCookie() throws IOException {
|
||||
public void missingResponseCookie() {
|
||||
assertThatExceptionOfType(SnippetException.class)
|
||||
.isThrownBy(() -> new ResponseCookiesSnippet(Collections
|
||||
.singletonList(CookieDocumentation.cookieWithName("Content-Type").description("one")))
|
||||
.isThrownBy(() -> new ResponseCookiesSnippet(
|
||||
Collections.singletonList(cookieWithName("JSESSIONID").description("one")))
|
||||
.document(this.operationBuilder.response().build()))
|
||||
.withMessage("Cookies with the following names were not found" + " in the response: [Content-Type]");
|
||||
.withMessage("Cookies with the following names were not found in the response: [JSESSIONID]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void undocumentedResponseCookieAndMissingResponseHeader() throws IOException {
|
||||
public void undocumentedResponseCookie() {
|
||||
assertThatExceptionOfType(SnippetException.class)
|
||||
.isThrownBy(() -> new ResponseCookiesSnippet(Collections
|
||||
.singletonList(CookieDocumentation.cookieWithName("Content-Type").description("one")))
|
||||
.document(this.operationBuilder.response().cookie("X-Test", "test").build()))
|
||||
.withMessageEndingWith(
|
||||
"Cookies with the following names were not found" + " in the response: [Content-Type]");
|
||||
.isThrownBy(() -> new ResponseCookiesSnippet(Collections.emptyList())
|
||||
.document(this.operationBuilder.response().cookie("JSESSIONID", "1234abcd5678efgh").build()))
|
||||
.withMessageEndingWith("Cookies with the following names were not documented: [JSESSIONID]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2022 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.
|
||||
@@ -32,15 +32,15 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
|
||||
import static org.springframework.restdocs.snippet.Attributes.attributes;
|
||||
import static org.springframework.restdocs.snippet.Attributes.key;
|
||||
|
||||
/**
|
||||
* Tests for {@link ResponseCookiesSnippet}.
|
||||
*
|
||||
* @author Andreas Evers
|
||||
* @author Andy Wilkinson
|
||||
* @author Clyde Stubbs
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ResponseCookiesSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@@ -50,27 +50,41 @@ public class ResponseCookiesSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void responseWithCookies() throws IOException {
|
||||
new ResponseCookiesSnippet(Arrays.asList(CookieDocumentation.cookieWithName("X-Test").description("one"),
|
||||
CookieDocumentation.cookieWithName("Content-Type").description("two"),
|
||||
CookieDocumentation.cookieWithName("Etag").description("three"),
|
||||
CookieDocumentation.cookieWithName("Cache-Control").description("five"),
|
||||
CookieDocumentation.cookieWithName("Vary").description("six")))
|
||||
.document(this.operationBuilder.response().cookie("X-Test", "test")
|
||||
.cookie("Content-Type", "application/json").cookie("Etag", "lskjadldj3ii32l2ij23")
|
||||
.cookie("Cache-Control", "max-age=0").cookie("Vary", "User-Agent").build());
|
||||
assertThat(this.generatedSnippets.responseCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`X-Test`", "one").row("`Content-Type`", "two")
|
||||
.row("`Etag`", "three").row("`Cache-Control`", "five").row("`Vary`", "six"));
|
||||
new ResponseCookiesSnippet(Arrays.asList(cookieWithName("has_recent_activity").description("one"),
|
||||
cookieWithName("user_session").description("two")))
|
||||
.document(this.operationBuilder.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row("`has_recent_activity`", "one").row("`user_session`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void undocumentedResponseCookie() throws IOException {
|
||||
new ResponseCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("X-Test").description("one")))
|
||||
.document(this.operationBuilder.response().cookie("X-Test", "test")
|
||||
.cookie("Content-Type", "*/*").build());
|
||||
public void ignoredResponseCookie() throws IOException {
|
||||
new ResponseCookiesSnippet(Arrays.asList(cookieWithName("has_recent_activity").ignored(),
|
||||
cookieWithName("user_session").description("two")))
|
||||
.document(this.operationBuilder.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").build());
|
||||
assertThat(this.generatedSnippets.responseCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
.is(tableWithHeader("Name", "Description").row("`user_session`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allUndocumentedResponseCookiesCanBeIgnored() throws IOException {
|
||||
new ResponseCookiesSnippet(Arrays.asList(cookieWithName("has_recent_activity").description("one"),
|
||||
cookieWithName("user_session").description("two")), true)
|
||||
.document(this.operationBuilder.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").cookie("some_cookie", "value").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row("`has_recent_activity`", "one").row("`user_session`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingOptionalResponseCookie() throws IOException {
|
||||
new ResponseCookiesSnippet(Arrays.asList(cookieWithName("has_recent_activity").description("one").optional(),
|
||||
cookieWithName("user_session").description("two")))
|
||||
.document(this.operationBuilder.response().cookie("user_session", "1234abcd5678efgh").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row("`has_recent_activity`", "one").row("`user_session`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,12 +92,11 @@ public class ResponseCookiesSnippetTests extends AbstractSnippetTests {
|
||||
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
|
||||
given(resolver.resolveTemplateResource("response-cookies"))
|
||||
.willReturn(snippetResource("response-cookies-with-title"));
|
||||
new ResponseCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("X-Test").description("one")),
|
||||
new ResponseCookiesSnippet(Collections.singletonList(cookieWithName("has_recent_activity").description("one")),
|
||||
attributes(key("title").value("Custom title")))
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver))
|
||||
.response().cookie("X-Test", "test").build());
|
||||
.response().cookie("has_recent_activity", "true").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).contains("Custom title");
|
||||
}
|
||||
|
||||
@@ -93,40 +106,45 @@ public class ResponseCookiesSnippetTests extends AbstractSnippetTests {
|
||||
given(resolver.resolveTemplateResource("response-cookies"))
|
||||
.willReturn(snippetResource("response-cookies-with-extra-column"));
|
||||
new ResponseCookiesSnippet(Arrays.asList(
|
||||
CookieDocumentation.cookieWithName("X-Test").description("one").attributes(key("foo").value("alpha")),
|
||||
CookieDocumentation.cookieWithName("Content-Type").description("two")
|
||||
.attributes(key("foo").value("bravo")),
|
||||
CookieDocumentation.cookieWithName("Etag").description("three")
|
||||
.attributes(key("foo").value("charlie"))))
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver))
|
||||
.response().cookie("X-Test", "test").cookie("Content-Type", "application/json")
|
||||
.cookie("Etag", "lskjadldj3ii32l2ij23").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description", "Foo")
|
||||
.row("X-Test", "one", "alpha").row("Content-Type", "two", "bravo").row("Etag", "three", "charlie"));
|
||||
cookieWithName("has_recent_activity").description("one").attributes(key("foo").value("alpha")),
|
||||
cookieWithName("user_session").description("two").attributes(key("foo").value("bravo")),
|
||||
cookieWithName("color_theme").description("three").attributes(key("foo").value("charlie"))))
|
||||
.document(this.operationBuilder
|
||||
.attribute(TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver))
|
||||
.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").cookie("color_theme", "high_contrast")
|
||||
.build());
|
||||
assertThat(this.generatedSnippets.responseCookies())
|
||||
.is(tableWithHeader("Name", "Description", "Foo").row("has_recent_activity", "one", "alpha")
|
||||
.row("user_session", "two", "bravo").row("color_theme", "three", "charlie"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
CookieDocumentation
|
||||
.responseCookies(CookieDocumentation.cookieWithName("X-Test").description("one"),
|
||||
CookieDocumentation.cookieWithName("Content-Type").description("two"),
|
||||
CookieDocumentation.cookieWithName("Etag").description("three"))
|
||||
.and(CookieDocumentation.cookieWithName("Cache-Control").description("five"),
|
||||
CookieDocumentation.cookieWithName("Vary").description("six"))
|
||||
.document(this.operationBuilder.response().cookie("X-Test", "test")
|
||||
.cookie("Content-Type", "application/json").cookie("Etag", "lskjadldj3ii32l2ij23")
|
||||
.cookie("Cache-Control", "max-age=0").cookie("Vary", "User-Agent").build());
|
||||
assertThat(this.generatedSnippets.responseCookies())
|
||||
.is(tableWithHeader("Name", "Description").row("`X-Test`", "one").row("`Content-Type`", "two")
|
||||
.row("`Etag`", "three").row("`Cache-Control`", "five").row("`Vary`", "six"));
|
||||
.responseCookies(cookieWithName("has_recent_activity").description("one"),
|
||||
cookieWithName("user_session").description("two"))
|
||||
.and(cookieWithName("color_theme").description("three"))
|
||||
.document(this.operationBuilder.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").cookie("color_theme", "light").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row("`has_recent_activity`", "one").row("`user_session`", "two").row("`color_theme`", "three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptorsWithRelaxedResponseCookies() throws IOException {
|
||||
CookieDocumentation.relaxedResponseCookies(cookieWithName("has_recent_activity").description("one"))
|
||||
.and(cookieWithName("color_theme").description("two"))
|
||||
.document(this.operationBuilder.response().cookie("has_recent_activity", "true")
|
||||
.cookie("user_session", "1234abcd5678efgh").cookie("color_theme", "light").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(
|
||||
tableWithHeader("Name", "Description").row("`has_recent_activity`", "one").row("`color_theme`", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tableCellContentIsEscapedWhenNecessary() throws IOException {
|
||||
new ResponseCookiesSnippet(
|
||||
Collections.singletonList(CookieDocumentation.cookieWithName("Foo|Bar").description("one|two")))
|
||||
.document(this.operationBuilder.response().cookie("Foo|Bar", "baz").build());
|
||||
new ResponseCookiesSnippet(Collections.singletonList(cookieWithName("Foo|Bar").description("one|two")))
|
||||
.document(this.operationBuilder.response().cookie("Foo|Bar", "baz").build());
|
||||
assertThat(this.generatedSnippets.responseCookies()).is(tableWithHeader("Name", "Description")
|
||||
.row(escapeIfNecessary("`Foo|Bar`"), escapeIfNecessary("one|two")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user