Skip to content

Commit

Permalink
Merge pull request #373 from dbmdz/fix-not-acceptable-accept-header
Browse files Browse the repository at this point in the history
Fix return code for HttpMediaTypeNotAcceptable
  • Loading branch information
stefan-it authored Apr 13, 2023
2 parents 5d57ee7 + 3c770fd commit c43ba8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
Expand Down Expand Up @@ -61,6 +62,12 @@ public void handleUnsupportedOperationException(Exception exception) {
// NOP
}

@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public void handleHttpMediaTypeNotAcceptableException(Exception exception) {
// NOP
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public void handleAllOther(Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -59,4 +58,21 @@ public void testManifest() {
assertThat(response.getHeaders().get("mani1")).containsExactly("mani-value1");
assertThat(response.getHeaders().get("mani2")).containsExactly("mani-value2");
}

@Test
public void testManifestNotAcceptableHeader() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Host", "localhost");
requestHeaders.add("Accept", "application/something-strange");

ResponseEntity<String> response =
restTemplate.exchange(
"/presentation/"
+ IIIFPresentationApiController.VERSION
+ "/manifest-valid-data/manifest",
HttpMethod.GET,
new HttpEntity<>(requestHeaders),
String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
}
}

0 comments on commit c43ba8b

Please sign in to comment.