Skip to content

Updated SimpleSavedRequest#getMethod #8686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}