Add JSON matcher to assert on request body

Support asserting JSON regardless of order and formatting.
Based on same JsonExpectationHelper used in ContentResultMatchers.

Issue: SPR-13919
This commit is contained in:
Bronwyn Perry-Huston
2016-02-26 06:49:37 -08:00
committed by sdeleuze
parent 1db7e02de3
commit d64f2eb038
2 changed files with 94 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -142,4 +142,52 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().node(hasXPath("/foo/bar/bar")).match(this.request);
}
@Test
public void testJsonLenientMatch() throws Exception {
String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is allowed\" \n}";
this.request.getBody().write(content.getBytes());
MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}")
.match(this.request);
MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", false)
.match(this.request);
}
@Test
public void testJsonStrictMatch() throws Exception {
String content = "{\n \"foo\": \"bar\", \"foo array\":[\"first\",\"second\"] \n}";
this.request.getBody().write(content.getBytes());
MockRestRequestMatchers
.content()
.json("{\n \"foo array\":[\"first\",\"second\"] , \"foo\": \"bar\" \n}", true)
.match(this.request);
}
@Test(expected = AssertionError.class)
public void testJsonLenientNoMatch() throws Exception {
String content = "{\n \"bar\" : \"foo\" \n}";
this.request.getBody().write(content.getBytes());
MockRestRequestMatchers
.content()
.json("{\n \"foo\" : \"bar\" \n}")
.match(this.request);
MockRestRequestMatchers
.content()
.json("{\n \"foo\" : \"bar\" \n}", false)
.match(this.request);
}
@Test(expected = AssertionError.class)
public void testJsonStrictNoMatch() throws Exception {
String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is NOT allowed\" \n}";
this.request.getBody().write(content.getBytes());
MockRestRequestMatchers
.content()
.json("{\n \"foo array\":[\"second\",\"first\"] \n}", true)
.match(this.request);
}
}