|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | + |
| 17 | +package org.springframework.test.web.servlet.request; |
| 18 | + |
| 19 | +import jakarta.servlet.ServletContext; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.http.HttpMethod; |
| 23 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 24 | +import org.springframework.mock.web.MockServletContext; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for {@link AbstractMockHttpServletRequestBuilder} |
| 30 | + * |
| 31 | + * @author Stephane Nicoll |
| 32 | + */ |
| 33 | +class AbstractMockHttpServletRequestBuilderTests { |
| 34 | + |
| 35 | + private final ServletContext servletContext = new MockServletContext(); |
| 36 | + |
| 37 | + |
| 38 | + @Test |
| 39 | + void mergeUriWhenUriIsNotSet() { |
| 40 | + TestRequestBuilder parentBuilder = new TestRequestBuilder(HttpMethod.GET).uri("/test"); |
| 41 | + TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.POST); |
| 42 | + builder.merge(parentBuilder); |
| 43 | + |
| 44 | + MockHttpServletRequest request = buildRequest(builder); |
| 45 | + assertThat(request.getRequestURI()).isEqualTo("/test"); |
| 46 | + assertThat(request.getMethod()).isEqualTo(HttpMethod.POST.name()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void mergeUriWhenUriIsSetDoesNotOverride() { |
| 51 | + TestRequestBuilder parentBuilder = new TestRequestBuilder(HttpMethod.GET).uri("/test"); |
| 52 | + TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.POST).uri("/another"); |
| 53 | + builder.merge(parentBuilder); |
| 54 | + |
| 55 | + MockHttpServletRequest request = buildRequest(builder); |
| 56 | + assertThat(request.getRequestURI()).isEqualTo("/another"); |
| 57 | + assertThat(request.getMethod()).isEqualTo(HttpMethod.POST.name()); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) { |
| 62 | + return builder.buildRequest(this.servletContext); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + private static class TestRequestBuilder extends AbstractMockHttpServletRequestBuilder<TestRequestBuilder> { |
| 67 | + |
| 68 | + TestRequestBuilder(HttpMethod httpMethod) { |
| 69 | + super(httpMethod); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments