Class TestMvcClient


  • public class TestMvcClient
    extends Object
    Helper methods for rest api web integration testing from spring-data-rest-tests-core module
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.springframework.http.MediaType DEFAULT_MEDIA_TYPE  
    • Constructor Summary

      Constructors 
      Constructor Description
      TestMvcClient​(org.springframework.test.web.servlet.MockMvc mvc, org.springframework.hateoas.client.LinkDiscoverers discoverers)
      Creates a new TestMvcClient for the given MockMvc and LinkDiscoverers.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertAllowHeaders​(org.springframework.http.HttpEntity<?> response, org.springframework.http.HttpMethod... methods)  
      org.springframework.hateoas.Link assertHasLinkWithRel​(String rel, org.springframework.mock.web.MockHttpServletResponse response)
      For a given servlet response, verify that the provided rel exists in its hypermedia.
      List<org.springframework.hateoas.Link> discover​(String rel)
      Discover list of URIs associated with a rel, starting at the root node ("/")
      List<org.springframework.hateoas.Link> discover​(org.springframework.hateoas.Link root, String rel)
      Given a URI (root), discover the URIs for a given rel.
      org.springframework.hateoas.Link discoverUnique​(String rel)
      Discover single URI associated with a rel, starting at the root node ("/")
      org.springframework.hateoas.Link discoverUnique​(String... rels)
      Traverses the given link relations from the root.
      org.springframework.hateoas.Link discoverUnique​(org.springframework.hateoas.Link root, String rel)
      Given a URI (root), discover the unique URI for a given rel.
      org.springframework.hateoas.Link discoverUnique​(org.springframework.hateoas.Link root, String rel, org.springframework.http.MediaType mediaType)
      Given a URI (root), discover the unique URI for a given rel.
      org.springframework.test.web.servlet.ResultActions follow​(String href)
      Follow URL supplied as a string.
      org.springframework.test.web.servlet.ResultActions follow​(String href, org.springframework.http.MediaType accept)
      Follow URL supplied as a string with a specific Accept header.
      org.springframework.test.web.servlet.ResultActions follow​(org.springframework.hateoas.Link link)
      For a given link, expand the href using URI substitution and then do a simple GET.
      org.springframework.test.web.servlet.ResultActions follow​(org.springframework.hateoas.Link link, org.springframework.http.MediaType accept)
      Folow Link with a specific Accept header (media type).
      org.springframework.hateoas.client.LinkDiscoverer getDiscoverer​(org.springframework.mock.web.MockHttpServletResponse response)
      Using the servlet response's content type, find the corresponding link discoverer.
      org.springframework.test.web.servlet.ResultMatcher hasLinkWithRel​(String rel)
      MockMvc matcher used to verify existence of rel with URI link
      static void initWebTest()
      Initializes web tests.
      org.springframework.mock.web.MockHttpServletResponse request​(String href)
      Convenience wrapper to GET [href] using the default media type.
      org.springframework.mock.web.MockHttpServletResponse request​(String href, org.springframework.http.MediaType contentType)
      Perform GET [href] with an explicit Accept media type using MockMvc.
      org.springframework.mock.web.MockHttpServletResponse request​(String href, org.springframework.http.MediaType contentType, org.springframework.http.HttpHeaders httpHeaders)
      Perform GET [href] with an explicit Accept media type using MockMvc.
      org.springframework.mock.web.MockHttpServletResponse request​(org.springframework.hateoas.Link link)
      Convenience wrapper that first expands the link using URI substitution before requesting with the default media type.
      org.springframework.mock.web.MockHttpServletResponse request​(org.springframework.hateoas.Link link, org.springframework.http.MediaType mediaType)
      Convenience wrapper that first expands the link using URI substitution and then GET [href] using an explicit media type
      TestMvcClient setBasePath​(String uri)
      Set base path uri.
    • Field Detail

      • DEFAULT_MEDIA_TYPE

        public static org.springframework.http.MediaType DEFAULT_MEDIA_TYPE
    • Constructor Detail

      • TestMvcClient

        public TestMvcClient​(org.springframework.test.web.servlet.MockMvc mvc,
                             org.springframework.hateoas.client.LinkDiscoverers discoverers)
        Creates a new TestMvcClient for the given MockMvc and LinkDiscoverers.
        Parameters:
        mvc - must not be null.
        discoverers - must not be null.
    • Method Detail

      • setBasePath

        public TestMvcClient setBasePath​(String uri)
        Set base path uri. Default value is "/"
        Parameters:
        uri - basePath
        Returns:
        TestMvcClient instance
      • initWebTest

        public static void initWebTest()
        Initializes web tests. Will register a MockHttpServletRequest for the current thread.
      • assertAllowHeaders

        public static void assertAllowHeaders​(org.springframework.http.HttpEntity<?> response,
                                              org.springframework.http.HttpMethod... methods)
      • request

        public org.springframework.mock.web.MockHttpServletResponse request​(String href,
                                                                            org.springframework.http.MediaType contentType)
                                                                     throws Exception
        Perform GET [href] with an explicit Accept media type using MockMvc. Verify the requests succeeded and also came back as the Accept type.
        Parameters:
        href -
        contentType -
        Returns:
        a mocked servlet response with results from GET [href]
        Throws:
        Exception
      • request

        public org.springframework.mock.web.MockHttpServletResponse request​(String href,
                                                                            org.springframework.http.MediaType contentType,
                                                                            org.springframework.http.HttpHeaders httpHeaders)
                                                                     throws Exception
        Perform GET [href] with an explicit Accept media type using MockMvc. Verify the requests succeeded and also came back as the Accept type.
        Parameters:
        href -
        contentType -
        Returns:
        a mocked servlet response with results from GET [href]
        Throws:
        Exception
      • request

        public org.springframework.mock.web.MockHttpServletResponse request​(org.springframework.hateoas.Link link)
                                                                     throws Exception
        Convenience wrapper that first expands the link using URI substitution before requesting with the default media type.
        Parameters:
        link -
        Returns:
        Throws:
        Exception
      • request

        public org.springframework.mock.web.MockHttpServletResponse request​(org.springframework.hateoas.Link link,
                                                                            org.springframework.http.MediaType mediaType)
                                                                     throws Exception
        Convenience wrapper that first expands the link using URI substitution and then GET [href] using an explicit media type
        Parameters:
        link -
        mediaType -
        Returns:
        Throws:
        Exception
      • request

        public org.springframework.mock.web.MockHttpServletResponse request​(String href)
                                                                     throws Exception
        Convenience wrapper to GET [href] using the default media type.
        Parameters:
        href -
        Returns:
        Throws:
        Exception
      • follow

        public org.springframework.test.web.servlet.ResultActions follow​(org.springframework.hateoas.Link link)
                                                                  throws Exception
        For a given link, expand the href using URI substitution and then do a simple GET.
        Parameters:
        link -
        Returns:
        Throws:
        Exception
      • follow

        public org.springframework.test.web.servlet.ResultActions follow​(String href)
                                                                  throws Exception
        Follow URL supplied as a string. NOTE: Assumes no URI templates.
        Parameters:
        href -
        Returns:
        Throws:
        Exception
      • follow

        public org.springframework.test.web.servlet.ResultActions follow​(org.springframework.hateoas.Link link,
                                                                         org.springframework.http.MediaType accept)
                                                                  throws Exception
        Folow Link with a specific Accept header (media type).
        Parameters:
        link -
        accept -
        Returns:
        Throws:
        Exception
      • follow

        public org.springframework.test.web.servlet.ResultActions follow​(String href,
                                                                         org.springframework.http.MediaType accept)
                                                                  throws Exception
        Follow URL supplied as a string with a specific Accept header.
        Parameters:
        href -
        accept -
        Returns:
        Throws:
        Exception
      • discover

        public List<org.springframework.hateoas.Link> discover​(String rel)
                                                        throws Exception
        Discover list of URIs associated with a rel, starting at the root node ("/")
        Parameters:
        rel -
        Returns:
        Throws:
        Exception
      • discoverUnique

        public org.springframework.hateoas.Link discoverUnique​(String rel)
                                                        throws Exception
        Discover single URI associated with a rel, starting at the root node ("/")
        Parameters:
        rel -
        Returns:
        Throws:
        Exception
      • discoverUnique

        public org.springframework.hateoas.Link discoverUnique​(String... rels)
                                                        throws Exception
        Traverses the given link relations from the root.
        Parameters:
        rels -
        Returns:
        Throws:
        Exception
      • discover

        public List<org.springframework.hateoas.Link> discover​(org.springframework.hateoas.Link root,
                                                               String rel)
                                                        throws Exception
        Given a URI (root), discover the URIs for a given rel.
        Parameters:
        root - - URI to start from
        rel - - name of the relationship to seek links
        Returns:
        list of Link objects associated with the rel
        Throws:
        Exception
      • discoverUnique

        public org.springframework.hateoas.Link discoverUnique​(org.springframework.hateoas.Link root,
                                                               String rel)
                                                        throws Exception
        Given a URI (root), discover the unique URI for a given rel. NOTE: Assumes there is only one URI
        Parameters:
        root -
        rel -
        Returns:
        Link tied to a given rel
        Throws:
        Exception
      • discoverUnique

        public org.springframework.hateoas.Link discoverUnique​(org.springframework.hateoas.Link root,
                                                               String rel,
                                                               org.springframework.http.MediaType mediaType)
                                                        throws Exception
        Given a URI (root), discover the unique URI for a given rel. NOTE: Assumes there is only one URI
        Parameters:
        root - the link to the resource to access.
        rel - the link relation to discover in the response.
        mediaType - the MediaType to request.
        Returns:
        Link tied to a given rel
        Throws:
        Exception
      • assertHasLinkWithRel

        public org.springframework.hateoas.Link assertHasLinkWithRel​(String rel,
                                                                     org.springframework.mock.web.MockHttpServletResponse response)
                                                              throws Exception
        For a given servlet response, verify that the provided rel exists in its hypermedia. If so, return the URI link.
        Parameters:
        rel -
        response -
        Returns:
        Link of the rel found in the response
        Throws:
        Exception
      • hasLinkWithRel

        public org.springframework.test.web.servlet.ResultMatcher hasLinkWithRel​(String rel)
        MockMvc matcher used to verify existence of rel with URI link
        Parameters:
        rel -
        Returns:
      • getDiscoverer

        public org.springframework.hateoas.client.LinkDiscoverer getDiscoverer​(org.springframework.mock.web.MockHttpServletResponse response)
        Using the servlet response's content type, find the corresponding link discoverer.
        Parameters:
        response -
        Returns:
        LinkDiscoverer