1515
1616package software .amazon .awssdk .nativeimagetest ;
1717
18+ import java .io .IOException ;
1819import java .nio .charset .StandardCharsets ;
20+ import java .nio .file .Files ;
21+ import java .nio .file .Path ;
1922import java .util .UUID ;
2023import org .slf4j .Logger ;
2124import org .slf4j .LoggerFactory ;
2427import software .amazon .awssdk .services .s3 .S3AsyncClient ;
2528import software .amazon .awssdk .services .s3 .S3Client ;
2629import software .amazon .awssdk .services .s3 .model .CreateBucketResponse ;
30+ import software .amazon .awssdk .services .s3 .model .HeadObjectResponse ;
2731
2832public class S3TestRunner implements TestRunner {
2933 private static final String BUCKET_NAME = "v2-native-image-tests-" + UUID .randomUUID ();
3034 private static final Logger logger = LoggerFactory .getLogger (S3TestRunner .class );
3135 private static final String KEY = "key" ;
36+ private static final String MIMETYPE_KEY = "mimetype-key.txt" ;
37+ private static final String EXPECTED_TXT_CONTENT_TYPE = "text/plain" ;
3238 private final S3Client s3ApacheHttpClient ;
3339 private final S3Client s3UrlConnectionHttpClient ;
3440 private final S3AsyncClient s3NettyClient ;
@@ -43,6 +49,8 @@ public S3TestRunner() {
4349 public void runTests () {
4450 logger .info ("starting to run S3 tests" );
4551 CreateBucketResponse bucketResponse = null ;
52+ Path tempFile = null ;
53+ boolean mimetypeObjectCreated = false ;
4654 try {
4755 bucketResponse = s3UrlConnectionHttpClient .createBucket (b -> b .bucket (BUCKET_NAME ));
4856
@@ -56,12 +64,42 @@ public void runTests() {
5664 s3NettyClient .getObject (b -> b .bucket (BUCKET_NAME ).key (KEY ),
5765 AsyncResponseTransformer .toBytes ()).join ();
5866
67+ tempFile = createTempTextFile ();
68+ s3ApacheHttpClient .putObject (b -> b .bucket (BUCKET_NAME ).key (MIMETYPE_KEY ),
69+ RequestBody .fromFile (tempFile ));
70+
71+ HeadObjectResponse head = s3ApacheHttpClient .headObject (b -> b .bucket (BUCKET_NAME ).key (MIMETYPE_KEY ));
72+ String contentType = head .contentType ();
73+ if (contentType == null || !contentType .startsWith (EXPECTED_TXT_CONTENT_TYPE )) {
74+ throw new RuntimeException ("Expected Content-Type to start with '" + EXPECTED_TXT_CONTENT_TYPE
75+ + "' but was '" + contentType + "'. The mime.types resource may be missing"
76+ + " from the native image classpath." );
77+ }
78+
5979 } finally {
6080 if (bucketResponse != null ) {
6181 s3NettyClient .deleteObject (b -> b .bucket (BUCKET_NAME ).key (KEY )).join ();
82+ s3NettyClient .deleteObject (b -> b .bucket (BUCKET_NAME ).key (MIMETYPE_KEY )).join ();
6283
6384 s3NettyClient .deleteBucket (b -> b .bucket (BUCKET_NAME )).join ();
6485 }
86+ if (tempFile != null ) {
87+ try {
88+ Files .deleteIfExists (tempFile );
89+ } catch (IOException e ) {
90+ logger .warn ("Failed to delete temp file {}" , tempFile , e );
91+ }
92+ }
93+ }
94+ }
95+
96+ private static Path createTempTextFile () {
97+ try {
98+ Path file = Files .createTempFile ("native-image-mimetype-" , ".txt" );
99+ Files .write (file , "helloworld" .getBytes (StandardCharsets .UTF_8 ));
100+ return file ;
101+ } catch (IOException e ) {
102+ throw new RuntimeException ("Failed to create temp .txt file for mimetype test" , e );
65103 }
66104 }
67105}
0 commit comments