spring-framework / org.springframework.test.web.servlet.htmlunit

Package org.springframework.test.web.servlet.htmlunit

Types

DelegatingWebConnection

class DelegatingWebConnection : WebConnection

Implementation of WebConnection that allows delegating to various WebConnection implementations.

For example, if you host your JavaScript on the domain code.jquery.com, you might want to use the following.

 WebClient webClient = new WebClient(); MockMvc mockMvc = ... MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient); WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); WebConnection httpConnection = new HttpWebConnection(webClient); WebConnection webConnection = new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection)); webClient.setWebConnection(webConnection); WebClient webClient = new WebClient(); webClient.setWebConnection(webConnection); 

HostRequestMatcher

class HostRequestMatcher : WebRequestMatcher

A WebRequestMatcher that allows matching on the host and optionally the port of WebRequest#getUrl().

For example, the following would match any request to the host "code.jquery.com" without regard for the port.

WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com");

Multiple hosts can also be passed in. For example, the following would match any request to the host "code.jquery.com" or the host "cdn.com" without regard for the port.

WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com", "cdn.com");

Alternatively, one can also specify the port. For example, the following would match any request to the host "code.jquery.com" with the port of 80.

WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com:80");

The above cdnMatcher would match "http://code.jquery.com/jquery.js" which has a default port of 80 and "http://code.jquery.com:80/jquery.js". However, it would not match "https://code.jquery.com/jquery.js" which has a default port of 443.

MockMvcWebClientBuilder

open class MockMvcWebClientBuilder : MockMvcWebConnectionBuilderSupport<MockMvcWebClientBuilder>

MockMvcWebClientBuilder simplifies the creation of an HtmlUnit WebClient that delegates to a MockMvc instance.

The MockMvc instance used by the builder may be supplied directly or created transparently from a WebApplicationContext.

MockMvcWebConnection

class MockMvcWebConnection : WebConnection

MockMvcWebConnection enables MockMvc to transform a WebRequest into a WebResponse.

This is the core integration with HtmlUnit.

Example usage can be seen below.

 WebClient webClient = new WebClient(); MockMvc mockMvc = ... MockMvcWebConnection webConnection = new MockMvcWebConnection(mockMvc, webClient); webClient.setWebConnection(webConnection); // Use webClient as normal ... 

MockMvcWebConnectionBuilderSupport

abstract class MockMvcWebConnectionBuilderSupport<T : MockMvcWebConnectionBuilderSupport<T>>

Support class that simplifies the creation of a WebConnection that uses MockMvc and optionally delegates to a real WebConnection for specific requests.

The default is to use MockMvc for requests to localhost and otherwise use a real WebConnection.

UrlRegexRequestMatcher

class UrlRegexRequestMatcher : WebRequestMatcher

A WebRequestMatcher that allows matching on WebRequest#getUrl().toExternalForm() using a regular expression.

For example, if you would like to match on the domain code.jquery.com, you might want to use the following.

 WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");