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

Fix #239: RestClient content type per request #240

Merged
merged 1 commit into from
Dec 4, 2023
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 @@ -363,7 +363,7 @@ public <T> ResponseEntity<T> post(String path, Object request, MultiValueMap<Str
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -398,7 +398,7 @@ public <T> void postNonBlocking(String path, Object request, MultiValueMap<Strin
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -444,7 +444,7 @@ public <T> ResponseEntity<T> put(String path, Object request, MultiValueMap<Stri
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -472,7 +472,7 @@ public <T> void putNonBlocking(String path, Object request, MultiValueMap<String
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -586,7 +586,7 @@ public <T> ResponseEntity<T> patch(String path, Object request, MultiValueMap<St
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -614,7 +614,7 @@ public <T> void patchNonBlocking(String path, Object request, MultiValueMap<Stri
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -770,6 +770,15 @@ private <T> Mono<ResponseEntity<T>> handleResponse(ClientResponse response, Para
});
}

private static MediaType resolveContentType(final RestClientConfiguration config, final MultiValueMap<String, String> headers) {
if (headers != null && headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
final MediaType contentType = MediaType.valueOf(headers.getFirst(HttpHeaders.CONTENT_TYPE));
logger.debug("Overriding content type {} from config by {} from the given headers", config.getContentType(), contentType);
return contentType;
}
return config.getContentType();
}

/**
* In case base URL is specified, append the path to complete the URL. Otherwise use the path as the URL specification.
* @param uriSpec Request headers URI specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,23 @@ void testPostWithMultipartData() throws RestClientException {
assertEquals(requestData, responseEntity.getBody().getResponseObject().getResponse());
}

@Test
void testPostOctetStream() throws Exception {
final byte[] request = {1, 2};

final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

final ResponseEntity<ObjectResponse<TestResponse>> responseEntity =
restClient.post("/octet-stream", request, null, headers, new ParameterizedTypeReference<>(){});

assertNotNull(responseEntity);
assertNotNull(responseEntity.getBody());
assertNotNull(responseEntity.getBody().getResponseObject());
assertEquals("OK", responseEntity.getBody().getStatus());
assertEquals("length: 2", responseEntity.getBody().getResponseObject().getResponse());
}

@Test
void testPostWithLargeServerResponse() {
final Logger defaultRestClientLogger = (Logger) LoggerFactory.getLogger(DefaultRestClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import io.getlime.core.rest.model.base.request.ObjectRequest;
import io.getlime.core.rest.model.base.response.ObjectResponse;
import io.getlime.core.rest.model.base.response.Response;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -85,6 +85,12 @@ public ObjectResponse<TestResponse> testPostWithMultipartRequestAndResponse(@Req
return new ObjectResponse<>(testResponse);
}

@PostMapping(value = "/octet-stream", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ObjectResponse<TestResponse> testPostOctetStream(@RequestBody byte[] request) {
final TestResponse testResponse = new TestResponse("length: " + request.length);
return new ObjectResponse<>(testResponse);
}

@PostMapping("/object-response-large")
public ObjectResponse<TestResponse> testPostWithLargeServerResponse() {
TestResponse testResponse = new TestResponse(Arrays.toString(new byte[10 * 1024 * 1024]));
Expand Down