|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
4 | 4 |
|
5 | 5 | import java.net.URI; |
6 | | -import org.junit.jupiter.api.Test; |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
7 | 10 |
|
8 | 11 | class RequestTest { |
9 | 12 |
|
10 | | - @Test |
11 | | - void requestUriJustPath() { |
12 | | - URI uri = new Request("GET", "/foo").getUri(); |
13 | | - assertEquals("/foo", uri.toString()); |
| 13 | + @ParameterizedTest(name = "{0}") |
| 14 | + @MethodSource("getUriCases") |
| 15 | + void getUri(String name, Request request, String expectedUri) { |
| 16 | + URI uri = request.getUri(); |
| 17 | + assertEquals(expectedUri, uri.toString()); |
14 | 18 | } |
15 | 19 |
|
16 | | - @Test |
17 | | - void requestUriWithQuery() { |
18 | | - URI uri = new Request("GET", "/foo").withQueryParam("foo", "bar").getUri(); |
19 | | - assertEquals("/foo?foo=bar", uri.toString()); |
| 20 | + static Stream<Arguments> getUriCases() { |
| 21 | + return Stream.of( |
| 22 | + Arguments.of( |
| 23 | + "just path", |
| 24 | + new Request("GET", "/foo"), |
| 25 | + "/foo"), |
| 26 | + Arguments.of( |
| 27 | + "with query param", |
| 28 | + new Request("GET", "/foo").withQueryParam("foo", "bar"), |
| 29 | + "/foo?foo=bar"), |
| 30 | + Arguments.of( |
| 31 | + "merge query into existing", |
| 32 | + new Request("GET", "/foo?api=1.0").withQueryParam("foo", "bar"), |
| 33 | + "/foo?api=1.0&foo=bar"), |
| 34 | + Arguments.of( |
| 35 | + "forward-slash encoded in query param", |
| 36 | + new Request("GET", "/foo").withQueryParam("foo", "bar/baz"), |
| 37 | + "/foo?foo=bar%2Fbaz"), |
| 38 | + Arguments.of( |
| 39 | + "multiple values for same query param", |
| 40 | + new Request("GET", "/foo") |
| 41 | + .withQueryParam("foo", "bar") |
| 42 | + .withQueryParam("foo", "baz"), |
| 43 | + "/foo?foo=bar&foo=baz"), |
| 44 | + Arguments.of( |
| 45 | + "pre-encoded pipe in path", |
| 46 | + new Request("GET", String.format("/api/2.1/tables/%s", |
| 47 | + Encoding.encodeMultiSegmentPathParameter("name|here"))), |
| 48 | + "/api/2.1/tables/name%7Chere"), |
| 49 | + Arguments.of( |
| 50 | + "pre-encoded path with query params preserves encoding", |
| 51 | + new Request("GET", String.format("/api/2.1/tables/%s", |
| 52 | + Encoding.encodeMultiSegmentPathParameter("name|here"))) |
| 53 | + .withQueryParam("key", "value"), |
| 54 | + "/api/2.1/tables/name%7Chere?key=value"), |
| 55 | + Arguments.of( |
| 56 | + "pre-encoded path with full URL preserves encoding", |
| 57 | + new Request("GET", "/ignored") |
| 58 | + .withUrl("https://host.example.com" + String.format("/api/2.1/tables/%s", |
| 59 | + Encoding.encodeMultiSegmentPathParameter("name|here"))), |
| 60 | + "https://host.example.com/api/2.1/tables/name%7Chere")); |
20 | 61 | } |
21 | 62 |
|
22 | | - @Test |
23 | | - void requestUriMergeQuery() { |
24 | | - URI uri = new Request("GET", "/foo?api=1.0").withQueryParam("foo", "bar").getUri(); |
25 | | - assertEquals("/foo?api=1.0&foo=bar", uri.toString()); |
| 63 | + @ParameterizedTest(name = "{0}") |
| 64 | + @MethodSource("getRequestLineCases") |
| 65 | + void getRequestLine(String name, Request request, String expectedLine) { |
| 66 | + assertEquals(expectedLine, request.getRequestLine()); |
26 | 67 | } |
27 | 68 |
|
28 | | - // Test that forward-slashes are appropriately encoded in query parameters. |
29 | | - @Test |
30 | | - void requestUriWithQueryWithForwardSlash() { |
31 | | - URI uri = new Request("GET", "/foo").withQueryParam("foo", "bar/baz").getUri(); |
32 | | - assertEquals("/foo?foo=bar%2Fbaz", uri.toString()); |
33 | | - } |
34 | | - |
35 | | - // Test that multiple values for the same query parameter are appropriately encoded. |
36 | | - @Test |
37 | | - void requestUriWithQueryWithMultipleValues() { |
38 | | - URI uri = |
39 | | - new Request("GET", "/foo") |
40 | | - .withQueryParam("foo", "bar") |
41 | | - .withQueryParam("foo", "baz") |
42 | | - .getUri(); |
43 | | - assertEquals("/foo?foo=bar&foo=baz", uri.toString()); |
| 69 | + static Stream<Arguments> getRequestLineCases() { |
| 70 | + return Stream.of( |
| 71 | + Arguments.of( |
| 72 | + "simple path", |
| 73 | + new Request("GET", "/foo"), |
| 74 | + "GET /foo"), |
| 75 | + Arguments.of( |
| 76 | + "path with query param", |
| 77 | + new Request("GET", "/foo").withQueryParam("key", "value"), |
| 78 | + "GET /foo?key=value"), |
| 79 | + Arguments.of( |
| 80 | + "encoded path preserves encoding in request line", |
| 81 | + new Request("GET", String.format("/api/2.1/tables/%s", |
| 82 | + Encoding.encodeMultiSegmentPathParameter("name|here"))), |
| 83 | + "GET /api/2.1/tables/name%7Chere"), |
| 84 | + Arguments.of( |
| 85 | + "encoded path with query preserves encoding in request line", |
| 86 | + new Request("GET", String.format("/api/2.1/tables/%s", |
| 87 | + Encoding.encodeMultiSegmentPathParameter("name|here"))) |
| 88 | + .withQueryParam("key", "val/ue"), |
| 89 | + "GET /api/2.1/tables/name%7Chere?key=val%2Fue")); |
44 | 90 | } |
45 | 91 | } |
0 commit comments