Skip to content

Commit

Permalink
Updated SimpleSavedRequest#getMethod
Browse files Browse the repository at this point in the history
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 spring-projectsgh-8675
  • Loading branch information
sentinalll authored and Roman Sydorov committed Jun 19, 2020
1 parent eb351f4 commit aa4663e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public SimpleSavedRequest(SavedRequest request) {
}
this.locales = request.getLocales();
this.parameters = request.getParameterMap();
this.method = request.getMethod();
}

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

@Override
public String getMethod() {
return null;
return this.method;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.savedrequest;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.Cookie;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class SimpleSavedRequestTests {

@Test
public void constructorWhenGivenSavedRequestThenCopies() {
SavedRequest savedRequest = new SimpleSavedRequest(prepareSavedRequest());

assertThat(savedRequest.getMethod()).isEqualTo("POST");

List<Cookie> cookies = savedRequest.getCookies();
assertThat(cookies).hasSize(1);
Cookie cookie = cookies.get(0);
assertThat(cookie.getName()).isEqualTo("cookiename");
assertThat(cookie.getValue()).isEqualTo("cookievalue");

Collection<String> headerNames = savedRequest.getHeaderNames();
assertThat(headerNames).hasSize(1);
String headerName = headerNames.iterator().next();
assertThat(headerName).isEqualTo("headername");

List<String> headerValues = savedRequest.getHeaderValues("headername");
assertThat(headerValues).hasSize(1);
String headerValue = headerValues.get(0);
assertThat(headerValue).isEqualTo("headervalue");

List<Locale> locales = savedRequest.getLocales();
assertThat(locales).hasSize(1);
Locale locale = locales.get(0);
assertThat(locale).isEqualTo(Locale.ENGLISH);

Map<String, String[]> parameterMap = savedRequest.getParameterMap();
assertThat(parameterMap).hasSize(1);
String[] values = parameterMap.get("key");
assertThat(values).hasSize(1);
assertThat(values[0]).isEqualTo("value");
}

@Test
public void constructorWhenGivenRedirectUrlThenDefaultValues() {
SavedRequest savedRequest = new SimpleSavedRequest("redirectUrl");

assertThat(savedRequest.getMethod()).isEqualTo("GET");
assertThat(savedRequest.getCookies()).isEmpty();
assertThat(savedRequest.getHeaderNames()).isEmpty();
assertThat(savedRequest.getHeaderValues("headername")).isEmpty();
assertThat(savedRequest.getLocales()).isEmpty();
assertThat(savedRequest.getParameterMap()).isEmpty();
}

private SimpleSavedRequest prepareSavedRequest() {
SimpleSavedRequest simpleSavedRequest = new SimpleSavedRequest("redirectUrl");
simpleSavedRequest.setCookies(Collections.singletonList(new Cookie("cookiename", "cookievalue")));
simpleSavedRequest.setMethod("POST");
simpleSavedRequest.setHeaders(Collections.singletonMap("headername", Collections.singletonList("headervalue")));
simpleSavedRequest.setLocales(Collections.singletonList(Locale.ENGLISH));
simpleSavedRequest.setParameters(Collections.singletonMap("key", new String[] {"value"}));
return simpleSavedRequest;
}

}

0 comments on commit aa4663e

Please sign in to comment.