|
| 1 | +package com.lithic.api.core.http |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import org.junit.jupiter.params.ParameterizedTest |
| 5 | +import org.junit.jupiter.params.provider.EnumSource |
| 6 | + |
| 7 | +internal class HttpRequestTest { |
| 8 | + |
| 9 | + enum class UrlTestCase(val request: HttpRequest, val expectedUrl: String) { |
| 10 | + BASE_URL_ONLY( |
| 11 | + HttpRequest.builder().method(HttpMethod.GET).baseUrl("https://api.example.com").build(), |
| 12 | + expectedUrl = "https://api.example.com", |
| 13 | + ), |
| 14 | + BASE_URL_WITH_TRAILING_SLASH( |
| 15 | + HttpRequest.builder() |
| 16 | + .method(HttpMethod.GET) |
| 17 | + .baseUrl("https://api.example.com/") |
| 18 | + .build(), |
| 19 | + expectedUrl = "https://api.example.com/", |
| 20 | + ), |
| 21 | + SINGLE_PATH_SEGMENT( |
| 22 | + HttpRequest.builder() |
| 23 | + .method(HttpMethod.GET) |
| 24 | + .baseUrl("https://api.example.com") |
| 25 | + .addPathSegment("users") |
| 26 | + .build(), |
| 27 | + expectedUrl = "https://api.example.com/users", |
| 28 | + ), |
| 29 | + MULTIPLE_PATH_SEGMENTS( |
| 30 | + HttpRequest.builder() |
| 31 | + .method(HttpMethod.GET) |
| 32 | + .baseUrl("https://api.example.com") |
| 33 | + .addPathSegments("users", "123", "profile") |
| 34 | + .build(), |
| 35 | + expectedUrl = "https://api.example.com/users/123/profile", |
| 36 | + ), |
| 37 | + PATH_SEGMENT_WITH_SPECIAL_CHARS( |
| 38 | + HttpRequest.builder() |
| 39 | + .method(HttpMethod.GET) |
| 40 | + .baseUrl("https://api.example.com") |
| 41 | + .addPathSegment("user name") |
| 42 | + .build(), |
| 43 | + expectedUrl = "https://api.example.com/user+name", |
| 44 | + ), |
| 45 | + SINGLE_QUERY_PARAM( |
| 46 | + HttpRequest.builder() |
| 47 | + .method(HttpMethod.GET) |
| 48 | + .baseUrl("https://api.example.com") |
| 49 | + .addPathSegment("users") |
| 50 | + .putQueryParam("limit", "10") |
| 51 | + .build(), |
| 52 | + expectedUrl = "https://api.example.com/users?limit=10", |
| 53 | + ), |
| 54 | + MULTIPLE_QUERY_PARAMS( |
| 55 | + HttpRequest.builder() |
| 56 | + .method(HttpMethod.GET) |
| 57 | + .baseUrl("https://api.example.com") |
| 58 | + .addPathSegment("users") |
| 59 | + .putQueryParam("limit", "10") |
| 60 | + .putQueryParam("offset", "20") |
| 61 | + .build(), |
| 62 | + expectedUrl = "https://api.example.com/users?limit=10&offset=20", |
| 63 | + ), |
| 64 | + QUERY_PARAM_WITH_SPECIAL_CHARS( |
| 65 | + HttpRequest.builder() |
| 66 | + .method(HttpMethod.GET) |
| 67 | + .baseUrl("https://api.example.com") |
| 68 | + .addPathSegment("search") |
| 69 | + .putQueryParam("q", "hello world") |
| 70 | + .build(), |
| 71 | + expectedUrl = "https://api.example.com/search?q=hello+world", |
| 72 | + ), |
| 73 | + MULTIPLE_VALUES_SAME_PARAM( |
| 74 | + HttpRequest.builder() |
| 75 | + .method(HttpMethod.GET) |
| 76 | + .baseUrl("https://api.example.com") |
| 77 | + .addPathSegment("users") |
| 78 | + .putQueryParams("tags", listOf("admin", "user")) |
| 79 | + .build(), |
| 80 | + expectedUrl = "https://api.example.com/users?tags=admin&tags=user", |
| 81 | + ), |
| 82 | + BASE_URL_WITH_TRAILING_SLASH_AND_PATH( |
| 83 | + HttpRequest.builder() |
| 84 | + .method(HttpMethod.GET) |
| 85 | + .baseUrl("https://api.example.com/") |
| 86 | + .addPathSegment("users") |
| 87 | + .build(), |
| 88 | + expectedUrl = "https://api.example.com/users", |
| 89 | + ), |
| 90 | + COMPLEX_URL( |
| 91 | + HttpRequest.builder() |
| 92 | + .method(HttpMethod.POST) |
| 93 | + .baseUrl("https://api.example.com") |
| 94 | + .addPathSegments("v1", "users", "123") |
| 95 | + .putQueryParams("include", listOf("profile", "settings")) |
| 96 | + .putQueryParam("format", "json") |
| 97 | + .build(), |
| 98 | + expectedUrl = |
| 99 | + "https://api.example.com/v1/users/123?include=profile&include=settings&format=json", |
| 100 | + ), |
| 101 | + } |
| 102 | + |
| 103 | + @ParameterizedTest |
| 104 | + @EnumSource |
| 105 | + fun url(testCase: UrlTestCase) { |
| 106 | + val actualUrl = testCase.request.url() |
| 107 | + |
| 108 | + assertThat(actualUrl).isEqualTo(testCase.expectedUrl) |
| 109 | + } |
| 110 | +} |
0 commit comments