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

Test coverage for ensuring the MessageBodyWriter is used with null Accept header #1949

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
@@ -0,0 +1,13 @@
package io.quarkus.ts.http.advanced.reactive;

public class CustomHeaderResponse {
private final String content;

public CustomHeaderResponse(String content) {
this.content = content;
}

public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.ts.http.advanced.reactive;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;

@Provider
public class HeadersMessageBodyWriter implements MessageBodyWriter<CustomHeaderResponse> {

@Override
public boolean isWriteable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return CustomHeaderResponse.class.isAssignableFrom(aClass) && MediaType.TEXT_PLAIN_TYPE.isCompatible(mediaType);
}

@Override
public void writeTo(CustomHeaderResponse customHeaderResponse, Class<?> aClass, Type type, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream)
throws IOException, WebApplicationException {
final String content = "Headers response: " + customHeaderResponse.getContent();
outputStream.write(content.getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import io.smallrye.mutiny.Uni;
Expand All @@ -28,4 +30,11 @@ public Uni<Response> headersOverride() {
return Uni.createFrom().item(response);
}

@GET
@Path("/no-accept")
@Produces(MediaType.TEXT_PLAIN)
jcarranzan marked this conversation as resolved.
Show resolved Hide resolved
public Uni<Response> noAcceptHeaders() {
return Uni.createFrom().item(Response.ok(new CustomHeaderResponse("ok headers")).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;
import io.restassured.http.Header;
import io.restassured.response.ValidatableResponse;

@QuarkusScenario
public class HeadersIT {

@QuarkusApplication(classes = { PathSpecificHeadersResource.class,
HeadersMessageBodyWriter.class,
CustomHeaderResponse.class,
HeadersResource.class }, properties = "headers.properties")
static RestService app = new RestService();

Expand Down Expand Up @@ -100,6 +103,19 @@ private ValidatableResponse whenGet(String path) {
.body(is("ok"));
}

@Test
@Tag("https://github.com/quarkusio/quarkus/pull/41411")
void testWithNoAcceptHeader() {
Header header = new Header("Accept", null);
given()
.when()
.header(header)
.get("/headers/no-accept")
.then()
.statusCode(200)
.body(is("Headers response: ok headers"));
}

/**
* Cache-Control header may be present multiple times in the response, e.g. in an OpenShift deployment. That is why we need
* to look for a specific value among all headers of the same name, and not just match the last one of them, which is what
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.ts.http.advanced;

public class CustomHeaderResponse {

private final String content;

public CustomHeaderResponse(String content) {
this.content = content;
}

public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.ts.http.advanced;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;

@Provider
public class HeadersMessageBodyWriter implements MessageBodyWriter<CustomHeaderResponse> {

@Override
public boolean isWriteable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return CustomHeaderResponse.class.isAssignableFrom(aClass) && MediaType.TEXT_PLAIN_TYPE.isCompatible(mediaType);
}

@Override
public void writeTo(CustomHeaderResponse customHeaderResponse, Class<?> aClass, Type type, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream)
throws IOException, WebApplicationException {
final String content = "Headers response: " + customHeaderResponse.getContent();
outputStream.write(content.getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/headers")
Expand All @@ -25,4 +27,10 @@ public Response headersOverride() {
return Response.ok("ok").header("foo", "abc").build();
}

@GET
@Path("/no-accept")
@Produces(MediaType.TEXT_PLAIN)
public Response noAcceptHeaders() {
return Response.ok(new CustomHeaderResponse("ok headers")).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;
import io.restassured.http.Header;
import io.restassured.response.ValidatableResponse;

@QuarkusScenario
public class HeadersIT {

@QuarkusApplication(classes = { PathSpecificHeadersResource.class,
HeadersMessageBodyWriter.class,
CustomHeaderResponse.class,
HeadersResource.class }, properties = "headers.properties")
static RestService app = new RestService();

Expand Down Expand Up @@ -93,6 +96,21 @@ void testPathSpecificHeaderRulesOrder() {
cacheControlMatches(response, "max-age=1");
}

/**
* Coverage for https://github.com/quarkusio/quarkus/issues/41354 in RESTEasy classic
*/
@Test
void testWithNoAcceptHeader() {
jedla97 marked this conversation as resolved.
Show resolved Hide resolved
Header header = new Header("Accept", null);
given()
.when()
.header(header)
.get("/headers/no-accept")
.then()
.statusCode(200)
.body(is("Headers response: ok headers"));
}

private ValidatableResponse whenGet(String path) {
return given()
.get(path)
Expand Down