/
HostPrefixTest.java
108 lines (92 loc) · 5.66 KB
/
HostPrefixTest.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.services;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.AbortableInputStream;
import software.amazon.awssdk.http.HttpExecuteResponse;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient;
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;
import software.amazon.awssdk.utils.StringInputStream;
import software.amazon.awssdk.utils.builder.SdkBuilder;
public class HostPrefixTest {
private MockSyncHttpClient mockHttpClient;
private ProtocolRestJsonClient client;
private MockAsyncHttpClient mockAsyncClient;
private ProtocolRestJsonAsyncClient asyncClient;
@BeforeEach
public void setupClient() {
mockHttpClient = new MockSyncHttpClient();
mockAsyncClient = new MockAsyncHttpClient();
client = ProtocolRestJsonClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid",
"skid")))
.region(Region.US_EAST_1)
.endpointOverride(URI.create("http://localhost"))
.httpClient(mockHttpClient)
.build();
asyncClient = ProtocolRestJsonAsyncClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
.region(Region.US_EAST_1)
.endpointOverride(URI.create("http://localhost"))
.httpClient(mockAsyncClient)
.build();
}
@Test
public void invalidHostPrefix_shouldThrowException() {
assertThatThrownBy(() -> client.operationWithHostPrefix(b -> b.stringMember("123#")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("The provided operationWithHostPrefixRequest is not valid: the 'StringMember' component must "
+ "match the pattern \"[A-Za-z0-9\\-]+\".");
assertThatThrownBy(() -> asyncClient.operationWithHostPrefix(b -> b.stringMember("123#")).join())
.hasCauseInstanceOf(IllegalArgumentException.class).hasMessageContaining("The provided operationWithHostPrefixRequest is not valid: the 'StringMember' component must match the pattern \"[A-Za-z0-9\\-]+\".");
}
@Test
public void nullHostPrefix_shouldThrowException() {
assertThatThrownBy(() -> client.operationWithHostPrefix(SdkBuilder::build))
.isInstanceOf(IllegalArgumentException.class).hasMessageContaining("component is missing");
assertThatThrownBy(() -> asyncClient.operationWithHostPrefix(SdkBuilder::build).join())
.hasCauseInstanceOf(IllegalArgumentException.class).hasMessageContaining("component is missing");
}
@Test
public void syncValidHostPrefix_shouldPrefixEndpoint() {
mockHttpClient.stubNextResponse(HttpExecuteResponse.builder()
.response(SdkHttpResponse.builder().statusCode(200)
.build())
.responseBody(AbortableInputStream.create(new StringInputStream("")))
.build());
client.operationWithHostPrefix(b -> b.stringMember("123"));
assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("123-foo.localhost");
}
@Test
public void asyncValidHostPrefix_shouldPrefixEndpoint() {
mockAsyncClient.stubNextResponse(HttpExecuteResponse.builder()
.response(SdkHttpResponse.builder().statusCode(200)
.build())
.responseBody(AbortableInputStream.create(new StringInputStream("")))
.build());
asyncClient.operationWithHostPrefix(b -> b.stringMember("123")).join();
assertThat(mockAsyncClient.getLastRequest().getUri().getHost()).isEqualTo("123-foo.localhost");
}
}