Skip to content

Commit

Permalink
getHeaderString was returning an empty string for missing headers (#341)
Browse files Browse the repository at this point in the history
* getHeaderString was returning an empty string for missing headers

This is not as per the definition.
This change makes it return null when it it missing.

https://jakarta.ee/specifications/platform/8/apidocs/javax/ws/rs/core/httpheaders#getHeaderString-java.lang.String-

* Defend against null, add happy path test
  • Loading branch information
timyates authored Aug 31, 2023
1 parent 7ab2eaa commit 7bbd0ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.jaxrs.runtime.ext.bind;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.jaxrs.runtime.core.Weighted;
import io.micronaut.jaxrs.runtime.ext.impl.CookieHeaderDelegate;
import jakarta.ws.rs.core.Cookie;
Expand Down Expand Up @@ -58,7 +59,12 @@ public List<String> getRequestHeader(String name) {

@Override
public String getHeaderString(String name) {
return String.join(",", httpHeaders.getAll(name));
List<String> all = httpHeaders.getAll(name);
if (CollectionUtils.isEmpty(all)) {
return null;
} else {
return String.join(",", all);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.CacheControl;
import jakarta.ws.rs.core.Cookie;
Expand Down Expand Up @@ -81,4 +82,14 @@ public Response headers(HttpHeaders httpHeaders) {
});
return ok.build();
}

@GET
@Path("/header")
@Produces("text/plain")
public Response header(HttpHeaders httpHeaders) {
final Response.ResponseBuilder ok = Response.ok();
String headerString = httpHeaders.getHeaderString("test-value");
ok.entity(headerString == null ? "null" : headerString);
return ok.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@MicronautTest
Expand Down Expand Up @@ -188,4 +189,24 @@ void testAcceptLanguageHeader() {
acceptableLanguages.get(0).getLanguage()
);
}

@Test
void testUnknownHeaderValueIsNull() {
final MutableHttpRequest<Object> request = HttpRequest.GET("/headers/header");
request.header(HttpHeaders.ACCEPT_LANGUAGE, "fr;q=0.7,en;q=0.9");

final HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
assertEquals("null", response.body());
}

@Test
void testKnownHeaderValueString() {
final MutableHttpRequest<Object> request = HttpRequest.GET("/headers/header");
request.header(HttpHeaders.ACCEPT_LANGUAGE, "fr;q=0.7,en;q=0.9");
request.header("test-value", "one");
request.header("test-value", "two");

final HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
assertEquals("one,two", response.body());
}
}

0 comments on commit 7bbd0ca

Please sign in to comment.