|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.retailsvc.http.spec.HttpMethod; |
| 6 | +import com.retailsvc.http.spec.Operation; |
| 7 | +import com.retailsvc.http.spec.PathTemplate; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +class RouterTest { |
| 14 | + private Operation op(String id, HttpMethod m, String path) { |
| 15 | + return new Operation(id, m, PathTemplate.compile(path), Optional.empty(), List.of(), Map.of()); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + void exactPathMatchByMethod() { |
| 20 | + Router r = |
| 21 | + new Router(List.of(op("a", HttpMethod.GET, "/users"), op("b", HttpMethod.POST, "/users"))); |
| 22 | + assertThat(r.match(HttpMethod.GET, "/users").orElseThrow().operation().operationId()) |
| 23 | + .isEqualTo("a"); |
| 24 | + assertThat(r.match(HttpMethod.POST, "/users").orElseThrow().operation().operationId()) |
| 25 | + .isEqualTo("b"); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void templatedPathExtractsParam() { |
| 30 | + Router r = new Router(List.of(op("g", HttpMethod.GET, "/users/{id}"))); |
| 31 | + Router.Match m = r.match(HttpMethod.GET, "/users/42").orElseThrow(); |
| 32 | + assertThat(m.operation().operationId()).isEqualTo("g"); |
| 33 | + assertThat(m.pathParameters()).containsEntry("id", "42"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void unknownPathReturnsEmpty() { |
| 38 | + Router r = new Router(List.of(op("g", HttpMethod.GET, "/users"))); |
| 39 | + assertThat(r.match(HttpMethod.GET, "/orders")).isEmpty(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void allowedMethodsForKnownPath() { |
| 44 | + Router r = |
| 45 | + new Router(List.of(op("a", HttpMethod.GET, "/users"), op("b", HttpMethod.POST, "/users"))); |
| 46 | + assertThat(r.allowedMethods("/users")) |
| 47 | + .containsExactlyInAnyOrder(HttpMethod.GET, HttpMethod.POST); |
| 48 | + assertThat(r.allowedMethods("/missing")).isEmpty(); |
| 49 | + } |
| 50 | +} |
0 commit comments