11package com .retailsvc .http ;
22
3+ import static com .retailsvc .http .spec .HttpMethod .GET ;
4+ import static com .retailsvc .http .spec .HttpMethod .HEAD ;
5+ import static com .retailsvc .http .spec .HttpMethod .POST ;
36import static org .assertj .core .api .Assertions .assertThat ;
47import static org .assertj .core .api .Assertions .assertThatThrownBy ;
5- import static org .mockito .Mockito .mock ;
6- import static org .mockito .Mockito .verify ;
7- import static org .mockito .Mockito .when ;
8-
9- import com .sun .net .httpserver .Headers ;
10- import com .sun .net .httpserver .HttpExchange ;
11- import java .io .ByteArrayOutputStream ;
12- import java .io .IOException ;
8+
9+ import com .retailsvc .http .spec .HttpMethod ;
10+ import java .util .Map ;
11+ import java .util .function .UnaryOperator ;
1312import org .junit .jupiter .api .Test ;
1413
1514class HandlersTest {
1615
17- @ Test
18- void aliveHandlerReturns204OnGet () throws IOException {
19- HttpExchange ex = newExchange ("GET" );
20- Handlers .aliveHandler ().handle (ex );
21- verify (ex ).sendResponseHeaders (204 , -1 );
16+ private static final UnaryOperator <String > NO_HEADERS = name -> null ;
17+
18+ private static Request request (HttpMethod method ) {
19+ return new Request (new byte [0 ], null , null , null , Map .of (), null , NO_HEADERS , Map .of (), method );
2220 }
2321
2422 @ Test
25- void aliveHandlerReturns204OnHead () throws IOException {
26- HttpExchange ex = newExchange ("HEAD" );
27- Handlers .aliveHandler ().handle (ex );
28- verify (ex ).sendResponseHeaders (204 , -1 );
23+ void aliveHandlerReturns204OnGet () {
24+ Response resp = Handlers .aliveHandler ().handle (request (GET ));
25+
26+ assertThat (resp .status ()).isEqualTo (204 );
27+ assertThat (resp .body ()).isNull ();
2928 }
3029
3130 @ Test
32- void aliveHandlerReturns405OnPost () throws IOException {
33- HttpExchange ex = newExchange ("POST" );
34- Headers headers = new Headers ();
35- when (ex .getResponseHeaders ()).thenReturn (headers );
36- Handlers .aliveHandler ().handle (ex );
37- verify (ex ).sendResponseHeaders (405 , -1 );
38- assertThat (headers .getFirst ("Allow" )).isEqualTo ("GET, HEAD" );
31+ void aliveHandlerReturns204OnHead () {
32+ Response resp = Handlers .aliveHandler ().handle (request (HEAD ));
33+
34+ assertThat (resp .status ()).isEqualTo (204 );
3935 }
4036
4137 @ Test
42- void specHandlerServesYamlWithInferredContentType () throws IOException {
43- HttpExchange ex = newExchange ("GET" );
44- Headers responseHeaders = new Headers ();
45- when (ex .getResponseHeaders ()).thenReturn (responseHeaders );
46- ByteArrayOutputStream body = new ByteArrayOutputStream ();
47- when (ex .getResponseBody ()).thenReturn (body );
48-
49- Handlers .specHandler ("/openapi.yaml" ).handle (ex );
50-
51- assertThat (responseHeaders .getFirst ("Content-Type" )).isEqualTo ("application/yaml" );
52- verify (ex )
53- .sendResponseHeaders (
54- org .mockito .ArgumentMatchers .eq (200 ),
55- org .mockito .ArgumentMatchers .longThat (n -> n > 0 ));
56- assertThat (body .toByteArray ()).isNotEmpty ();
38+ void aliveHandlerReturns405OnPost () {
39+ Response resp = Handlers .aliveHandler ().handle (request (POST ));
40+
41+ assertThat (resp .status ()).isEqualTo (405 );
42+ assertThat (resp .headers ()).containsEntry ("Allow" , "GET, HEAD" );
5743 }
5844
5945 @ Test
60- void specHandlerInfersJsonContentType () throws IOException {
61- HttpExchange ex = newExchange ("GET" );
62- Headers responseHeaders = new Headers ();
63- when (ex .getResponseHeaders ()).thenReturn (responseHeaders );
64- when (ex .getResponseBody ()).thenReturn (new ByteArrayOutputStream ());
46+ void specHandlerServesYamlBytesWithInferredContentType () {
47+ Response resp = Handlers .specHandler ("/openapi.yaml" ).handle (request (GET ));
6548
66- Handlers .specHandler ("/openapi.json" ).handle (ex );
49+ assertThat (resp .status ()).isEqualTo (200 );
50+ assertThat (resp .contentType ()).isEqualTo ("application/yaml" );
51+ assertThat (resp .body ()).isInstanceOf (byte [].class );
52+ assertThat ((byte []) resp .body ()).isNotEmpty ();
53+ }
54+
55+ @ Test
56+ void specHandlerInfersJsonContentType () {
57+ Response resp = Handlers .specHandler ("/openapi.json" ).handle (request (GET ));
6758
68- assertThat (responseHeaders . getFirst ( "Content-Type" )).isEqualTo ("application/json" );
59+ assertThat (resp . contentType ( )).isEqualTo ("application/json" );
6960 }
7061
7162 @ Test
@@ -76,21 +67,20 @@ void specHandlerThrowsAtConstructionForMissingResource() {
7667 }
7768
7869 @ Test
79- void specHandlerReturns405OnPost () throws IOException {
80- HttpExchange ex = newExchange ("POST" );
81- Headers headers = new Headers ();
82- when (ex .getResponseHeaders ()).thenReturn (headers );
70+ void specHandlerReturns405OnPost () {
71+ Response resp = Handlers .specHandler ("/openapi.yaml" ).handle (request (POST ));
8372
84- Handlers .specHandler ("/openapi.yaml" ).handle (ex );
85-
86- verify (ex ).sendResponseHeaders (405 , -1 );
87- assertThat (headers .getFirst ("Allow" )).isEqualTo ("GET, HEAD" );
73+ assertThat (resp .status ()).isEqualTo (405 );
74+ assertThat (resp .headers ()).containsEntry ("Allow" , "GET, HEAD" );
8875 }
8976
90- private static HttpExchange newExchange (String method ) {
91- HttpExchange ex = mock (HttpExchange .class );
92- when (ex .getRequestMethod ()).thenReturn (method );
93- when (ex .getResponseHeaders ()).thenReturn (new Headers ());
94- return ex ;
77+ @ Test
78+ void specHandlerHeadReturnsContentLengthWithoutBody () {
79+ Response resp = Handlers .specHandler ("/openapi.yaml" ).handle (request (HEAD ));
80+
81+ assertThat (resp .status ()).isEqualTo (200 );
82+ assertThat (resp .body ()).isNull ();
83+ assertThat (resp .headers ()).containsKey ("Content-Length" );
84+ assertThat (Integer .parseInt (resp .headers ().get ("Content-Length" ))).isGreaterThan (0 );
9585 }
9686}
0 commit comments