Skip to content

Commit

Permalink
fix(cloudfront): pass viewerProtocolPolicy to the distribution's be…
Browse files Browse the repository at this point in the history
…haviors (#1932)
  • Loading branch information
skorfmann authored and Sam Goodwin committed Mar 7, 2019
1 parent dbd2401 commit 615ecd4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,13 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53.
if (defaultBehaviors.length !== 1) {
throw new Error("There can only be one default behavior across all sources. [ One default behavior per distribution ].");
}
distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0]);
distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0], props.viewerProtocolPolicy);
const otherBehaviors: CfnDistribution.CacheBehaviorProperty[] = [];
for (const behavior of behaviors.filter(b => !b.isDefaultBehavior)) {
if (!behavior.pathPattern) {
throw new Error("pathPattern is required for all non-default behaviors");
}
otherBehaviors.push(this.toBehavior(behavior) as CfnDistribution.CacheBehaviorProperty);
otherBehaviors.push(this.toBehavior(behavior, props.viewerProtocolPolicy) as CfnDistribution.CacheBehaviorProperty);
}
distributionConfig.cacheBehaviors = otherBehaviors;

Expand Down
78 changes: 77 additions & 1 deletion packages/@aws-cdk/aws-cloudfront/test/test.basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from '@aws-cdk/assert';
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { CloudFrontWebDistribution } from '../lib';
import { CloudFrontWebDistribution, ViewerProtocolPolicy } from '../lib';

// tslint:disable:object-literal-key-quotes

Expand Down Expand Up @@ -246,4 +246,80 @@ export = {
});
test.done();
},

'distribution with ViewerProtocolPolicy set to a non-default value'(test: Test) {
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');

new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
viewerProtocolPolicy: ViewerProtocolPolicy.AllowAll,
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket
},
behaviors: [
{
isDefaultBehavior: true,
}
]
}
]
});

expect(stack).toMatch({
"Resources": {
"Bucket83908E77": {
"Type": "AWS::S3::Bucket",
"DeletionPolicy": "Retain",
},
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"DefaultRootObject": "index.html",
"Origins": [
{
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
"DomainName"
]
},
"Id": "origin1",
"S3OriginConfig": {}
}
],
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
},
"PriceClass": "PriceClass_100",
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {
"QueryString": false,
"Cookies": { "Forward": "none" }
}
},
"Enabled": true,
"IPV6Enabled": true,
"HttpVersion": "http2",
"CacheBehaviors": []
}
}
}
}
});
test.done();
},

};

0 comments on commit 615ecd4

Please sign in to comment.