|
16 | 16 |
|
17 | 17 | package com.palantir.conjure.java.client.jaxrs;
|
18 | 18 |
|
| 19 | +import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy; |
19 | 20 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 21 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
| 22 | +import static org.assertj.core.api.InstanceOfAssertFactories.collection; |
21 | 23 | import static org.mockito.ArgumentMatchers.any;
|
22 | 24 | import static org.mockito.Mockito.mock;
|
| 25 | +import static org.mockito.Mockito.never; |
23 | 26 | import static org.mockito.Mockito.times;
|
24 | 27 | import static org.mockito.Mockito.verify;
|
25 | 28 | import static org.mockito.Mockito.when;
|
|
35 | 38 | import com.palantir.dialogue.Request;
|
36 | 39 | import com.palantir.dialogue.Response;
|
37 | 40 | import com.palantir.dialogue.UrlBuilder;
|
| 41 | +import com.palantir.logsafe.SafeArg; |
| 42 | +import com.palantir.logsafe.UnsafeArg; |
| 43 | +import com.palantir.logsafe.exceptions.SafeIllegalStateException; |
38 | 44 | import jakarta.ws.rs.Consumes;
|
39 | 45 | import jakarta.ws.rs.GET;
|
40 | 46 | import jakarta.ws.rs.POST;
|
@@ -95,6 +101,56 @@ public void testQueryParameterCollection() {
|
95 | 101 | verify(urlBuilder).queryParam("query", "a+b");
|
96 | 102 | }
|
97 | 103 |
|
| 104 | + @Test |
| 105 | + public void testQueryParameters() { |
| 106 | + Channel channel = stubNoContentResponseChannel(); |
| 107 | + StubService service = JaxRsClient.create(StubService.class, channel, runtime); |
| 108 | + service.collectionOfQueryParams(List.of("", "=", "test=value")); |
| 109 | + |
| 110 | + ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class); |
| 111 | + ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); |
| 112 | + verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture()); |
| 113 | + assertThat(requestCaptor.getValue().pathParameters().asMap()) |
| 114 | + .hasSize(1) |
| 115 | + .extractingByKey("request-url") |
| 116 | + .asInstanceOf(collection(String.class)) |
| 117 | + .containsExactly("dialogue://feign/foo/params?query=&query=%3D&query=test%3Dvalue"); |
| 118 | + Endpoint endpoint = endpointCaptor.getValue(); |
| 119 | + UrlBuilder urlBuilder = mock(UrlBuilder.class); |
| 120 | + endpoint.renderPath( |
| 121 | + ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?query=&query==&query=test=value"), |
| 122 | + urlBuilder); |
| 123 | + verify(urlBuilder).queryParam("query", ""); |
| 124 | + verify(urlBuilder).queryParam("query", "="); |
| 125 | + verify(urlBuilder).queryParam("query", "test=value"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testInvalidQueryParameters() { |
| 130 | + Channel channel = stubNoContentResponseChannel(); |
| 131 | + StubService service = JaxRsClient.create(StubService.class, channel, runtime); |
| 132 | + service.collectionOfQueryParams(List.of()); |
| 133 | + |
| 134 | + ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class); |
| 135 | + ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); |
| 136 | + verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture()); |
| 137 | + Endpoint endpoint = endpointCaptor.getValue(); |
| 138 | + UrlBuilder urlBuilder = mock(UrlBuilder.class); |
| 139 | + assertThatLoggableExceptionThrownBy(() -> endpoint.renderPath( |
| 140 | + ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?="), urlBuilder)) |
| 141 | + .isInstanceOf(SafeIllegalStateException.class) |
| 142 | + .hasLogMessage("Expected two parameters") |
| 143 | + .args() |
| 144 | + .containsExactlyInAnyOrder(SafeArg.of("parameters", 0), UnsafeArg.of("values", "=")); |
| 145 | + assertThatLoggableExceptionThrownBy(() -> endpoint.renderPath( |
| 146 | + ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?query"), urlBuilder)) |
| 147 | + .isInstanceOf(SafeIllegalStateException.class) |
| 148 | + .hasLogMessage("Expected two parameters") |
| 149 | + .args() |
| 150 | + .containsExactlyInAnyOrder(SafeArg.of("parameters", 1), UnsafeArg.of("values", "query")); |
| 151 | + verify(urlBuilder, never()).queryParam(any(), any()); |
| 152 | + } |
| 153 | + |
98 | 154 | @Test
|
99 | 155 | public void testPostWithBody() {
|
100 | 156 | Channel channel = stubNoContentResponseChannel();
|
|
0 commit comments