Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encode queryparams special characters #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public static PostgrestRestTemplate of(RestTemplate restTemplate, String baseUrl
@Override
public <T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
URI uri = getUri(resource, params);
ResponseEntity<List<T>> response = restTemplate.exchange(
getUri(resource, params), HttpMethod.GET,
uri, HttpMethod.GET,
new HttpEntity<>(null, toHeaders(headers)), listRef(clazz));

// Retrieve result headers
Expand Down Expand Up @@ -140,12 +141,13 @@ private static HttpHeaders toHeaders(Map<String, List<String>> headers) {
* @param params params
* @return URI
*/
private URI getUri(String resource, Map<String, List<String>> params) {
protected URI getUri(String resource, Map<String, List<String>> params) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(baseUrl).path(resource);
if (Objects.nonNull(params)) {
uriBuilder = uriBuilder.queryParams(toMultiMap(params));
}
return uriBuilder.build().encode().toUri();
URI uri = uriBuilder.build().encode().toUri();
return URI.create(uri.toASCIIString().replace("+", "%2B"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.MediaType;
import org.mockserver.verify.Verification;
import org.mockserver.verify.VerificationTimes;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import shaded_package.org.apache.commons.io.IOUtils;

import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -32,13 +36,14 @@ class PostgrestRestTemplateRepositoryTest {

private PostgrestRepository<Post> repository;
private PostgrestRpcClient rpcClient;
private PostgrestRestTemplate postgrestRestTemplate;

@BeforeEach
void beforeEach(MockServerClient client) {
client.reset();
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault()));
PostgrestRestTemplate postgrestRestTemplate = PostgrestRestTemplate.of(restTemplate, "http://localhost:8007");
postgrestRestTemplate = PostgrestRestTemplate.of(restTemplate, "http://localhost:8007");
repository = new PostRepository(postgrestRestTemplate);
rpcClient = new PostgrestRpcClient(postgrestRestTemplate);
}
Expand Down Expand Up @@ -212,6 +217,24 @@ void shouldCallRpcResultIsList(MockServerClient client) {
assertNotNull(result);
}



@Test
void shouldEncodePlusChars(MockServerClient client) {
client.when(HttpRequest.request().withPath("/posts")
.withQueryStringParameter("id", "eq.Romeo + Juliette")
.withQueryStringParameter("select", "*,authors(*)"))
.respond(jsonFileResponse("posts.json").withHeader("Content-Range", "0-6/300"));
Page<Post> search = repository.search(Criteria.byId("Romeo + Juliette"), Pageable.ofSize(6));
assertNotNull(search);

// assert uri
URI uri = postgrestRestTemplate.getUri("/posts", Map.of("id",
List.of("eq.Romeo + Juliette")));

assertEquals("http://localhost:8007/posts?id=eq.Romeo%20%2B%20Juliette", uri.toString());
}

private HttpResponse jsonResponse(String content) {
return HttpResponse.response().withContentType(MediaType.APPLICATION_JSON)
.withBody(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.util.UriBuilder;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -57,11 +59,7 @@ public static PostgrestWebClient of(WebClient webClient) {
@Override
public <T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = webClient.get().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
}).headers(httpHeaders ->
ResponseEntity<List<T>> response = webClient.get().uri(uriBuilder -> getUri(resource, params, uriBuilder)).headers(httpHeaders ->
safeAdd(headers, httpHeaders)
)
.retrieve()
Expand All @@ -77,13 +75,11 @@ public <T> RangeResponse<T> search(String resource, Map<String, List<String>> pa
}).orElse(new RangeResponse<>(List.of(), null));
}



@Override
public List<CountItem> count(String resource, Map<String, List<String>> params) {
ResponseEntity<List<CountItem>> response = webClient.get().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
})
ResponseEntity<List<CountItem>> response = webClient.get().uri(uriBuilder -> getUri(resource, params, uriBuilder))
.retrieve()
.toEntity(listRef(CountItem.class))
.block();
Expand All @@ -92,11 +88,7 @@ public List<CountItem> count(String resource, Map<String, List<String>> params)

@Override
public <V> V rpc(String rpcName, Map<String, List<String>> params, Object body, Type clazz) {
WebClient.RequestBodySpec request = webClient.post().uri(uriBuilder -> {
uriBuilder.path(rpcName);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
});
WebClient.RequestBodySpec request = webClient.post().uri(uriBuilder -> getUri(rpcName, params, uriBuilder));
Optional.ofNullable(body).ifPresent(request::bodyValue);
Object result = request
.retrieve()
Expand All @@ -113,11 +105,7 @@ public <V> V rpc(String rpcName, Map<String, List<String>> params, Object body,

@Override
public <T> BulkResponse<T> post(String resource, Map<String, List<String>> params, Object value, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = webClient.post().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
}).headers(httpHeaders -> safeAdd(headers, httpHeaders))
ResponseEntity<List<T>> response = webClient.post().uri(uriBuilder -> getUri(resource, params, uriBuilder)).headers(httpHeaders -> safeAdd(headers, httpHeaders))
.bodyValue(value)
.retrieve()
.toEntity(listRef(clazz))
Expand All @@ -128,11 +116,7 @@ public <T> BulkResponse<T> post(String resource, Map<String, List<String>> param

@Override
public <T> BulkResponse<T> patch(String resource, Map<String, List<String>> params, Object value, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = webClient.patch().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
})
ResponseEntity<List<T>> response = webClient.patch().uri(uriBuilder -> getUri(resource, params, uriBuilder))
.bodyValue(value)
.headers(httpHeaders -> safeAdd(headers, httpHeaders))
.retrieve()
Expand All @@ -142,18 +126,20 @@ public <T> BulkResponse<T> patch(String resource, Map<String, List<String>> para

@Override
public <T> BulkResponse<T> delete(String resource, Map<String, List<String>> params, Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = webClient.delete().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return uriBuilder.build();
})
ResponseEntity<List<T>> response = webClient.delete().uri(uriBuilder -> getUri(resource, params, uriBuilder))
.headers(httpHeaders -> safeAdd(headers, httpHeaders))
.retrieve()
.toEntity(listRef(clazz)).block();
return toBulkResponse(response);
}


protected URI getUri(String resource, Map<String, List<String>> params, UriBuilder uriBuilder) {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
return URI.create(uriBuilder.build().toASCIIString().replace("+", "%2B"));
}

/**
* Convert map to MultiValueMap
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.Pageable;
import fr.ouestfrance.querydsl.postgrest.model.Range;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.TypeUtils;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.junit.jupiter.MockServerSettings;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import shaded_package.org.apache.commons.io.IOUtils;
import org.springframework.web.util.UriBuilder;
import org.springframework.web.util.UriComponentsBuilder;

import java.nio.charset.Charset;
import java.net.URI;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static fr.ouestfrance.querydsl.postgrest.TestUtils.jsonFileResponse;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -33,10 +31,10 @@
@Slf4j
class PostgrestWebClientRepositoryTest {

private final PostgrestWebClient client = PostgrestWebClient.of(WebClient.builder()
private final PostgrestWebClient postgrestClient = PostgrestWebClient.of(WebClient.builder()
.baseUrl("http://localhost:8007/")
.build());
private final PostgrestRepository<Post> repository = new PostRepository(client);
private final PostgrestRepository<Post> repository = new PostRepository(postgrestClient);


@Test
Expand Down Expand Up @@ -211,4 +209,20 @@ void shouldValidateRangeRequest(ClientAndServer client) {
Page<Post> oldWaySearch = repository.search(oldWayRangeRequest, Pageable.ofSize(6));
assertNotNull(oldWaySearch);
}

@Test
void shouldEncodePlusChars(MockServerClient client) {
client.when(HttpRequest.request().withPath("/posts")
.withQueryStringParameter("id", "eq.Romeo + Juliette")
.withQueryStringParameter("select", "*,authors(*)"))
.respond(jsonFileResponse("posts.json").withHeader("Content-Range", "0-6/300"));
Page<Post> search = repository.search(Criteria.byId("Romeo + Juliette"), Pageable.ofSize(6));
assertNotNull(search);

// assert uri
URI uri = postgrestClient.getUri("/posts", Map.of("id",
List.of("eq.Romeo + Juliette")), UriComponentsBuilder.newInstance().host("localhost").port(8007).scheme("http"));

assertEquals("http://localhost:8007/posts?id=eq.Romeo%20%2B%20Juliette", uri.toString());
}
}
Loading