Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#setQueryString()

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setQueryString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlashMapManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void flashAttributesWithQueryParamsWithSpace() throws Exception {

	String encodedValue = URLEncoder.encode("1 2", "UTF-8");

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", encodedValue);

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=" + encodedValue);
	requestAfterRedirect.addParameter("param", "1 2");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 2
Source File: FlashMapManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void saveOutputFlashMapDecodeParameters() throws Exception {

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", "%D0%90%D0%90");
	flashMap.addTargetRequestParam("param", "%D0%91%D0%91");
	flashMap.addTargetRequestParam("param", "%D0%92%D0%92");
	flashMap.addTargetRequestParam("%3A%2F%3F%23%5B%5D%40", "value");

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=%D0%90%D0%90&param=%D0%91%D0%91&param=%D0%92%D0%92&%3A%2F%3F%23%5B%5D%40=value");
	requestAfterRedirect.addParameter("param", "\u0410\u0410");
	requestAfterRedirect.addParameter("param", "\u0411\u0411");
	requestAfterRedirect.addParameter("param", "\u0412\u0412");
	requestAfterRedirect.addParameter(":/?#[]@", "value");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 3
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void flashAttributesWithQueryParamsWithSpace() throws Exception {

	String encodedValue = URLEncoder.encode("1 2", "UTF-8");

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", encodedValue);

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=" + encodedValue);
	requestAfterRedirect.addParameter("param", "1 2");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 4
Source File: RequestLoggingFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void uri() throws Exception {
	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setQueryString("booking=42");

	FilterChain filterChain = new NoOpFilterChain();

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.beforeRequestMessage);
	assertTrue(filter.beforeRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.beforeRequestMessage.contains("booking=42"));

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.afterRequestMessage.contains("booking=42"));
}
 
Example 5
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequest() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/path");
	request.setQueryString("a=1");

	UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
	assertEquals("http", result.getScheme());
	assertEquals("localhost", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/path", result.getPath());
	assertEquals("a=1", result.getQuery());
}
 
Example 6
Source File: RequestLoggingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void uri() throws Exception {
	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setQueryString("booking=42");

	FilterChain filterChain = new NoOpFilterChain();
	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.beforeRequestMessage);
	assertTrue(filter.beforeRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.beforeRequestMessage.contains("booking=42"));

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.afterRequestMessage.contains("booking=42"));
}
 
Example 7
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromHttpRequest() throws URISyntaxException {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/path");
	request.setQueryString("a=1");

	UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
	assertEquals("http", result.getScheme());
	assertEquals("localhost", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/path", result.getPath());
	assertEquals("a=1", result.getQuery());
}
 
Example 8
Source File: InternalResourceViewTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void forward() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
	request.setContextPath("/mycontext");
	request.setServletPath("/myservlet");
	request.setPathInfo(";mypathinfo");
	request.setQueryString("?param1=value1");

	view.setUrl(url);
	view.setServletContext(new MockServletContext() {
		@Override
		public int getMinorVersion() {
			return 4;
		}
	});

	view.render(model, request, response);
	assertEquals(url, response.getForwardedUrl());

	model.forEach((key, value) -> assertEquals("Values for model key '" + key
			+ "' must match", value, request.getAttribute(key)));
}
 
Example 9
Source File: InternalResourceViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void forward() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
	request.setContextPath("/mycontext");
	request.setServletPath("/myservlet");
	request.setPathInfo(";mypathinfo");
	request.setQueryString("?param1=value1");

	view.setUrl(url);
	view.setServletContext(new MockServletContext() {
		@Override
		public int getMinorVersion() {
			return 4;
		}
	});

	view.render(model, request, response);
	assertEquals(url, response.getForwardedUrl());

	model.keySet().stream().forEach(
		key -> assertEquals("Values for model key '" + key + "' must match", model.get(key), request.getAttribute(key))
	);
}
 
Example 10
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void flashAttributesWithQueryParamsWithSpace() throws Exception {

	String encodedValue = URLEncoder.encode("1 2", "UTF-8");

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", encodedValue);

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=" + encodedValue);
	requestAfterRedirect.addParameter("param", "1 2");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 11
Source File: RequestLoggingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void queryStringIncluded() throws Exception {
	filter.setIncludeQueryString(true);

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setQueryString("booking=42");

	FilterChain filterChain = new NoOpFilterChain();
	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.beforeRequestMessage);
	assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels?booking=42]"));

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("[uri=/hotels?booking=42]"));
}
 
Example 12
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequest() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/path");
	request.setQueryString("a=1");

	UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
	assertEquals("http", result.getScheme());
	assertEquals("localhost", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/path", result.getPath());
	assertEquals("a=1", result.getQuery());
}
 
Example 13
Source File: RequestLoggingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void uri() throws Exception {
	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setQueryString("booking=42");

	FilterChain filterChain = new NoOpFilterChain();
	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.beforeRequestMessage);
	assertTrue(filter.beforeRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.beforeRequestMessage.contains("booking=42"));

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("uri=/hotel"));
	assertFalse(filter.afterRequestMessage.contains("booking=42"));
}
 
Example 14
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void uriBuilder() {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/path");
	servletRequest.setQueryString("a=1");

	DefaultServerRequest request =
			new DefaultServerRequest(servletRequest, this.messageConverters);

	URI result = request.uriBuilder().build();
	assertEquals("http", result.getScheme());
	assertEquals("localhost", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/path", result.getPath());
	assertEquals("a=1", result.getQuery());
}
 
Example 15
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void saveOutputFlashMapDecodeParameters() throws Exception {

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", "%D0%90%D0%90");
	flashMap.addTargetRequestParam("param", "%D0%91%D0%91");
	flashMap.addTargetRequestParam("param", "%D0%92%D0%92");
	flashMap.addTargetRequestParam("%3A%2F%3F%23%5B%5D%40", "value");

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString(
			"param=%D0%90%D0%90&param=%D0%91%D0%91&param=%D0%92%D0%92&%3A%2F%3F%23%5B%5D%40=value");
	requestAfterRedirect.addParameter("param", "\u0410\u0410");
	requestAfterRedirect.addParameter("param", "\u0411\u0411");
	requestAfterRedirect.addParameter("param", "\u0412\u0412");
	requestAfterRedirect.addParameter(":/?#[]@", "value");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 16
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void saveOutputFlashMapDecodeParameters() throws Exception {

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", "%D0%90%D0%90");
	flashMap.addTargetRequestParam("param", "%D0%91%D0%91");
	flashMap.addTargetRequestParam("param", "%D0%92%D0%92");
	flashMap.addTargetRequestParam("%3A%2F%3F%23%5B%5D%40", "value");

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString(
			"param=%D0%90%D0%90&param=%D0%91%D0%91&param=%D0%92%D0%92&%3A%2F%3F%23%5B%5D%40=value");
	requestAfterRedirect.addParameter("param", "\u0410\u0410");
	requestAfterRedirect.addParameter("param", "\u0411\u0411");
	requestAfterRedirect.addParameter("param", "\u0412\u0412");
	requestAfterRedirect.addParameter(":/?#[]@", "value");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example 17
Source File: FormTagTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	request.setRequestURI(REQUEST_URI);
	request.setQueryString(QUERY_STRING);
	this.request = request;
}
 
Example 18
Source File: FormTagTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	request.setRequestURI(REQUEST_URI);
	request.setQueryString(QUERY_STRING);
	this.request = request;
}
 
Example 19
Source File: RequestLoggingFilterTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void queryString() throws Exception {
	filter.setIncludeQueryString(true);

	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setQueryString("booking=42");

	FilterChain filterChain = new NoOpFilterChain();

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.beforeRequestMessage);
	assertTrue(filter.beforeRequestMessage.contains("uri=/hotels?booking=42"));

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("uri=/hotels?booking=42"));
}
 
Example 20
Source File: FormTagTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	request.setRequestURI(REQUEST_URI);
	request.setQueryString(QUERY_STRING);
	this.request = request;
}