Class ContentRequestMatchers

java.lang.Object
org.springframework.test.web.client.match.ContentRequestMatchers

public class ContentRequestMatchers extends Object
Factory for request content RequestMatcher's. An instance of this class is typically accessed via MockRestRequestMatchers.content().
Since:
3.2
Author:
Rossen Stoyanchev
  • Constructor Details

  • Method Details

    • contentType

      public RequestMatcher contentType(String expectedContentType)
      Assert the request content type as a String.
    • contentType

      public RequestMatcher contentType(MediaType expectedContentType)
      Assert the request content type as a MediaType.
    • contentTypeCompatibleWith

      public RequestMatcher contentTypeCompatibleWith(String contentType)
      Assert the request content type is compatible with the given content type as defined by MediaType.isCompatibleWith(MediaType).
    • contentTypeCompatibleWith

      public RequestMatcher contentTypeCompatibleWith(MediaType contentType)
      Assert the request content type is compatible with the given content type as defined by MediaType.isCompatibleWith(MediaType).
    • string

      public RequestMatcher string(Matcher<? super String> matcher)
      Get the body of the request as a UTF-8 string and apply the given Matcher.
    • string

      public RequestMatcher string(String expectedContent)
      Get the body of the request as a UTF-8 string and compare it to the given String.
    • bytes

      public RequestMatcher bytes(byte[] expectedContent)
      Compare the body of the request to the given byte array.
    • formData

      public RequestMatcher formData(MultiValueMap<String,String> expected)
      Parse the body as form data and compare to the given MultiValueMap.
      Since:
      4.3
    • formDataContains

      public RequestMatcher formDataContains(Map<String,String> expected)
      Variant of formData(MultiValueMap) that matches the given subset of expected form parameters.
      Since:
      5.3
    • multipartData

      public RequestMatcher multipartData(MultiValueMap<String,?> expectedMap)
      Parse the body as multipart data and assert it contains exactly the values from the given MultiValueMap. Values may be of type:
      • String - form field
      • Resource - content from a file
      • byte[] - other raw content

      Note: This method uses the fork of Commons FileUpload library packaged with Apache Tomcat in the org.apache.tomcat.util.http.fileupload package to parse the multipart data and it must be on the test classpath.

      Parameters:
      expectedMap - the expected multipart values
      Since:
      5.3
    • multipartData

      public RequestMatcher multipartData(MultiValueMap<String,?> expectedMap, Charset defaultCharset)
      Variant of multipartData(MultiValueMap) with a defaultCharset.
      Since:
      6.2
    • multipartDataContains

      public RequestMatcher multipartDataContains(Map<String,?> expectedMap)
      Variant of multipartData(MultiValueMap) that does the same but only for a subset of the actual values.

      Note: This method uses the fork of Commons FileUpload library packaged with Apache Tomcat in the org.apache.tomcat.util.http.fileupload package to parse the multipart data and it must be on the test classpath.

      Parameters:
      expectedMap - the expected multipart values
      Since:
      5.3
    • xml

      public RequestMatcher xml(String expectedXmlContent)
      Parse the request body and the given String as XML and assert that the two are "similar" - i.e. they contain the same elements and attributes regardless of order.

      Use of this matcher assumes the XMLUnit library is available.

      Parameters:
      expectedXmlContent - the expected XML content
    • node

      public RequestMatcher node(Matcher<? super Node> matcher)
      Parse the request content as Node and apply the given Matcher.
    • source

      public RequestMatcher source(Matcher<? super Source> matcher)
      Parse the request content as DOMSource and apply the given Matcher.
      See Also:
    • json

      public RequestMatcher json(String expectedJsonContent)
      Parse the expected and actual strings as JSON and assert the two are "similar" - i.e. they contain the same attribute-value pairs regardless of formatting with a lenient checking (extensible, and non-strict array ordering).

      Use of this matcher requires the JSONassert library.

      Parameters:
      expectedJsonContent - the expected JSON content
      Since:
      5.0.5
    • json

      @Deprecated(since="6.2") public RequestMatcher json(String expectedJsonContent, boolean strict)
      Deprecated.
      Parse the request body and the given string as JSON and assert the two are "similar" - i.e. they contain the same attribute-value pairs regardless of formatting.

      Can compare in two modes, depending on strict parameter value:

      • true: strict checking. Not extensible, and strict array ordering.
      • false: lenient checking. Extensible, and non-strict array ordering.

      Use of this matcher requires the JSONassert library.

      Parameters:
      expectedJsonContent - the expected JSON content
      strict - enables strict checking
      Since:
      5.0.5
    • json

      public RequestMatcher json(String expectedJsonContent, JsonCompareMode compareMode)
      Parse the request body and the given string as JSON and assert the two using the given mode. If the comparison failed, throws an AssertionError with the message of the JsonComparison.

      Use of this matcher requires the JSONassert library.

      Parameters:
      expectedJsonContent - the expected JSON content
      compareMode - the compare mode
      Since:
      6.2
    • json

      public RequestMatcher json(String expectedJsonContent, JsonComparator comparator)
      Parse the request body and the given string as JSON and assert the two using the given JsonComparator. If the comparison failed, throws an AssertionError with the message of the JsonComparison.

      Use this matcher if you require a custom JSONAssert configuration or if you desire to use another assertion library.

      Parameters:
      expectedJsonContent - the expected JSON content
      comparator - the comparator to use
      Since:
      6.2