-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor HTTP Method from Enum to Class
This commit refactors HttpMethod from a Java enum into a class. The underlying reason being that HTTP methods are not enumerable, but instead an open range and not limited to the predefined values in the specifications. Closes gh-27697
- Loading branch information
Showing
2 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
spring-web/src/test/java/org/springframework/http/HttpMethodTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2002-2021 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.http; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Arjen Poutsma | ||
*/ | ||
class HttpMethodTests { | ||
|
||
@Test | ||
public void comparison() { | ||
HttpMethod method1 = HttpMethod.valueOf("FOO"); | ||
HttpMethod method2 = HttpMethod.valueOf("FOO"); | ||
HttpMethod method3 = HttpMethod.valueOf("BAR"); | ||
|
||
assertThat(method1).isEqualTo(method2); | ||
assertThat(method1).isNotEqualTo(method3); | ||
|
||
assertThat(method1.hashCode()).isEqualTo(method2.hashCode()); | ||
|
||
assertThat(method1.compareTo(method2)).isEqualTo(0); | ||
assertThat(method1.compareTo(method3)).isNotEqualTo(0); | ||
} | ||
|
||
@Test | ||
void values() { | ||
HttpMethod[] values = HttpMethod.values(); | ||
assertThat(values).containsExactly(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST, HttpMethod.PUT, | ||
HttpMethod.PATCH, HttpMethod.DELETE, HttpMethod.OPTIONS, HttpMethod.TRACE); | ||
|
||
// check defensive copy | ||
values[0] = HttpMethod.POST; | ||
assertThat(HttpMethod.values()).containsExactly(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST, HttpMethod.PUT, | ||
HttpMethod.PATCH, HttpMethod.DELETE, HttpMethod.OPTIONS, HttpMethod.TRACE); | ||
} | ||
|
||
@Test | ||
void valueOf() { | ||
HttpMethod get = HttpMethod.valueOf("GET"); | ||
assertThat(get).isSameAs(HttpMethod.GET); | ||
|
||
HttpMethod foo = HttpMethod.valueOf("FOO"); | ||
HttpMethod other = HttpMethod.valueOf("FOO"); | ||
assertThat(foo).isEqualTo(other); | ||
} | ||
|
||
@Test | ||
void name() { | ||
HttpMethod method = HttpMethod.valueOf("FOO"); | ||
assertThat(method.name()).isEqualTo("FOO"); | ||
} | ||
|
||
@Test | ||
void matches() { | ||
assertThat(HttpMethod.GET.matches("GET")).isTrue(); | ||
assertThat(HttpMethod.GET.matches("FOO")).isFalse(); | ||
} | ||
} |
the if else can be simplified by the use getOrDefault of the Map: return mappings.getOrDefault(method,new HttpMethod(method));
Or the use of if else is used for code visibility.
Thanks :)