Improve Readablility of JSON test strings

This improves the readability of the JSON strings used for
testing JSON serialize / deserialize of Spring Security

Issue gh-3736
This commit is contained in:
Rob Winch
2016-09-02 11:00:29 -05:00
parent d4c48dd3e1
commit bd925313af
13 changed files with 389 additions and 233 deletions

View File

@@ -34,8 +34,20 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class CookieMixinTests {
String cookieJson = "{\"@class\": \"javax.servlet.http.Cookie\", \"name\": \"demo\", \"value\": \"cookie1\"," +
"\"comment\": null, \"maxAge\": -1, \"path\": null, \"secure\": false, \"version\": 0, \"isHttpOnly\": false, \"domain\": null}";
// @formatter:off
private static final String COOKIE_JSON = "{"
+ "\"@class\": \"javax.servlet.http.Cookie\", "
+ "\"name\": \"demo\", "
+ "\"value\": \"cookie1\","
+ "\"comment\": null, "
+ "\"maxAge\": -1, "
+ "\"path\": null, "
+ "\"secure\": false, "
+ "\"version\": 0, "
+ "\"isHttpOnly\": false, "
+ "\"domain\": null"
+ "}";
// @formatter:on
ObjectMapper buildObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
@@ -48,12 +60,12 @@ public class CookieMixinTests {
public void serializeCookie() throws JsonProcessingException, JSONException {
Cookie cookie = new Cookie("demo", "cookie1");
String actualString = buildObjectMapper().writeValueAsString(cookie);
JSONAssert.assertEquals(cookieJson, actualString, true);
JSONAssert.assertEquals(COOKIE_JSON, actualString, true);
}
@Test
public void deserializeCookie() throws IOException {
Cookie cookie = buildObjectMapper().readValue(cookieJson, Cookie.class);
Cookie cookie = buildObjectMapper().readValue(COOKIE_JSON, Cookie.class);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("demo");
assertThat(cookie.getDomain()).isEqualTo("");

View File

@@ -38,27 +38,34 @@ import static org.assertj.core.api.Assertions.assertThat;
public class DefaultCsrfTokenMixinTests {
ObjectMapper objectMapper;
String defaultCsrfTokenJson;
// @formatter:off
public static final String CSRF_JSON = "{"
+ "\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", "
+ "\"headerName\": \"csrf-header\", "
+ "\"parameterName\": \"_csrf\", "
+ "\"token\": \"1\""
+ "}";
// @formatter:on
@Before
public void setup() {
objectMapper = new ObjectMapper();
ClassLoader loader = getClass().getClassLoader();
objectMapper.registerModules(SecurityJacksonModules.getModules(loader));
defaultCsrfTokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", " +
"\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
}
@Test
public void defaultCsrfTokenSerializedTest() throws JsonProcessingException, JSONException {
DefaultCsrfToken token = new DefaultCsrfToken("csrf-header", "_csrf", "1");
String serializedJson = objectMapper.writeValueAsString(token);
JSONAssert.assertEquals(defaultCsrfTokenJson, serializedJson, true);
JSONAssert.assertEquals(CSRF_JSON, serializedJson, true);
}
@Test
public void defaultCsrfTokenDeserializeTest() throws IOException {
DefaultCsrfToken token = objectMapper.readValue(defaultCsrfTokenJson, DefaultCsrfToken.class);
DefaultCsrfToken token = objectMapper.readValue(CSRF_JSON, DefaultCsrfToken.class);
assertThat(token).isNotNull();
assertThat(token.getHeaderName()).isEqualTo("csrf-header");
assertThat(token.getParameterName()).isEqualTo("_csrf");

View File

@@ -16,6 +16,12 @@
package org.springframework.security.web.jackson2;
import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import javax.servlet.http.Cookie;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
@@ -25,11 +31,6 @@ import org.springframework.security.web.PortResolverImpl;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
import org.springframework.security.web.savedrequest.SavedCookie;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -38,12 +39,40 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
String defaultSavedRequestJson = "{" +
"\"@class\": \"org.springframework.security.web.savedrequest.DefaultSavedRequest\", \"cookies\": [\"java.util.ArrayList\", [{\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", \"name\": \"SESSION\", \"value\": \"123456789\", \"comment\": null, \"maxAge\": -1, \"path\": null, \"secure\":false, \"version\": 0, \"domain\": null}]]," +
"\"locales\": [\"java.util.ArrayList\", [\"en\"]], \"headers\": {\"@class\": \"java.util.TreeMap\", \"x-auth-token\": [\"java.util.ArrayList\", [\"12\"]]}, \"parameters\": {\"@class\": \"java.util.TreeMap\"}," +
"\"contextPath\": \"\", \"method\": \"\", \"pathInfo\": null, \"queryString\": null, \"requestURI\": \"\", \"requestURL\": \"http://localhost\", \"scheme\": \"http\", " +
"\"serverName\": \"localhost\", \"servletPath\": \"\", \"serverPort\": 80"+
"}";
// @formatter:off
private static final String COOKIES_JSON = "[\"java.util.ArrayList\", [{"
+ "\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", "
+ "\"name\": \"SESSION\", "
+ "\"value\": \"123456789\", "
+ "\"comment\": null, "
+ "\"maxAge\": -1, "
+ "\"path\": null, "
+ "\"secure\":false, "
+ "\"version\": 0, "
+ "\"domain\": null"
+ "}]]";
// @formatter:on
// @formatter:off
private static final String REQUEST_JSON = "{" +
"\"@class\": \"org.springframework.security.web.savedrequest.DefaultSavedRequest\", "
+ "\"cookies\": "+ COOKIES_JSON +","
+ "\"locales\": [\"java.util.ArrayList\", [\"en\"]], "
+ "\"headers\": {\"@class\": \"java.util.TreeMap\", \"x-auth-token\": [\"java.util.ArrayList\", [\"12\"]]}, "
+ "\"parameters\": {\"@class\": \"java.util.TreeMap\"},"
+ "\"contextPath\": \"\", "
+ "\"method\": \"\", "
+ "\"pathInfo\": null, "
+ "\"queryString\": null, "
+ "\"requestURI\": \"\", "
+ "\"requestURL\": \"http://localhost\", "
+ "\"scheme\": \"http\", "
+ "\"serverName\": \"localhost\", "
+ "\"servletPath\": \"\", "
+ "\"serverPort\": 80"
+ "}";
// @formatter:on
@Test
public void matchRequestBuildWithConstructorAndBuilder() {
@@ -66,7 +95,7 @@ public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
request.setCookies(new Cookie("SESSION", "123456789"));
request.addHeader("x-auth-token", "12");
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new DefaultSavedRequest(request, new PortResolverImpl()));
JSONAssert.assertEquals(defaultSavedRequestJson, actualString, true);
JSONAssert.assertEquals(REQUEST_JSON, actualString, true);
}
@Test
@@ -78,12 +107,12 @@ public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
.setLocales(Collections.singletonList(new Locale("en"))).setContextPath("").setMethod("")
.setServletPath("").build();
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(request);
JSONAssert.assertEquals(defaultSavedRequestJson, actualString, true);
JSONAssert.assertEquals(REQUEST_JSON, actualString, true);
}
@Test
public void deserializeDefaultSavedRequest() throws IOException {
DefaultSavedRequest request = (DefaultSavedRequest) buildObjectMapper().readValue(defaultSavedRequestJson, Object.class);
DefaultSavedRequest request = (DefaultSavedRequest) buildObjectMapper().readValue(REQUEST_JSON, Object.class);
assertThat(request).isNotNull();
assertThat(request.getCookies()).hasSize(1);
assertThat(request.getLocales()).hasSize(1).contains(new Locale("en"));

View File

@@ -16,20 +16,21 @@
package org.springframework.security.web.jackson2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.security.web.savedrequest.SavedCookie;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.security.web.savedrequest.SavedCookie;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,59 +38,66 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Jitendra Singh.
*/
public class SavedCookieMixinTests extends AbstractMixinTests {
// @formatter:off
private static final String COOKIE_JSON = "{"
+ "\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", "
+ "\"name\": \"SESSION\", "
+ "\"value\": \"123456789\", "
+ "\"comment\": null, "
+ "\"maxAge\": -1, "
+ "\"path\": null, "
+ "\"secure\":false, "
+ "\"version\": 0, "
+ "\"domain\": null"
+ "}";
// @formatter:on
private String expectedSavedCookieJson;
@Before
public void setup() {
expectedSavedCookieJson = "{\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", " +
"\"name\": \"session\", \"value\": \"123456\", \"comment\": null, \"domain\": null, \"maxAge\": -1, " +
"\"path\": null, \"secure\": false, \"version\": 0}";
}
// @formatter:off
private static final String COOKIES_JSON = "[\"java.util.ArrayList\", ["
+ COOKIE_JSON
+ "]]";
// @formatter:on
@Test
public void serializeWithDefaultConfigurationTest() throws JsonProcessingException, JSONException {
SavedCookie savedCookie = new SavedCookie(new Cookie("session", "123456"));
SavedCookie savedCookie = new SavedCookie(new Cookie("SESSION", "123456789"));
String actualJson = buildObjectMapper().writeValueAsString(savedCookie);
JSONAssert.assertEquals(expectedSavedCookieJson, actualJson, true);
JSONAssert.assertEquals(COOKIE_JSON, actualJson, true);
}
@Test
public void serializeWithOverrideConfigurationTest() throws JsonProcessingException, JSONException {
SavedCookie savedCookie = new SavedCookie(new Cookie("session", "123456"));
SavedCookie savedCookie = new SavedCookie(new Cookie("SESSION", "123456789"));
ObjectMapper mapper = buildObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PUBLIC_ONLY)
.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
String actualJson = mapper.writeValueAsString(savedCookie);
JSONAssert.assertEquals(expectedSavedCookieJson, actualJson, true);
JSONAssert.assertEquals(COOKIE_JSON, actualJson, true);
}
@Test
public void serializeSavedCookieWithList() throws JsonProcessingException, JSONException {
List<SavedCookie> savedCookies = new ArrayList<SavedCookie>();
savedCookies.add(new SavedCookie(new Cookie("session", "123456")));
String expectedJson = String.format("[\"java.util.ArrayList\", [%s]]", expectedSavedCookieJson);
savedCookies.add(new SavedCookie(new Cookie("SESSION", "123456789")));
String actualJson = buildObjectMapper().writeValueAsString(savedCookies);
JSONAssert.assertEquals(expectedJson, actualJson, true);
JSONAssert.assertEquals(COOKIES_JSON, actualJson, true);
}
@Test
@SuppressWarnings("unchecked")
public void deserializeSavedCookieWithList() throws IOException, JSONException {
String expectedJson = String.format("[\"java.util.ArrayList\", [%s]]", expectedSavedCookieJson);
List<SavedCookie> savedCookies = (List<SavedCookie>)buildObjectMapper().readValue(expectedJson, Object.class);
List<SavedCookie> savedCookies = (List<SavedCookie>)buildObjectMapper().readValue(COOKIES_JSON, Object.class);
assertThat(savedCookies).isNotNull().hasSize(1);
assertThat(savedCookies.get(0).getName()).isEqualTo("session");
assertThat(savedCookies.get(0).getValue()).isEqualTo("123456");
assertThat(savedCookies.get(0).getName()).isEqualTo("SESSION");
assertThat(savedCookies.get(0).getValue()).isEqualTo("123456789");
}
@Test
public void deserializeSavedCookieJsonTest() throws IOException {
SavedCookie savedCookie = (SavedCookie) buildObjectMapper().readValue(expectedSavedCookieJson, Object.class);
SavedCookie savedCookie = (SavedCookie) buildObjectMapper().readValue(COOKIE_JSON, Object.class);
assertThat(savedCookie).isNotNull();
assertThat(savedCookie.getName()).isEqualTo("session");
assertThat(savedCookie.getValue()).isEqualTo("123456");
assertThat(savedCookie.getName()).isEqualTo("SESSION");
assertThat(savedCookie.getValue()).isEqualTo("123456789");
assertThat(savedCookie.isSecure()).isEqualTo(false);
assertThat(savedCookie.getVersion()).isEqualTo(0);
assertThat(savedCookie.getComment()).isNull();

View File

@@ -39,8 +39,15 @@ import static org.assertj.core.api.Assertions.assertThat;
public class WebAuthenticationDetailsMixinTests {
ObjectMapper mapper;
String webAuthenticationDetailsJson = "{\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\","
+ "\"sessionId\": \"1\", \"remoteAddress\": \"/localhost\"}";
// @formatter:off
private static final String AUTHENTICATION_DETAILS_JSON = "{"
+ "\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\","
+ "\"sessionId\": \"1\", "
+ "\"remoteAddress\": "
+ "\"/localhost\""
+ "}";
// @formatter:on
@Before
public void setup() {
@@ -58,7 +65,7 @@ public class WebAuthenticationDetailsMixinTests {
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
WebAuthenticationDetails authenticationDetails = this.mapper.readValue(webAuthenticationDetailsJson,
WebAuthenticationDetails authenticationDetails = this.mapper.readValue(AUTHENTICATION_DETAILS_JSON,
WebAuthenticationDetails.class);
assertThat(details.equals(authenticationDetails));
}
@@ -71,13 +78,13 @@ public class WebAuthenticationDetailsMixinTests {
request.setSession(new MockHttpSession(null, "1"));
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
String actualJson = this.mapper.writeValueAsString(details);
JSONAssert.assertEquals(webAuthenticationDetailsJson, actualJson, true);
JSONAssert.assertEquals(AUTHENTICATION_DETAILS_JSON, actualJson, true);
}
@Test
public void webAuthenticationDetailsDeserializeTest()
throws IOException, JSONException {
WebAuthenticationDetails details = this.mapper.readValue(webAuthenticationDetailsJson,
WebAuthenticationDetails details = this.mapper.readValue(AUTHENTICATION_DETAILS_JSON,
WebAuthenticationDetails.class);
assertThat(details).isNotNull();
assertThat(details.getRemoteAddress()).isEqualTo("/localhost");