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 .SdkHttpRequest ;
56
+ import software .amazon .awssdk .http .SdkHttpResponse ;
41
57
import software .amazon .awssdk .imds .Ec2MetadataClient ;
42
58
import software .amazon .awssdk .imds .Ec2MetadataResponse ;
43
59
@@ -58,13 +74,31 @@ void tearDown(WireMockRuntimeInfo wmRuntimeInfo) {
58
74
}
59
75
60
76
@ 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" )));
77
+ void get_tokenFailsError4xx_shouldNotRetry () throws IOException {
78
+ SdkHttpClient mockClient = mock (SdkHttpClient .class );
79
+ ExecutableHttpRequest mockRequest = mock (ExecutableHttpRequest .class );
80
+ when (mockClient .prepareRequest (any (HttpExecuteRequest .class ))).thenReturn (mockRequest );
81
+
82
+ AbortableInputStream content =
83
+ AbortableInputStream .create (new ByteArrayInputStream ("ERROR 400" .getBytes (StandardCharsets .UTF_8 )));
84
+ SdkHttpResponse httpResponse = SdkHttpFullResponse .builder ()
85
+ .statusCode (400 )
86
+ .build ();
87
+ HttpExecuteResponse executeResponse = HttpExecuteResponse .builder ()
88
+ .response (httpResponse )
89
+ .responseBody (content )
90
+ .build ();
91
+ when (mockRequest .call ()).thenReturn (executeResponse );
92
+
93
+ Ec2MetadataClient imdsClient = Ec2MetadataClient .builder ().httpClient (mockClient ).build ();
94
+
95
+ assertThatThrownBy (() ->imdsClient .get ("/latest/meta-data/ami-id" )).isInstanceOf (SdkClientException .class );
96
+
97
+ ArgumentCaptor <HttpExecuteRequest > requestCaptor = ArgumentCaptor .forClass (HttpExecuteRequest .class );
98
+ Mockito .verify (mockClient ).prepareRequest (requestCaptor .capture ());
99
+ SdkHttpRequest httpRequest = requestCaptor .getValue ().httpRequest ();
100
+ assertThat (httpRequest .encodedPath ()).isEqualTo ("/latest/api/token" );
101
+ assertThat (httpRequest .firstMatchingHeader ("x-aws-ec2-metadata-token-ttl-seconds" ).get ()).isEqualTo ("21600" );
68
102
}
69
103
70
104
@ Test
0 commit comments