|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 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.services.s3; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.URI; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 31 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 32 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 33 | +import software.amazon.awssdk.regions.Region; |
| 34 | + |
| 35 | +public class EndpointOverrideTest { |
| 36 | + |
| 37 | + @Rule |
| 38 | + public WireMockRule mockServer = new WireMockRule(0); |
| 39 | + |
| 40 | + private S3Client s3Client; |
| 41 | + |
| 42 | + private S3AsyncClient s3AsyncClient; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setup() { |
| 46 | + s3Client = S3Client.builder() |
| 47 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) |
| 48 | + .region(Region.US_WEST_2).endpointOverride(URI.create(getEndpoint())) |
| 49 | + .build(); |
| 50 | + |
| 51 | + s3AsyncClient = S3AsyncClient.builder() |
| 52 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) |
| 53 | + .region(Region.US_WEST_2) |
| 54 | + .endpointOverride(URI.create(getEndpoint())) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + private String getEndpoint() { |
| 59 | + return "http://localhost:" + mockServer.port(); |
| 60 | + } |
| 61 | + |
| 62 | + //https://github.com/aws/aws-sdk-java-v2/issues/437 |
| 63 | + @Test |
| 64 | + public void getObjectAsync_shouldNotThrowNPE() throws IOException { |
| 65 | + stubFor(get(anyUrl()) |
| 66 | + .willReturn(aResponse() |
| 67 | + .withStatus(200) |
| 68 | + .withBody("<?xml version=\"1.0\"?><GetObjectResult xmlns=\"http://s3" |
| 69 | + + ".amazonaws.com/doc/2006-03-01\"></GetObjectResult>"))); |
| 70 | + assertThat(s3AsyncClient.getObject(b -> b.bucket("test").key("test").build(), |
| 71 | + AsyncResponseTransformer.toBytes()).join()).isNotNull(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void getObject_shouldNotThrowNPE() { |
| 76 | + stubFor(get(anyUrl()) |
| 77 | + .willReturn(aResponse() |
| 78 | + .withStatus(200).withBody("<?xml version=\"1.0\"?><GetObjectResult xmlns=\"http://s3" |
| 79 | + + ".amazonaws.com/doc/2006-03-01\"></GetObjectResult>"))); |
| 80 | + assertThat(s3Client.listObjectVersions(b -> b.bucket("test"))).isNotNull(); |
| 81 | + } |
| 82 | +} |
0 commit comments