28
28
import static com .github .tomakehurst .wiremock .stubbing .Scenario .STARTED ;
29
29
import static org .assertj .core .api .Assertions .assertThat ;
30
30
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
31
+ import static org .mockito .ArgumentMatchers .any ;
32
+ import static org .mockito .Mockito .mock ;
33
+ import static org .mockito .Mockito .when ;
31
34
import static software .amazon .awssdk .imds .TestConstants .EC2_METADATA_TOKEN_TTL_HEADER ;
32
35
33
36
import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
34
37
import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
38
+ import java .io .ByteArrayInputStream ;
39
+ import java .io .IOException ;
35
40
import java .net .URI ;
41
+ import java .nio .charset .StandardCharsets ;
36
42
import java .time .Duration ;
37
43
import org .junit .jupiter .api .AfterEach ;
38
44
import org .junit .jupiter .api .BeforeEach ;
39
45
import org .junit .jupiter .api .Test ;
46
+ import org .mockito .ArgumentCaptor ;
47
+ import org .mockito .Mockito ;
40
48
import software .amazon .awssdk .core .exception .SdkClientException ;
49
+ import software .amazon .awssdk .http .AbortableInputStream ;
50
+ import software .amazon .awssdk .http .ExecutableHttpRequest ;
51
+ import software .amazon .awssdk .http .HttpExecuteRequest ;
52
+ import software .amazon .awssdk .http .HttpExecuteResponse ;
53
+ import software .amazon .awssdk .http .SdkHttpClient ;
54
+ import software .amazon .awssdk .http .SdkHttpFullResponse ;
55
+ import software .amazon .awssdk .http .SdkHttpResponse ;
41
56
import software .amazon .awssdk .imds .Ec2MetadataClient ;
42
57
import software .amazon .awssdk .imds .Ec2MetadataResponse ;
43
58
@@ -58,13 +73,30 @@ void tearDown(WireMockRuntimeInfo wmRuntimeInfo) {
58
73
}
59
74
60
75
@ Test
61
- void get_tokenFailsError4xx_shouldNotRetry () {
62
- stubFor (put (urlPathEqualTo ("/latest/api/token" )).willReturn (aResponse ().withStatus (400 ).withBody ("ERROR 400" )));
63
- stubFor (get (urlPathEqualTo ("/latest/meta-data/ami-id" )).willReturn (aResponse ().withBody ("{}" )));
64
-
65
- assertThatThrownBy (() -> clientBuilder .build ().get ("/latest/meta-data/ami-id" )).isInstanceOf (SdkClientException .class );
66
- verify (exactly (1 ), putRequestedFor (urlPathEqualTo ("/latest/api/token" ))
67
- .withHeader ("x-aws-ec2-metadata-token-ttl-seconds" , equalTo ("21600" )));
76
+ void get_tokenFailsError4xx_shouldNotRetry () throws IOException {
77
+ SdkHttpClient mockClient = mock (SdkHttpClient .class );
78
+ ExecutableHttpRequest mockRequest = mock (ExecutableHttpRequest .class );
79
+ when (mockClient .prepareRequest (any (HttpExecuteRequest .class ))).thenReturn (mockRequest );
80
+
81
+ AbortableInputStream content =
82
+ AbortableInputStream .create (new ByteArrayInputStream ("ERROR 400" .getBytes (StandardCharsets .UTF_8 )));
83
+ SdkHttpResponse httpResponse = SdkHttpFullResponse .builder ()
84
+ .statusCode (400 )
85
+ .build ();
86
+ HttpExecuteResponse executeResponse = HttpExecuteResponse .builder ()
87
+ .response (httpResponse )
88
+ .responseBody (content )
89
+ .build ();
90
+ when (mockRequest .call ()).thenReturn (executeResponse );
91
+
92
+ Ec2MetadataClient imdsClient = Ec2MetadataClient .builder ().httpClient (mockClient ).build ();
93
+
94
+ assertThatThrownBy (() ->imdsClient .get ("/latest/meta-data/ami-id" )).isInstanceOf (SdkClientException .class );
95
+
96
+ ArgumentCaptor <HttpExecuteRequest > requestCaptor = ArgumentCaptor .forClass (HttpExecuteRequest .class );
97
+ Mockito .verify (mockClient ).prepareRequest (requestCaptor .capture ());
98
+ HttpExecuteRequest executeRequest = requestCaptor .getValue ();
99
+ assertThat (executeRequest .httpRequest ().encodedPath ()).isEqualTo ("/latest/api/token" );
68
100
}
69
101
70
102
@ Test
0 commit comments