|
| 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