|
| 1 | +/* |
| 2 | + * Copyright 2004-2025 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.security.web.servlet; |
| 18 | + |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import org.springframework.http.HttpMethod; |
| 28 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +public final class TestMockHttpServletRequests { |
| 32 | + |
| 33 | + private TestMockHttpServletRequests() { |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public static Builder get() { |
| 38 | + return new Builder(HttpMethod.GET); |
| 39 | + } |
| 40 | + |
| 41 | + public static Builder get(String url) { |
| 42 | + return get().applyUrl(url); |
| 43 | + } |
| 44 | + |
| 45 | + public static Builder post() { |
| 46 | + return new Builder(HttpMethod.POST); |
| 47 | + } |
| 48 | + |
| 49 | + public static Builder post(String url) { |
| 50 | + return post().applyUrl(url); |
| 51 | + } |
| 52 | + |
| 53 | + public static Builder request(String method) { |
| 54 | + return new Builder(HttpMethod.valueOf(method)); |
| 55 | + } |
| 56 | + |
| 57 | + public static final class Builder { |
| 58 | + |
| 59 | + private static final Pattern URL = Pattern.compile("((?<scheme>https?)://)?" |
| 60 | + + "((?<hostname>[^:/]+)(:(?<port>\\d+))?)?" + "(?<path>[^?]+)?" + "(\\?(?<query>.*))?"); |
| 61 | + |
| 62 | + private final HttpMethod method; |
| 63 | + |
| 64 | + private String requestUri; |
| 65 | + |
| 66 | + private final Map<String, String> parameters = new LinkedHashMap<>(); |
| 67 | + |
| 68 | + private String scheme = MockHttpServletRequest.DEFAULT_SCHEME; |
| 69 | + |
| 70 | + private int port = MockHttpServletRequest.DEFAULT_SERVER_PORT; |
| 71 | + |
| 72 | + private String hostname = MockHttpServletRequest.DEFAULT_SERVER_NAME; |
| 73 | + |
| 74 | + private String contextPath; |
| 75 | + |
| 76 | + private String servletPath; |
| 77 | + |
| 78 | + private String pathInfo; |
| 79 | + |
| 80 | + private String queryString; |
| 81 | + |
| 82 | + private Builder(HttpMethod method) { |
| 83 | + this.method = method; |
| 84 | + } |
| 85 | + |
| 86 | + private Builder applyUrl(String url) { |
| 87 | + Matcher matcher = URL.matcher(url); |
| 88 | + if (matcher.matches()) { |
| 89 | + applyElement(this::scheme, matcher.group("scheme")); |
| 90 | + applyElement(this::port, matcher.group("port")); |
| 91 | + applyElement(this::serverName, matcher.group("hostname")); |
| 92 | + applyElement(this::requestUri, matcher.group("path")); |
| 93 | + applyElement(this::queryString, matcher.group("query")); |
| 94 | + } |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + private <T> void applyElement(Consumer<T> apply, T value) { |
| 99 | + if (value != null) { |
| 100 | + apply.accept(value); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public Builder requestUri(String contextPath, String servletPath, String pathInfo) { |
| 105 | + this.contextPath = contextPath; |
| 106 | + this.servletPath = servletPath; |
| 107 | + this.pathInfo = pathInfo; |
| 108 | + this.requestUri = Stream.of(contextPath, servletPath, pathInfo) |
| 109 | + .filter(StringUtils::hasText) |
| 110 | + .collect(Collectors.joining()); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public Builder requestUri(String requestUri) { |
| 115 | + return requestUri(null, requestUri, null); |
| 116 | + } |
| 117 | + |
| 118 | + public Builder param(String name, String value) { |
| 119 | + this.parameters.put(name, value); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + private Builder port(String port) { |
| 124 | + if (port != null) { |
| 125 | + this.port = Integer.parseInt(port); |
| 126 | + } |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public Builder port(int port) { |
| 131 | + this.port = port; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public Builder queryString(String queryString) { |
| 136 | + this.queryString = queryString; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + public Builder scheme(String scheme) { |
| 141 | + this.scheme = scheme; |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + public Builder serverName(String serverName) { |
| 146 | + this.hostname = serverName; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public MockHttpServletRequest build() { |
| 151 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 152 | + applyElement(request::setContextPath, this.contextPath); |
| 153 | + applyElement(request::setContextPath, this.contextPath); |
| 154 | + applyElement(request::setMethod, this.method.name()); |
| 155 | + applyElement(request::setParameters, this.parameters); |
| 156 | + applyElement(request::setPathInfo, this.pathInfo); |
| 157 | + applyElement(request::setServletPath, this.servletPath); |
| 158 | + applyElement(request::setScheme, this.scheme); |
| 159 | + applyElement(request::setServerPort, this.port); |
| 160 | + applyElement(request::setServerName, this.hostname); |
| 161 | + applyElement(request::setQueryString, this.queryString); |
| 162 | + applyElement(request::setRequestURI, this.requestUri); |
| 163 | + request.setSecure("https".equals(this.scheme)); |
| 164 | + return request; |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments