Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra app names #332

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/microapps-datalib/src/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('application records', () => {

await application.Save(dbManager);

//
// Single item key
//
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Expand All @@ -45,6 +48,9 @@ describe('application records', () => {
expect(Item?.DisplayName).toBe('Dog');
}

//
// List key
//
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Expand All @@ -59,6 +65,96 @@ describe('application records', () => {
}
});

it.only('saving an application with two extra appnames should create four records', async () => {
const application = new Application();
application.AppName = 'Cat';
application.DisplayName = 'Dog';
application.ExtraAppNames = ['Kitty', 'Kitten'];

await application.Save(dbManager);

//
// Single item key
//

// Get the primary app record
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'appname#cat', SK: 'application' },
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('appname#cat');
expect(Item?.SK).toBe('application');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}

// Get the secondary app records
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'appname#kitty', SK: 'application' },
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('appname#kitty');
expect(Item?.SK).toBe('application');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'appname#kitten', SK: 'application' },
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('appname#kitten');
expect(Item?.SK).toBe('application');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}

//
// List key
//
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'applications', SK: 'appname#cat' },
// ProjectionExpression: 'PK,SK,AppName,DisplayName',
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('applications');
expect(Item?.SK).toBe('appname#cat');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'applications', SK: 'appname#keey' },
// ProjectionExpression: 'PK,SK,AppName,DisplayName',
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('applications');
expect(Item?.SK).toBe('appname#kitty');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}
{
const { Item } = await dbManager.ddbDocClient.get({
TableName: TEST_TABLE_NAME,
Key: { PK: 'applications', SK: 'appname#kitten' },
// ProjectionExpression: 'PK,SK,AppName,DisplayName',
});
expect(Item).toBeDefined();
expect(Item?.PK).toBe('applications');
expect(Item?.SK).toBe('appname#kitten');
expect(Item?.AppName).toBe('cat');
expect(Item?.DisplayName).toBe('Dog');
}
});

it('load function should load records', async () => {
let application = new Application();
application.AppName = 'App1';
Expand Down
10 changes: 10 additions & 0 deletions packages/microapps-datalib/src/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface IApplicationRecord {
SK: string;
AppName: string;
DisplayName: string;
ExtraAppNames: string[];
}

export class Application implements IApplicationRecord {
Expand Down Expand Up @@ -96,6 +97,7 @@ export class Application implements IApplicationRecord {
private _keyBy: SaveBy;
private _appName: string | undefined;
private _displayName: string | undefined;
private _extraAppNames: string[] | undefined;

public constructor(init?: Partial<IApplicationRecord>) {
Object.assign(this, init);
Expand All @@ -108,6 +110,7 @@ export class Application implements IApplicationRecord {
SK: this.SK,
AppName: this.AppName,
DisplayName: this.DisplayName,
ExtraAppNames: this._extraAppNames ?? [],
};
}

Expand Down Expand Up @@ -170,4 +173,11 @@ export class Application implements IApplicationRecord {
public set DisplayName(value: string) {
this._displayName = value;
}

public get ExtraAppNames(): string[] {
return this._extraAppNames as string[];
}
public set ExtraAppNames(value: string[]) {
this._extraAppNames = value;
}
}
103 changes: 103 additions & 0 deletions packages/microapps-edge-to-origin/src/index.route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,15 @@ describe('edge-to-origin - routing - without prefix', () => {
const AppName = 'BatDirect';
const SemVer = '1.2.1-beta.1';
const Locale = 'sv';
// it('should route `direct` app request with *additional* appName to origin for actual appName', async () => {
// theConfig.replaceHostHeader = true;
// const AppName = 'BatDirect';
// const SemVer = '1.2.1-beta.1';
// const AppNameExtraRoute = 'BatDirectExtraRoute';

const app = new Application({
AppName,
// ExtraAppNames: [AppNameExtraRoute],
DisplayName: 'Direct Bat App',
});
await app.Save(dbManager);
Expand Down Expand Up @@ -442,6 +448,103 @@ describe('edge-to-origin - routing - without prefix', () => {
querystring: '',
clientIp: '1.1.1.1',
uri: `/${Locale}/${AppName.toLowerCase()}`,
// uri: `/${AppNameExtraRoute.toLowerCase()}`,
origin: {
custom: {
customHeaders: {},
domainName: 'zyz.cloudfront.net',
keepaliveTimeout: 5,
path: '',
port: 443,
protocol: 'https',
readTimeout: 30,
sslProtocols: ['TLSv1.2'],
},
},
},
},
},
],
} as lambda.CloudFrontRequestEvent,
{} as lambda.Context,
);

const requestResponse = response as lambda.CloudFrontRequest;
expect(requestResponse).toBeDefined();
expect(requestResponse).not.toHaveProperty('status');
expect(requestResponse).not.toHaveProperty('body');
expect(requestResponse).toHaveProperty('headers');
expect(requestResponse.headers['x-microapps-appname'][0].key).toBe('X-MicroApps-AppName');
expect(requestResponse.headers['x-microapps-appname'][0].value).toBe(AppName.toLowerCase());
expect(requestResponse.headers).toHaveProperty('x-microapps-semver');
expect(requestResponse.headers['x-microapps-semver'][0].key).toBe('X-MicroApps-SemVer');
expect(requestResponse.headers['x-microapps-semver'][0].value).toBe(SemVer);
expect(requestResponse.headers).toHaveProperty('host');
expect(requestResponse.headers.host).toHaveLength(1);
expect(requestResponse.headers.host[0].key).toBe('Host');
expect(requestResponse.headers.host[0].value).toBe('abc123.lambda-url.us-east-1.on.aws');
expect(requestResponse).toHaveProperty('origin');
expect(requestResponse.origin).toHaveProperty('custom');
expect(requestResponse?.origin?.custom).toHaveProperty('domainName');
expect(requestResponse?.origin?.custom?.domainName).toBe('abc123.lambda-url.us-east-1.on.aws');
});

it('should route `direct` app request with *additional* appName to origin for actual appName', async () => {
theConfig.replaceHostHeader = true;
const AppName = 'BatDirect';
const SemVer = '1.2.1-beta.1';
const AppNameExtraRoute = 'BatDirectExtraRoute';

const app = new Application({
AppName,
ExtraAppNames: [AppNameExtraRoute],
DisplayName: 'Direct Bat App',
});
await app.Save(dbManager);

const version = new Version({
AppName,
SemVer,
Status: 'deployed',
Type: 'lambda-url',
StartupType: 'direct',
URL: 'https://abc123.lambda-url.us-east-1.on.aws/',
});
await version.Save(dbManager);

const rules = new Rules({
AppName,
Version: 0,
RuleSet: { default: { SemVer, AttributeName: '', AttributeValue: '' } },
});
await rules.Save(dbManager);

// Call the handler
// @ts-expect-error no callback
const response = await handler(
{
Records: [
{
cf: {
config: {
distributionDomainName: 'zyz.cloudfront.net',
distributionId: '123',
eventType: 'origin-request',
requestId: '123',
},
request: {
headers: {
host: [
{
key: 'Host',
value: 'zyz.cloudfront.net',
},
],
},
method: 'GET',
querystring: '',
clientIp: '1.1.1.1',
uri: `/${AppNameExtraRoute.toLowerCase()}`,
origin: {
custom: {
customHeaders: {},
Expand Down
Loading