-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
213 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,3 +117,6 @@ dist | |
|
||
# Compiled code | ||
lib/ | ||
|
||
# IDEs | ||
.idea |
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
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
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,129 @@ | ||
import { SNSEvent } from 'aws-lambda'; | ||
import { SNSAdapter } from '../../../src/v2/adapters/aws'; | ||
import { Resolver } from '../../../src/v2/contracts'; | ||
import { | ||
EmptyResponse, | ||
getEventBodyAsBuffer, | ||
IEmptyResponse, | ||
ILogger, | ||
} from '../../../src/v2/core'; | ||
import { createCanHandleTestsForAdapter } from '../utils/can-handle'; | ||
import { createSNSEvent } from './utils/sns'; | ||
|
||
describe(SNSAdapter.name, () => { | ||
let adapter!: SNSAdapter; | ||
|
||
beforeEach(() => { | ||
adapter = new SNSAdapter(); | ||
}); | ||
|
||
describe('getAdapterName', () => { | ||
it('should be the same name of the class', () => { | ||
expect(adapter.getAdapterName()).toBe(SNSAdapter.name); | ||
}); | ||
}); | ||
|
||
createCanHandleTestsForAdapter(() => new SNSAdapter(), undefined); | ||
|
||
describe('getRequest', () => { | ||
it('should return the correct mapping for the request', () => { | ||
const event = createSNSEvent(); | ||
|
||
const result = adapter.getRequest(event); | ||
|
||
expect(result.method).toBe('POST'); | ||
expect(result.path).toBe('/sns'); | ||
expect(result.headers).toHaveProperty('host', 'sns.amazonaws.com'); | ||
expect(result.headers).toHaveProperty('content-type', 'application/json'); | ||
|
||
const [bodyBuffer, contentLength] = getEventBodyAsBuffer( | ||
JSON.stringify(event), | ||
false | ||
); | ||
|
||
expect(result.body).toBeInstanceOf(Buffer); | ||
expect(result.body).toStrictEqual(bodyBuffer); | ||
|
||
expect(result.headers).toHaveProperty( | ||
'content-length', | ||
String(contentLength) | ||
); | ||
}); | ||
|
||
it('should return the correct mapping for the request with custom path and method', () => { | ||
const event = createSNSEvent(); | ||
|
||
const method = 'PUT'; | ||
const path = '/custom/sns'; | ||
|
||
const customAdapter = new SNSAdapter({ | ||
snsForwardMethod: method, | ||
snsForwardPath: path, | ||
}); | ||
|
||
const result = customAdapter.getRequest(event); | ||
|
||
expect(result.method).toBe(method); | ||
expect(result.path).toBe(path); | ||
expect(result.headers).toHaveProperty('host', 'sns.amazonaws.com'); | ||
expect(result.headers).toHaveProperty('content-type', 'application/json'); | ||
|
||
const [bodyBuffer, contentLength] = getEventBodyAsBuffer( | ||
JSON.stringify(event), | ||
false | ||
); | ||
|
||
expect(result.body).toBeInstanceOf(Buffer); | ||
expect(result.body).toStrictEqual(bodyBuffer); | ||
|
||
expect(result.headers).toHaveProperty( | ||
'content-length', | ||
String(contentLength) | ||
); | ||
}); | ||
}); | ||
|
||
describe('getResponse', () => { | ||
it('should return the correct mapping for the response', () => { | ||
const result = adapter.getResponse(); | ||
|
||
expect(result).toBe(EmptyResponse); | ||
}); | ||
}); | ||
|
||
describe('onErrorWhileForwarding', () => { | ||
it('should resolver just call fail without get response', () => { | ||
const event = createSNSEvent(); | ||
|
||
const error = new Error('fail because I need to test.'); | ||
const resolver: Resolver<SNSEvent> = { | ||
fail: jest.fn(), | ||
succeed: jest.fn(), | ||
}; | ||
|
||
const oldGetResponse = adapter.getResponse.bind(adapter); | ||
|
||
let getResponseResult: IEmptyResponse; | ||
|
||
adapter.getResponse = jest.fn(() => { | ||
getResponseResult = oldGetResponse(); | ||
|
||
return getResponseResult; | ||
}); | ||
|
||
adapter.onErrorWhileForwarding({ | ||
event, | ||
error, | ||
resolver, | ||
log: {} as ILogger, | ||
respondWithErrors: false, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
expect(adapter.getResponse).toHaveBeenCalledTimes(0); | ||
|
||
expect(resolver.fail).toHaveBeenCalledTimes(1); | ||
expect(resolver.succeed).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
}); |
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
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
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,42 @@ | ||
import { SNSEvent } from 'aws-lambda'; | ||
|
||
/** | ||
* Sample event from {@link https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html} | ||
*/ | ||
export function createSNSEvent(): SNSEvent { | ||
return { | ||
Records: [ | ||
{ | ||
EventVersion: '1.0', | ||
EventSubscriptionArn: | ||
'arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486', | ||
EventSource: 'aws:sns', | ||
Sns: { | ||
SignatureVersion: '1', | ||
Timestamp: '2019-01-02T12:45:07.000Z', | ||
Signature: | ||
'tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==', | ||
SigningCertUrl: | ||
'https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem', | ||
MessageId: '95df01b4-ee98-5cb9-9903-4c221d41eb5e', | ||
Message: 'Hello from SNS!', | ||
MessageAttributes: { | ||
Test: { | ||
Type: 'String', | ||
Value: 'TestString', | ||
}, | ||
TestBinary: { | ||
Type: 'Binary', | ||
Value: 'TestBinary', | ||
}, | ||
}, | ||
Type: 'Notification', | ||
UnsubscribeUrl: | ||
'https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486', | ||
TopicArn: 'arn:aws:sns:us-east-2:123456789012:sns-lambda', | ||
Subject: 'TestInvoke', | ||
}, | ||
}, | ||
], | ||
}; | ||
} |
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