API versioning support for Spring MVC
See gh-34566
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2002-2025 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.web.accept;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultApiVersionStrategy}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class DefaultApiVersionStrategiesTests {
|
||||
|
||||
private final SemanticApiVersionParser parser = new SemanticApiVersionParser();
|
||||
|
||||
|
||||
@Test
|
||||
void defaultVersion() {
|
||||
SemanticApiVersionParser.Version version = this.parser.parseVersion("1.2.3");
|
||||
ApiVersionStrategy strategy = initVersionStrategy(version.toString());
|
||||
|
||||
assertThat(strategy.getDefaultVersion()).isEqualTo(version);
|
||||
}
|
||||
|
||||
@Test
|
||||
void supportedVersions() {
|
||||
SemanticApiVersionParser.Version v1 = this.parser.parseVersion("1");
|
||||
SemanticApiVersionParser.Version v2 = this.parser.parseVersion("2");
|
||||
SemanticApiVersionParser.Version v9 = this.parser.parseVersion("9");
|
||||
|
||||
DefaultApiVersionStrategy strategy = initVersionStrategy(null);
|
||||
strategy.addSupportedVersion(v1.toString());
|
||||
strategy.addSupportedVersion(v2.toString());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
strategy.validateVersion(v1, request);
|
||||
strategy.validateVersion(v2, request);
|
||||
|
||||
assertThatThrownBy(() -> strategy.validateVersion(v9, request))
|
||||
.isInstanceOf(InvalidApiVersionException.class);
|
||||
}
|
||||
|
||||
private static DefaultApiVersionStrategy initVersionStrategy(@Nullable String defaultValue) {
|
||||
return new DefaultApiVersionStrategy(
|
||||
List.of(request -> request.getParameter("api-version")),
|
||||
new SemanticApiVersionParser(), true, defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2002-2025 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.web.accept;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
import org.springframework.web.util.ServletRequestPathUtils;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PathApiVersionResolver}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class PathApiVersionResolverTests {
|
||||
|
||||
@Test
|
||||
void resolve() {
|
||||
testResolve(0, "/1.0/path", "1.0");
|
||||
testResolve(1, "/app/1.1/path", "1.1");
|
||||
}
|
||||
|
||||
private static void testResolve(int index, String requestUri, String expected) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri);
|
||||
try {
|
||||
ServletRequestPathUtils.parseAndCache(request);
|
||||
String actual = new PathApiVersionResolver(index).resolveVersion(request);
|
||||
assertThat(actual).isEqualTo(expected);
|
||||
}
|
||||
finally {
|
||||
ServletRequestPathUtils.clearParsedRequestPath(request);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2002-2025 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.web.accept;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SemanticApiVersionParser}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class SemanticApiVersionParserTests {
|
||||
|
||||
private final SemanticApiVersionParser parser = new SemanticApiVersionParser();
|
||||
|
||||
|
||||
@Test
|
||||
void parse() {
|
||||
testParse("0", 0, 0, 0);
|
||||
testParse("0.3", 0, 3, 0);
|
||||
testParse("4.5", 4, 5, 0);
|
||||
testParse("6.7.8", 6, 7, 8);
|
||||
testParse("v01", 1, 0, 0);
|
||||
testParse("version-1.2", 1, 2, 0);
|
||||
}
|
||||
|
||||
private void testParse(String input, int major, int minor, int patch) {
|
||||
SemanticApiVersionParser.Version actual = this.parser.parseVersion(input);
|
||||
assertThat(actual.getMajor()).isEqualTo(major);
|
||||
assertThat(actual.getMinor()).isEqualTo(minor);
|
||||
assertThat(actual.getPatch()).isEqualTo(patch);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "v", "1a", "1.0a", "1.0.0a", "1.0.0.", "1.0.0-"})
|
||||
void parseInvalid(String input) {
|
||||
testParseInvalid(input);
|
||||
}
|
||||
|
||||
private void testParseInvalid(String input) {
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.parser.parseVersion(input))
|
||||
.withMessage("Invalid API version format");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user