Skip to content

Commit

Permalink
Merge pull request #8 from Theodo-UK/feature/s3-content-type
Browse files Browse the repository at this point in the history
Add toHaveContentTypeEqualTo assertion
  • Loading branch information
hamilton-s authored Jul 26, 2021
2 parents 11a83a8 + 788149f commit 58d03f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ sls-test-tools is currently being actively maintained, yet is in alpha. Your fee

### S3

// note these is an async assertion and requires "await"
Note: these async assertions require "await"

```
await expect("BUCKET NAME").toHaveS3ObjectWithNameEqualTo("FILE NAME");
Expand All @@ -54,6 +54,15 @@ sls-test-tools is currently being actively maintained, yet is in alpha. Your fee
await expect("BUCKET NAME").toExistAsS3Bucket();
```

```
await expect({
bucketName: "BUCKET_NAME",
objectName: "FILE NAME",
}).toHaveContentTypeEqualTo("CONTENT_TYPE");;
```

where CONTENT_TYPE are [standards MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types)

## Helpers

### General
Expand Down
33 changes: 33 additions & 0 deletions src/assertions/toHaveContentTypeEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";

export default {
async toHaveContentTypeEqualTo({ bucketName, objectName }, contentType) {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
Key: objectName,
};

let message;
try {
const object = await s3.getObject(params).promise();
if (object.ContentType === contentType) {
message = `expected ${objectName} to have content type ${contentType}`;
return testResult(message, true);
}
message = `expected ${objectName} to have content type ${contentType}, but content type found was ${object.ContentType}`;
return testResult(message, false);
} catch (error) {
if (error.code === "NoSuchKey") {
message = `expected ${bucketName} to have object with name ${objectName} - not found`;
return testResult(message, false);
}
if (error.code === "NoSuchBucket") {
message = `expected ${bucketName} to exist - not found`;
return testResult(message, false);
}
throw error;
}
},
};
17 changes: 7 additions & 10 deletions src/assertions/toHaveEventWithSource/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { testResult } from "../../utils/testResult";

export default {
toHaveEventWithSource(eventBridgeEvents, expectedSourceName) {
let message;
const receivedSource = JSON.parse(eventBridgeEvents.Messages[0].Body)
.source;
if (receivedSource === expectedSourceName) {
return {
message: () =>
`expected sent event to have source ${expectedSourceName}`,
pass: true,
};
message = `expected sent event to have source ${expectedSourceName}`;
return testResult(message, true);
}
return {
message: () =>
`sent event source "${receivedSource}" does not match expected source "${expectedSourceName}"`,
pass: false,
};
message = `sent event source "${receivedSource}" does not match expected source "${expectedSourceName}"`;
return testResult(message, false);
},
};
5 changes: 5 additions & 0 deletions src/utils/testResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line import/prefer-default-export
export const testResult = (message, pass) => ({
message: () => message,
pass,
});

0 comments on commit 58d03f0

Please sign in to comment.