-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add CORS Property to S3 Bucket for configuring bucket cross-origin access rules.
- Loading branch information
Showing
2 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { expect, haveResource } from '@aws-cdk/assert'; | ||
import { Stack } from '@aws-cdk/cdk'; | ||
import { Test } from 'nodeunit'; | ||
import { Bucket, HttpMethods } from '../lib'; | ||
|
||
export = { | ||
'Can use addCors() to add a CORS configuration'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
// WHEN | ||
const bucket = new Bucket(stack, 'Bucket'); | ||
bucket.addCorsRule({ | ||
allowedMethods: [HttpMethods.GET, HttpMethods.HEAD], | ||
allowedOrigins: ["https://example.com"] | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::S3::Bucket', { | ||
CorsConfiguration: { | ||
CorsRules: [{ | ||
AllowedMethods: ["GET", "HEAD"], | ||
AllowedOrigins: ["https://example.com"] | ||
}] | ||
} | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
'Bucket with multiple cors configurations'(test: Test) { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
// WHEN | ||
new Bucket(stack, 'Bucket', { | ||
cors: [ | ||
{ | ||
allowedHeaders: [ | ||
"*" | ||
], | ||
allowedMethods: [ | ||
HttpMethods.GET | ||
], | ||
allowedOrigins: [ | ||
"*" | ||
], | ||
exposedHeaders: [ | ||
"Date" | ||
], | ||
id: "myCORSRuleId1", | ||
maxAge: 3600 | ||
}, | ||
{ | ||
allowedHeaders: [ | ||
"x-amz-*" | ||
], | ||
allowedMethods: [ | ||
HttpMethods.DELETE | ||
], | ||
allowedOrigins: [ | ||
"http://www.example1.com", | ||
"http://www.example2.com" | ||
], | ||
exposedHeaders: [ | ||
"Connection", | ||
"Server", | ||
"Date" | ||
], | ||
id: "myCORSRuleId2", | ||
maxAge: 1800 | ||
} | ||
] | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::S3::Bucket', { | ||
CorsConfiguration: { | ||
CorsRules: [ | ||
{ | ||
AllowedHeaders: [ | ||
"*" | ||
], | ||
AllowedMethods: [ | ||
"GET" | ||
], | ||
AllowedOrigins: [ | ||
"*" | ||
], | ||
ExposedHeaders: [ | ||
"Date" | ||
], | ||
Id: "myCORSRuleId1", | ||
MaxAge: 3600 | ||
}, | ||
{ | ||
AllowedHeaders: [ | ||
"x-amz-*" | ||
], | ||
AllowedMethods: [ | ||
"DELETE" | ||
], | ||
AllowedOrigins: [ | ||
"http://www.example1.com", | ||
"http://www.example2.com" | ||
], | ||
ExposedHeaders: [ | ||
"Connection", | ||
"Server", | ||
"Date" | ||
], | ||
Id: "myCORSRuleId2", | ||
MaxAge: 1800 | ||
} | ||
] | ||
} | ||
})); | ||
|
||
test.done(); | ||
}, | ||
}; |