From 32a9b06ec5df5aeb13d8b6cc16b6a1345b9b7464 Mon Sep 17 00:00:00 2001 From: Bud Byrd Date: Wed, 20 May 2020 16:26:59 -0500 Subject: [PATCH] Ensure request content-type is preserved over the entity. (#24) --- gradle.properties | 2 +- .../httprequests/reference/ReferenceHttpClient.java | 2 +- http-requests-documentation/src/docs/changelog.adoc | 6 ++++++ .../httprequests/HttpIntegrationTestSuiteSpec.groovy | 12 ++++++++++++ .../budjb/httprequests/jersey1/JerseyHttpClient.java | 2 +- .../budjb/httprequests/jersey2/JerseyHttpClient.java | 4 +++- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 092b39f..85e0fc1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -version=2.0.6 +version=2.0.7 group=com.budjb githubHttpsUrl=https://github.com/budjb/http-requests githubGitUrl=git@github.com:budjb/http-request.git diff --git a/http-requests-core/src/main/java/com/budjb/httprequests/reference/ReferenceHttpClient.java b/http-requests-core/src/main/java/com/budjb/httprequests/reference/ReferenceHttpClient.java index 34fb569..6240c69 100644 --- a/http-requests-core/src/main/java/com/budjb/httprequests/reference/ReferenceHttpClient.java +++ b/http-requests-core/src/main/java/com/budjb/httprequests/reference/ReferenceHttpClient.java @@ -86,7 +86,7 @@ protected HttpResponse execute(HttpContext context, HttpEntity httpEntity, HttpC } connection.setDoOutput(true); - connection.setRequestProperty("Content-Type", httpEntity.getFullContentType()); + connection.setRequestProperty("Content-Type", request.getHeaders().containsKey("Content-Type") ? request.getHeaders().getFlat("Content-Type") : httpEntity.getFullContentType()); OutputStream outputStream = filterProcessor.filterOutputStream(connection.getOutputStream()); StreamUtils.shovel(httpEntity.getInputStream(), outputStream); diff --git a/http-requests-documentation/src/docs/changelog.adoc b/http-requests-documentation/src/docs/changelog.adoc index 5b2482e..5f69105 100644 --- a/http-requests-documentation/src/docs/changelog.adoc +++ b/http-requests-documentation/src/docs/changelog.adoc @@ -2,6 +2,12 @@ The changelog will be updated with release notes for each release of the library. +2.0.7:: + +* Ensure that a Content-Type specified in the request is preferred over the one provided + in the HTTP entity. This ensures that any Content-Type provided by a converter will not take + precedence over a user-specified one. + 2.0.6:: * Make the build compatible with JDK 8. diff --git a/http-requests-functional-test/src/main/groovy/com/budjb/httprequests/HttpIntegrationTestSuiteSpec.groovy b/http-requests-functional-test/src/main/groovy/com/budjb/httprequests/HttpIntegrationTestSuiteSpec.groovy index c966a62..5144696 100644 --- a/http-requests-functional-test/src/main/groovy/com/budjb/httprequests/HttpIntegrationTestSuiteSpec.groovy +++ b/http-requests-functional-test/src/main/groovy/com/budjb/httprequests/HttpIntegrationTestSuiteSpec.groovy @@ -605,6 +605,18 @@ abstract class HttpIntegrationTestSuiteSpec extends AbstractIntegrationSpec { !((List) response.get('x-foo'))[0] } + def 'A Content-Type contained in the request is preferred over the one in the entity'() { + setup: + HttpRequest request = new HttpRequest("${baseUrl}/echo") + request.setHeader('content-type', 'foo/bar') + + when: + def response = httpClientFactory.createHttpClient().post(request, 'foo') + + then: + response.getHeader('content-type') == 'foo/bar' + } + static class CloseableFilter implements HttpClientFilter, Closeable { boolean closed = false diff --git a/http-requests-jersey1/src/main/java/com/budjb/httprequests/jersey1/JerseyHttpClient.java b/http-requests-jersey1/src/main/java/com/budjb/httprequests/jersey1/JerseyHttpClient.java index 8f4985c..0022456 100644 --- a/http-requests-jersey1/src/main/java/com/budjb/httprequests/jersey1/JerseyHttpClient.java +++ b/http-requests-jersey1/src/main/java/com/budjb/httprequests/jersey1/JerseyHttpClient.java @@ -65,7 +65,7 @@ protected HttpResponse execute(HttpContext context, HttpEntity entity, HttpClien builder = applyHeaders(builder, request.getHeaders()); - if (entity != null && entity.getFullContentType() != null) { + if (!request.getHeaders().containsKey("Content-Type") && entity != null && entity.getFullContentType() != null) { builder = builder.type(entity.getFullContentType()); } diff --git a/http-requests-jersey2/src/main/java/com/budjb/httprequests/jersey2/JerseyHttpClient.java b/http-requests-jersey2/src/main/java/com/budjb/httprequests/jersey2/JerseyHttpClient.java index d4869ea..ae29c12 100644 --- a/http-requests-jersey2/src/main/java/com/budjb/httprequests/jersey2/JerseyHttpClient.java +++ b/http-requests-jersey2/src/main/java/com/budjb/httprequests/jersey2/JerseyHttpClient.java @@ -68,7 +68,9 @@ protected HttpResponse execute(HttpContext context, HttpEntity httpEntity, HttpC Entity entity = null; if (httpEntity != null) { - entity = Entity.entity(httpEntity.getInputStream(), MediaType.valueOf(httpEntity.getFullContentType())); + entity = Entity.entity(httpEntity.getInputStream(), MediaType.valueOf( + request.getHeaders().containsKey("Content-Type") ? request.getHeaders().getFlat("Content-Type") : httpEntity.getFullContentType() + )); } Response clientResponse;