Skip to content

Commit 896b324

Browse files
sentinallljzheaux
authored andcommitted
Updated SimpleSavedRequest#getMethod
Before: 1. SimpleSavedRequest#getMethod returned null 2. SimpleSavedRequest(SavedRequest request) constructor did not set the method field from request After: 1. SimpleSavedRequest#getMethod returns method property value 2. SimpleSavedRequest(SavedRequest request) constructor sets the method field from request Closes gh-8675
1 parent 815ceae commit 896b324

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

web/src/main/java/org/springframework/security/web/savedrequest/SimpleSavedRequest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public SimpleSavedRequest(SavedRequest request) {
5757
}
5858
this.locales = request.getLocales();
5959
this.parameters = request.getParameterMap();
60+
this.method = request.getMethod();
6061
}
6162

6263
@Override
@@ -71,7 +72,7 @@ public List<Cookie> getCookies() {
7172

7273
@Override
7374
public String getMethod() {
74-
return null;
75+
return this.method;
7576
}
7677

7778
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.web.savedrequest;
17+
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Locale;
22+
import java.util.Map;
23+
import javax.servlet.http.Cookie;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class SimpleSavedRequestTests {
29+
30+
@Test
31+
public void constructorWhenGivenSavedRequestThenCopies() {
32+
SavedRequest savedRequest = new SimpleSavedRequest(prepareSavedRequest());
33+
34+
assertThat(savedRequest.getMethod()).isEqualTo("POST");
35+
36+
List<Cookie> cookies = savedRequest.getCookies();
37+
assertThat(cookies).hasSize(1);
38+
Cookie cookie = cookies.get(0);
39+
assertThat(cookie.getName()).isEqualTo("cookiename");
40+
assertThat(cookie.getValue()).isEqualTo("cookievalue");
41+
42+
Collection<String> headerNames = savedRequest.getHeaderNames();
43+
assertThat(headerNames).hasSize(1);
44+
String headerName = headerNames.iterator().next();
45+
assertThat(headerName).isEqualTo("headername");
46+
47+
List<String> headerValues = savedRequest.getHeaderValues("headername");
48+
assertThat(headerValues).hasSize(1);
49+
String headerValue = headerValues.get(0);
50+
assertThat(headerValue).isEqualTo("headervalue");
51+
52+
List<Locale> locales = savedRequest.getLocales();
53+
assertThat(locales).hasSize(1);
54+
Locale locale = locales.get(0);
55+
assertThat(locale).isEqualTo(Locale.ENGLISH);
56+
57+
Map<String, String[]> parameterMap = savedRequest.getParameterMap();
58+
assertThat(parameterMap).hasSize(1);
59+
String[] values = parameterMap.get("key");
60+
assertThat(values).hasSize(1);
61+
assertThat(values[0]).isEqualTo("value");
62+
}
63+
64+
@Test
65+
public void constructorWhenGivenRedirectUrlThenDefaultValues() {
66+
SavedRequest savedRequest = new SimpleSavedRequest("redirectUrl");
67+
68+
assertThat(savedRequest.getMethod()).isEqualTo("GET");
69+
assertThat(savedRequest.getCookies()).isEmpty();
70+
assertThat(savedRequest.getHeaderNames()).isEmpty();
71+
assertThat(savedRequest.getHeaderValues("headername")).isEmpty();
72+
assertThat(savedRequest.getLocales()).isEmpty();
73+
assertThat(savedRequest.getParameterMap()).isEmpty();
74+
}
75+
76+
private SimpleSavedRequest prepareSavedRequest() {
77+
SimpleSavedRequest simpleSavedRequest = new SimpleSavedRequest("redirectUrl");
78+
simpleSavedRequest.setCookies(Collections.singletonList(new Cookie("cookiename", "cookievalue")));
79+
simpleSavedRequest.setMethod("POST");
80+
simpleSavedRequest.setHeaders(Collections.singletonMap("headername", Collections.singletonList("headervalue")));
81+
simpleSavedRequest.setLocales(Collections.singletonList(Locale.ENGLISH));
82+
simpleSavedRequest.setParameters(Collections.singletonMap("key", new String[] {"value"}));
83+
return simpleSavedRequest;
84+
}
85+
86+
}

0 commit comments

Comments
 (0)