-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlersDefaultExceptionTest.java
More file actions
54 lines (46 loc) · 1.97 KB
/
Copy pathHandlersDefaultExceptionTest.java
File metadata and controls
54 lines (46 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.retailsvc.http;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import com.retailsvc.http.spec.HttpMethod;
import com.retailsvc.http.validate.ValidationError;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import java.io.ByteArrayOutputStream;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
class HandlersDefaultExceptionTest {
private HttpExchange newExchange(ByteArrayOutputStream sink) {
HttpExchange ex = mock(HttpExchange.class);
Mockito.when(ex.getResponseHeaders()).thenReturn(new Headers());
Mockito.when(ex.getResponseBody()).thenReturn(sink);
return ex;
}
@Test
void validationExceptionRendersProblem() throws Exception {
ByteArrayOutputStream sink = new ByteArrayOutputStream();
HttpExchange ex = newExchange(sink);
Handlers.defaultExceptionHandler()
.handle(
ex,
new ValidationException(new ValidationError("/x", "type", "expected string", null)));
Mockito.verify(ex).sendResponseHeaders(Mockito.eq(400), Mockito.anyLong());
assertThat(ex.getResponseHeaders().getFirst("Content-Type"))
.isEqualTo("application/problem+json");
assertThat(sink.toString()).contains("\"keyword\":\"type\"");
}
@Test
void notFoundReturns404() throws Exception {
HttpExchange ex = newExchange(new ByteArrayOutputStream());
Handlers.defaultExceptionHandler().handle(ex, new NotFoundException("GET /x"));
Mockito.verify(ex).sendResponseHeaders(404, 0);
}
@Test
void methodNotAllowedReturns405WithAllowHeader() throws Exception {
HttpExchange ex = newExchange(new ByteArrayOutputStream());
Handlers.defaultExceptionHandler()
.handle(ex, new MethodNotAllowedException(Set.of(HttpMethod.GET, HttpMethod.POST)));
Mockito.verify(ex).sendResponseHeaders(405, 0);
assertThat(ex.getResponseHeaders().getFirst("Allow")).contains("GET").contains("POST");
}
}