11package dev .tomr .hcloud .component .http ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
34import com .fasterxml .jackson .databind .ObjectMapper ;
45import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
56import com .github .tomakehurst .wiremock .common .Notifier ;
67import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
78import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
89import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
10+ import dev .tomr .hcloud .HetznerCloud ;
911import dev .tomr .hcloud .http .HetznerCloudHttpClient ;
1012import dev .tomr .hcloud .http .RequestVerb ;
1113import dev .tomr .hcloud .http .exception .HetznerApiException ;
1416import org .junit .jupiter .api .extension .RegisterExtension ;
1517import org .junit .jupiter .params .ParameterizedTest ;
1618import org .junit .jupiter .params .provider .ValueSource ;
19+ import org .mockito .MockedStatic ;
1720
1821import java .io .IOException ;
22+ import java .util .List ;
1923
2024import static com .github .tomakehurst .wiremock .client .WireMock .*;
2125import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
2226import static org .junit .jupiter .api .Assertions .*;
27+ import static org .mockito .Mockito .*;
2328
2429public class HttpClientComponentTest {
2530
@@ -99,15 +104,7 @@ void testHttpClientThrowsBadResponseGracefully() {
99104
100105 HetznerApiException hetznerApiException = assertThrows (
101106 HetznerApiException .class ,
102- () -> {
103- try {
104- client .sendHttpRequest (TestModel .class , HOST + "badResponse" , RequestVerb .GET , "" );
105- } catch (Exception e ) {
106- System .out .println (e .getMessage ());
107- e .printStackTrace ();
108- throw e ;
109- }
110- }
107+ () -> client .sendHttpRequest (TestModel .class , HOST + "badResponse" , RequestVerb .GET , "" )
111108 );
112109
113110 assertEquals ("HetznerErrorResponse [code=uniqueness_error, message=SSH key with the same fingerprint already exists]" , hetznerApiException .getMessage ());
@@ -124,12 +121,33 @@ void testHttpClientThrowsHetznerApiExceptionWhenBadJsonResponse() {
124121
125122 @ Test
126123 @ DisplayName ("HTTP Client handles 204 no content correctly" )
127- void httpClientHandles204NoContent () throws IOException , InterruptedException , IllegalAccessException {
124+ void httpClientHandles204NoContent () {
128125 // this test is needed because 204 does not support handling a body
129126 // todo refactor how we handle HTTP Status codes we **know** will never supply a body, i.e. 204 - this implementation leaves a lot to be desired
130127 wireMockExtension .stubFor (get ("/test" ).willReturn (aResponse ().withStatus (204 )));
131128 HetznerCloudHttpClient client = new HetznerCloudHttpClient ();
132129
133130 assertDoesNotThrow (() -> client .sendHttpRequest (TestModel .class , HOST + "test" , RequestVerb .GET , "" ));
134131 }
132+
133+ @ Test
134+ @ DisplayName ("Request uses HTTP/2 when targeting Hetzner, instead of HTTP/1.1 by default" )
135+ void requestUsesHttp2WhenTargetingHetznerHost () throws IOException , InterruptedException , IllegalAccessException {
136+ wireMockExtension .stubFor (get ("/test" ).willReturn (ok (objectMapper .writeValueAsString (new TestModel (1 , 1 , "sunt aut facere repellat provident occaecati excepturi optio reprehenderit" , "quia et suscipit\n suscipit recusandae consequuntur expedita et cum\n reprehenderit molestiae ut ut quas totam\n nostrum rerum est autem sunt rem eveniet architecto" )))));
137+ HetznerCloud hetznerCloud = mock (HetznerCloud .class );
138+ try (MockedStatic <HetznerCloud > hetznerCloudMockedStatic = mockStatic (HetznerCloud .class )) {
139+ hetznerCloudMockedStatic .when (HetznerCloud ::getHetznerCloudHost ).thenReturn (HOST );
140+ hetznerCloudMockedStatic .when (HetznerCloud ::getInstance ).thenReturn (hetznerCloud );
141+ when (hetznerCloud .getHttpDetails ()).thenReturn (List .of (HOST , "" ));
142+
143+ HetznerCloudHttpClient client = new HetznerCloudHttpClient ();
144+
145+ TestModel testModel = client .sendHttpRequest (TestModel .class , HOST + "test" , RequestVerb .GET , "" );
146+
147+ assertEquals (1 , testModel .getId ());
148+ assertEquals (1 , testModel .getUserId ());
149+ assertEquals ("sunt aut facere repellat provident occaecati excepturi optio reprehenderit" , testModel .getTitle ());
150+ assertEquals ("quia et suscipit\n suscipit recusandae consequuntur expedita et cum\n reprehenderit molestiae ut ut quas totam\n nostrum rerum est autem sunt rem eveniet architecto" , testModel .getBody ());
151+ }
152+ }
135153}
0 commit comments