Skip to content

Commit 0f2bc3f

Browse files
committed
AbstractRequestLoggingFilter ignores non-available query string
Issue: SPR-14244 (cherry picked from commit 08ddd1b)
1 parent 1e491f1 commit 0f2bc3f

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -255,7 +255,10 @@ protected String createMessage(HttpServletRequest request, String prefix, String
255255
msg.append(prefix);
256256
msg.append("uri=").append(request.getRequestURI());
257257
if (isIncludeQueryString()) {
258-
msg.append('?').append(request.getQueryString());
258+
String queryString = request.getQueryString();
259+
if (queryString != null) {
260+
msg.append('?').append(queryString);
261+
}
259262
}
260263
if (isIncludeClientInfo()) {
261264
String client = request.getRemoteAddr();

spring-web/src/test/java/org/springframework/web/filter/RequestLoggingFilterTests.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
2626

27-
import org.junit.Before;
2827
import org.junit.Test;
2928

3029
import org.springframework.mock.web.test.MockHttpServletRequest;
@@ -34,19 +33,14 @@
3433
import static org.junit.Assert.*;
3534

3635
/**
37-
* Test for {@link AbstractRequestLoggingFilter} and sub classes.
36+
* Test for {@link AbstractRequestLoggingFilter} and subclasses.
3837
*
3938
* @author Arjen Poutsma
39+
* @author Juergen Hoeller
4040
*/
4141
public class RequestLoggingFilterTests {
4242

43-
private MyRequestLoggingFilter filter;
44-
45-
46-
@Before
47-
public void createFilter() throws Exception {
48-
filter = new MyRequestLoggingFilter();
49-
}
43+
private final MyRequestLoggingFilter filter = new MyRequestLoggingFilter();
5044

5145

5246
@Test
@@ -70,23 +64,39 @@ public void uri() throws Exception {
7064
}
7165

7266
@Test
73-
public void queryString() throws Exception {
67+
public void queryStringIncluded() throws Exception {
7468
filter.setIncludeQueryString(true);
7569

76-
final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
70+
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
7771
MockHttpServletResponse response = new MockHttpServletResponse();
7872

7973
request.setQueryString("booking=42");
8074

8175
FilterChain filterChain = new NoOpFilterChain();
76+
filter.doFilter(request, response, filterChain);
77+
78+
assertNotNull(filter.beforeRequestMessage);
79+
assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels?booking=42]"));
80+
81+
assertNotNull(filter.afterRequestMessage);
82+
assertTrue(filter.afterRequestMessage.contains("[uri=/hotels?booking=42]"));
83+
}
84+
85+
@Test
86+
public void noQueryStringAvailable() throws Exception {
87+
filter.setIncludeQueryString(true);
88+
89+
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
90+
MockHttpServletResponse response = new MockHttpServletResponse();
8291

92+
FilterChain filterChain = new NoOpFilterChain();
8393
filter.doFilter(request, response, filterChain);
8494

8595
assertNotNull(filter.beforeRequestMessage);
86-
assertTrue(filter.beforeRequestMessage.contains("uri=/hotels?booking=42"));
96+
assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels]"));
8797

8898
assertNotNull(filter.afterRequestMessage);
89-
assertTrue(filter.afterRequestMessage.contains("uri=/hotels?booking=42"));
99+
assertTrue(filter.afterRequestMessage.contains("[uri=/hotels]"));
90100
}
91101

92102
@Test
@@ -98,8 +108,8 @@ public void payloadInputStream() throws Exception {
98108

99109
final byte[] requestBody = "Hello World".getBytes("UTF-8");
100110
request.setContent(requestBody);
101-
FilterChain filterChain = new FilterChain() {
102111

112+
FilterChain filterChain = new FilterChain() {
103113
@Override
104114
public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
105115
throws IOException, ServletException {
@@ -124,8 +134,8 @@ public void payloadReader() throws Exception {
124134

125135
final String requestBody = "Hello World";
126136
request.setContent(requestBody.getBytes("UTF-8"));
127-
FilterChain filterChain = new FilterChain() {
128137

138+
FilterChain filterChain = new FilterChain() {
129139
@Override
130140
public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
131141
throws IOException, ServletException {
@@ -151,8 +161,8 @@ public void payloadMaxLength() throws Exception {
151161

152162
final byte[] requestBody = "Hello World".getBytes("UTF-8");
153163
request.setContent(requestBody);
154-
FilterChain filterChain = new FilterChain() {
155164

165+
FilterChain filterChain = new FilterChain() {
156166
@Override
157167
public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
158168
throws IOException, ServletException {

0 commit comments

Comments
 (0)