Skip to content

Commit

Permalink
[RESTEASY-2874] Web Target Proxy not overriding content type header
Browse files Browse the repository at this point in the history
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
petrberan authored and jamezp committed Dec 12, 2022
1 parent a758a6d commit a581790
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 2 deletions.
Expand Up @@ -29,6 +29,7 @@ public class ClientRequestHeaders
{
protected CaseInsensitiveMap<Object> headers = new CaseInsensitiveMap<Object>();
protected ClientConfiguration configuration;
private boolean userForcedContentType = false;


public ClientRequestHeaders(final ClientConfiguration configuration)
Expand Down Expand Up @@ -75,7 +76,10 @@ public void setMediaType(MediaType mediaType)
headers.remove(HttpHeaders.CONTENT_TYPE);
return;
}
headers.putSingle(HttpHeaders.CONTENT_TYPE, mediaType);
if (!userForcedContentType)
{
headers.putSingle(HttpHeaders.CONTENT_TYPE, mediaType);
}
}

public void acceptLanguage(Locale... locales)
Expand Down Expand Up @@ -169,6 +173,11 @@ public void header(String name, Object value)
if (name.equalsIgnoreCase(HttpHeaders.ACCEPT)) accept(configuration.toHeaderString(value));
else if (name.equalsIgnoreCase(HttpHeaders.ACCEPT_ENCODING)) acceptEncoding(configuration.toHeaderString(value));
else if (name.equalsIgnoreCase(HttpHeaders.ACCEPT_LANGUAGE)) acceptLanguage(configuration.toHeaderString(value));
else if (name.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE))
{
headers.putSingle(HttpHeaders.CONTENT_TYPE, value);
userForcedContentType = true;
}
else headers.add(name, value);
}

Expand Down
Expand Up @@ -74,7 +74,7 @@ public void testCount() throws Exception {
@Test
public void testNullJaxb() throws Exception {
Response response = client.target(generateURL("/my/null")).request().header("Content-Type", "application/xml").post(Entity.text(""));
Assert.assertEquals(HttpResponseCodes.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
response.close();
}

Expand Down
@@ -0,0 +1,148 @@
package org.jboss.resteasy.test.resource.param;


import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.test.resource.param.resource.UserDefinedHeaderParamResource;

import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.ClientBuilder;

/**
* @tpSubChapter Resteasy-client
* @tpChapter Integration tests
* @tpTestCaseDetails Regression test for RESTEASY-2874
* @tpSince RESTEasy 4.7.0
*/
@RunWith(Arquillian.class)
@RunAsClient
public class UserDefinedHeaderParamTest {

ResteasyClient client;

private static final String HEADER_PARAM = "image/jpeg";

@Path("")
public interface UserHeaderParamInterface {

@POST
@Path("/header")
@Consumes({"application/json", "text/plain", "image/jpeg"})
String sendHeaderFirst(@HeaderParam("Content-Type") String contentType, String text);

@POST
@Path("/header")
@Consumes({"application/json", "text/plain", "image/jpeg"})
String sendTextFirst(String text, @HeaderParam("Content-Type") String contentType);

@POST
@Path("/header")
@Consumes({"application/json", "text/plain", "image/jpeg"})
String sendDefaultType(String text);

@POST
@Path("/header")
@Consumes({"application/json", "text/plain", "image/jpeg"})
String sendMultipleTypes(String text, @HeaderParam("Content-Type") String contentType,
@HeaderParam("Content-Type") String secondContentType,
@HeaderParam("Content-Type") String thirdContentType);
}

@Before
public void setUp() {
client = (ResteasyClient) ClientBuilder.newClient();
}

@After
public void after() throws Exception {
client.close();
}

@Deployment
public static Archive<?> deploy() throws Exception {
WebArchive war = TestUtil.prepareArchive(UserDefinedHeaderParamTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, UserDefinedHeaderParamResource.class);
}

private String generateURL() {
return PortProviderUtil.generateBaseUrl(UserDefinedHeaderParamTest.class.getSimpleName());
}

/**
* @tpTestDetails Checks whether the correct content type header is returned in case user specified header param
* as a first argument in proxy method.
* @tpPassCrit Expected header is returned
* @tpSince RESTEasy 4.7.0
*/
@Test
public void testHeaderParamFirst(){
ResteasyWebTarget target = client.target(generateURL());
UserHeaderParamInterface proxy = target.proxy(UserDefinedHeaderParamTest.UserHeaderParamInterface.class);

String response = proxy.sendHeaderFirst(HEADER_PARAM, "text");
Assert.assertEquals("Incorrect header param returned,", HEADER_PARAM, response);
}

/**
* @tpTestDetails Checks whether the correct content type header is returned in case user specified header param
* as a second argument in proxy method.
* @tpPassCrit Expected header is returned
* @tpSince RESTEasy 4.7.0
*/
@Test
public void testTextFirst(){
ResteasyWebTarget target = client.target(generateURL());
UserHeaderParamInterface proxy = target.proxy(UserDefinedHeaderParamTest.UserHeaderParamInterface.class);

String response = proxy.sendTextFirst("text", HEADER_PARAM);
Assert.assertEquals("Incorrect header param returned,", HEADER_PARAM, response);
}

/**
* @tpTestDetails Checks whether the correct content type header is returned in case user didn't specify header
* param in proxy method. This should be the first content type in case of multiple @Consumes values.
* @tpPassCrit Expected header is returned
* @tpSince RESTEasy 4.7.0
*/
@Test
public void testDefaultHeaderParam(){
ResteasyWebTarget target = client.target(generateURL());
UserHeaderParamInterface proxy = target.proxy(UserDefinedHeaderParamTest.UserHeaderParamInterface.class);

String response = proxy.sendDefaultType("text");
Assert.assertEquals("Incorrect header param returned,", "application/json", response);
}

/**
* @tpTestDetails Checks whether the correct content type header is returned in case user specified header
* param in proxy method with multiple other header params. This should be the last content type in arguments.
* @tpPassCrit Expected header is returned
* @tpSince RESTEasy 4.7.0
*/
@Test
public void testMultipleHeaderParams(){
ResteasyWebTarget target = client.target(generateURL());
UserHeaderParamInterface proxy = target.proxy(UserDefinedHeaderParamTest.UserHeaderParamInterface.class);

String response = proxy.sendMultipleTypes("text", "text/plain","application/json","image/jpeg");
Assert.assertEquals("Incorrect header param returned,", HEADER_PARAM, response);
}


}
@@ -0,0 +1,41 @@
package org.jboss.resteasy.test.resource.param.resource;

import org.jboss.resteasy.test.resource.param.UserDefinedHeaderParamTest;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;

public class UserDefinedHeaderParamResource implements UserDefinedHeaderParamTest.UserHeaderParamInterface {

@Context
private HttpHeaders httpHeaders;

@GET
@Path("/header")
public String sendHeaderFirst(@HeaderParam("Content-Type") String contentType, String text) {
return httpHeaders.getHeaderString("Content-Type");
}

@GET
@Path("/header")
public String sendTextFirst(String text, @HeaderParam("Content-Type") String contentType) {
return httpHeaders.getHeaderString("Content-Type");
}

@GET
@Path("/header")
public String sendDefaultType(String text) {
return httpHeaders.getHeaderString("Content-Type");
}

@GET
@Path("/header")
public String sendMultipleTypes(String text, @HeaderParam("Content-Type") String contentType,
@HeaderParam("Content-Type") String secondContentType,
@HeaderParam("Content-Type") String thirdContentType) {
return httpHeaders.getHeaderString("Content-Type");
}
}

0 comments on commit a581790

Please sign in to comment.