1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.protocol.tests.endpoint; 17 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import java.net.URI; 24 import java.net.URISyntaxException; 25 import org.junit.Assert; 26 import org.junit.Before; 27 import org.junit.Ignore; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.ArgumentCaptor; 31 import org.mockito.Mock; 32 import org.mockito.junit.MockitoJUnitRunner; 33 import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; 34 import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; 35 import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; 36 import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; 37 import software.amazon.awssdk.core.exception.SdkClientException; 38 import software.amazon.awssdk.http.ExecutableHttpRequest; 39 import software.amazon.awssdk.http.HttpExecuteRequest; 40 import software.amazon.awssdk.http.SdkHttpClient; 41 import software.amazon.awssdk.http.SdkHttpRequest; 42 import software.amazon.awssdk.regions.Region; 43 import software.amazon.awssdk.services.protocoljsonendpointtrait.ProtocolJsonEndpointTraitClient; 44 import software.amazon.awssdk.services.protocoljsonendpointtrait.ProtocolJsonEndpointTraitClientBuilder; 45 import software.amazon.awssdk.services.protocoljsonendpointtrait.model.EndpointTraitOneRequest; 46 import software.amazon.awssdk.services.protocoljsonendpointtrait.model.EndpointTraitTwoRequest; 47 48 @RunWith(MockitoJUnitRunner.class) 49 public class EndpointTraitTest { 50 51 @Mock 52 private SdkHttpClient mockHttpClient; 53 54 @Mock 55 private ExecutableHttpRequest abortableCallable; 56 57 private ProtocolJsonEndpointTraitClient client; 58 59 private ProtocolJsonEndpointTraitClient clientWithDisabledHostPrefix; 60 61 @Before setup()62 public void setup() throws Exception { 63 client = clientBuilder().build(); 64 65 clientWithDisabledHostPrefix = clientBuilder().overrideConfiguration( 66 ClientOverrideConfiguration.builder() 67 .putAdvancedOption(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION, true) 68 .build()).build(); 69 70 when(mockHttpClient.prepareRequest(any())).thenReturn(abortableCallable); 71 72 when(abortableCallable.call()).thenThrow(SdkClientException.create("Dummy exception")); 73 } 74 75 @Test hostExpression_withoutInputMemberLabel()76 public void hostExpression_withoutInputMemberLabel() throws URISyntaxException { 77 try { 78 client.endpointTraitOne(EndpointTraitOneRequest.builder().build()); 79 Assert.fail("Expected an exception"); 80 } catch (SdkClientException exception) { 81 ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); 82 verify(mockHttpClient).prepareRequest(httpRequestCaptor.capture()); 83 84 SdkHttpRequest request = httpRequestCaptor.getAllValues().get(0).httpRequest(); 85 assertThat(request.host()).isEqualTo("data.localhost.com"); 86 assertThat(request.port()).isEqualTo(443); 87 assertThat(request.encodedPath()).isEqualTo("/"); 88 assertThat(request.getUri()).isEqualTo(new URI("http://data.localhost.com:443/")); 89 } 90 } 91 92 @Test hostExpression_withInputMemberLabel()93 public void hostExpression_withInputMemberLabel() throws URISyntaxException { 94 try { 95 client.endpointTraitTwo(EndpointTraitTwoRequest.builder() 96 .stringMember("123456") 97 .pathIdempotentToken("dummypath") 98 .build()); 99 Assert.fail("Expected an exception"); 100 } catch (SdkClientException exception) { 101 ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); 102 verify(mockHttpClient).prepareRequest(httpRequestCaptor.capture()); 103 104 SdkHttpRequest request = httpRequestCaptor.getAllValues().get(0).httpRequest(); 105 assertThat(request.host()).isEqualTo("123456-localhost.com"); 106 assertThat(request.port()).isEqualTo(443); 107 assertThat(request.encodedPath()).isEqualTo("/dummypath"); 108 assertThat(request.getUri()).isEqualTo(new URI("http://123456-localhost.com:443/dummypath")); 109 } 110 } 111 112 @Test (expected = IllegalArgumentException.class) validationException_whenInputMember_inHostPrefix_isNull()113 public void validationException_whenInputMember_inHostPrefix_isNull() { 114 client.endpointTraitTwo(EndpointTraitTwoRequest.builder().build()); 115 } 116 117 @Test (expected = IllegalArgumentException.class) validationException_whenInputMember_inHostPrefix_isEmpty()118 public void validationException_whenInputMember_inHostPrefix_isEmpty() { 119 client.endpointTraitTwo(EndpointTraitTwoRequest.builder().stringMember("").build()); 120 } 121 122 @Test clientWithDisabledHostPrefix_withoutInputMemberLabel_usesOriginalUri()123 public void clientWithDisabledHostPrefix_withoutInputMemberLabel_usesOriginalUri() { 124 try { 125 clientWithDisabledHostPrefix.endpointTraitOne(EndpointTraitOneRequest.builder().build()); 126 Assert.fail("Expected an exception"); 127 } catch (SdkClientException exception) { 128 ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); 129 verify(mockHttpClient).prepareRequest(httpRequestCaptor.capture()); 130 131 SdkHttpRequest request = httpRequestCaptor.getAllValues().get(0).httpRequest(); 132 assertThat(request.host()).isEqualTo("localhost.com"); 133 } 134 } 135 136 @Test clientWithDisabledHostPrefix_withInputMemberLabel_usesOriginalUri()137 public void clientWithDisabledHostPrefix_withInputMemberLabel_usesOriginalUri() { 138 try { 139 clientWithDisabledHostPrefix.endpointTraitTwo(EndpointTraitTwoRequest.builder() 140 .stringMember("123456") 141 .build()); 142 Assert.fail("Expected an exception"); 143 } catch (SdkClientException exception) { 144 ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); 145 verify(mockHttpClient).prepareRequest(httpRequestCaptor.capture()); 146 147 SdkHttpRequest request = httpRequestCaptor.getAllValues().get(0).httpRequest(); 148 assertThat(request.host()).isEqualTo("localhost.com"); 149 } 150 } 151 mockCredentials()152 private StaticCredentialsProvider mockCredentials() { 153 return StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); 154 } 155 getEndpoint()156 private String getEndpoint() { 157 return "http://localhost.com:443"; 158 } 159 clientBuilder()160 private ProtocolJsonEndpointTraitClientBuilder clientBuilder() { 161 return ProtocolJsonEndpointTraitClient.builder() 162 .httpClient(mockHttpClient) 163 .credentialsProvider(mockCredentials()) 164 .region(Region.US_EAST_1) 165 .endpointOverride(URI.create(getEndpoint())); 166 } 167 } 168