Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanbarlog committed Nov 21, 2024
1 parent 88411db commit 91da02b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
32 changes: 31 additions & 1 deletion packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOrigin, defaultOriginGroup } from './test-origin';
import { defaultOrigin, defaultOriginGroup, defaultOriginWithOriginAccessControl } from './test-origin';
import { Annotations, Match, Template } from '../../assertions';
import * as acm from '../../aws-certificatemanager';
import * as cloudwatch from '../../aws-cloudwatch';
Expand Down Expand Up @@ -1282,6 +1282,36 @@ test('with publish additional metrics', () => {
});
});

test('with origin access control id', () => {
const origin = defaultOriginWithOriginAccessControl();
new Distribution(stack, 'MyDist', {
defaultBehavior: { origin },
publishAdditionalMetrics: true,
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
DistributionConfig: {
DefaultCacheBehavior: {
CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6',
Compress: true,
TargetOriginId: 'StackMyDistOrigin1D6D5E535',
ViewerProtocolPolicy: 'allow-all',
},
Enabled: true,
HttpVersion: 'http2',
IPV6Enabled: true,
Origins: [{
DomainName: 'www.example.com',
Id: 'StackMyDistOrigin1D6D5E535',
CustomOriginConfig: {
OriginProtocolPolicy: 'https-only',
},
OriginAccessControlId: "test-origin-access-control-id"
}],
},
});
});

describe('Distribution metrics tests', () => {
const additionalMetrics = [
{ name: 'OriginLatency', method: 'metricOriginLatency', statistic: 'Average', additionalMetricsRequired: true, errorMetricName: 'Origin latency' },
Expand Down
41 changes: 34 additions & 7 deletions packages/aws-cdk-lib/aws-cloudfront/test/test-origin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Construct } from 'constructs';
import { CfnDistribution, IOrigin, OriginBase, OriginBindConfig, OriginBindOptions, OriginProps, OriginProtocolPolicy } from '../lib';
import { Construct } from "constructs";
import {
CfnDistribution,
IOrigin,
OriginBase,
OriginBindConfig,
OriginBindOptions,
OriginProps,
OriginProtocolPolicy,
} from "../lib";

/** Used for testing common Origin functionality */
export class TestOrigin extends OriginBase {
constructor(domainName: string, props: OriginProps = {}) { super(domainName, props); }
protected renderCustomOriginConfig(): CfnDistribution.CustomOriginConfigProperty | undefined {
constructor(domainName: string, props: OriginProps = {}) {
super(domainName, props);
}
protected renderCustomOriginConfig():
| CfnDistribution.CustomOriginConfigProperty
| undefined {
return { originProtocolPolicy: OriginProtocolPolicy.HTTPS_ONLY };
}
}

export class TestOriginGroup implements IOrigin {
constructor(private readonly primaryDomainName: string, private readonly secondaryDomainName: string) { }
constructor(
private readonly primaryDomainName: string,
private readonly secondaryDomainName: string
) {}
/* eslint-disable @cdklabs/no-core-construct */
public bind(scope: Construct, options: OriginBindOptions): OriginBindConfig {
const primaryOrigin = new TestOrigin(this.primaryDomainName);
Expand All @@ -27,11 +42,23 @@ export class TestOriginGroup implements IOrigin {
}

export function defaultOrigin(domainName?: string, originId?: string): IOrigin {
return new TestOrigin(domainName ?? 'www.example.com', {
return new TestOrigin(domainName ?? "www.example.com", {
originId,
});
}

export function defaultOriginGroup(): IOrigin {
return new TestOriginGroup('www.example.com', 'foo.example.com');
return new TestOriginGroup("www.example.com", "foo.example.com");
}

export function defaultOriginWithOriginAccessControl(
domainName?: string,
originId?: string,
originAccessControlId?: string
): IOrigin {
return new TestOrigin(domainName ?? "www.example.com", {
originId,
originAccessControlId:
originAccessControlId ?? "test-origin-access-control-id",
});
}

0 comments on commit 91da02b

Please sign in to comment.