|
| 1 | +import { addUserAgentMiddleware, isSdkClient } from '../../src/awsSdk'; |
| 2 | +import { PT_VERSION as version } from '../../src/version'; |
| 3 | +import { customUserAgentMiddleware } from '../../src/awsSdk/userAgentMiddleware'; |
| 4 | + |
| 5 | +describe('Helpers: awsSdk', () => { |
| 6 | + describe('Function: userAgentMiddleware', () => { |
| 7 | + beforeAll(() => { |
| 8 | + jest.spyOn(console, 'warn').mockImplementation(() => ({})); |
| 9 | + }); |
| 10 | + |
| 11 | + it('handles gracefully failures in adding a middleware and only log a warning', () => { |
| 12 | + // Prepare |
| 13 | + const client = { |
| 14 | + middlewareStack: { |
| 15 | + addRelativeTo: () => { |
| 16 | + throw new Error('test'); |
| 17 | + }, |
| 18 | + }, |
| 19 | + }; |
| 20 | + const warningSpy = jest |
| 21 | + .spyOn(console, 'warn') |
| 22 | + .mockImplementation(() => ({})); |
| 23 | + |
| 24 | + // Act & Assess |
| 25 | + expect(() => addUserAgentMiddleware(client, 'my-feature')).not.toThrow(); |
| 26 | + expect(warningSpy).toHaveBeenCalledTimes(1); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return and do nothing if the client already has a Powertools UA middleware', async () => { |
| 30 | + // Prepare |
| 31 | + const client = { |
| 32 | + middlewareStack: { |
| 33 | + identify: () => 'addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT', |
| 34 | + addRelativeTo: jest.fn(), |
| 35 | + }, |
| 36 | + send: jest.fn(), |
| 37 | + config: { |
| 38 | + defaultSigningName: 'bar', |
| 39 | + }, |
| 40 | + }; |
| 41 | + const feature = 'my-feature'; |
| 42 | + |
| 43 | + // Act |
| 44 | + addUserAgentMiddleware(client, feature); |
| 45 | + |
| 46 | + // Assess |
| 47 | + expect(client.middlewareStack.addRelativeTo).toHaveBeenCalledTimes(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should call the function to add the middleware with the correct arguments', async () => { |
| 51 | + // Prepare |
| 52 | + const client = { |
| 53 | + middlewareStack: { |
| 54 | + identify: () => '', |
| 55 | + addRelativeTo: jest.fn(), |
| 56 | + }, |
| 57 | + send: jest.fn(), |
| 58 | + config: { |
| 59 | + defaultSigningName: 'bar', |
| 60 | + }, |
| 61 | + }; |
| 62 | + const feature = 'my-feature'; |
| 63 | + |
| 64 | + // Act |
| 65 | + addUserAgentMiddleware(client, feature); |
| 66 | + |
| 67 | + // Assess |
| 68 | + expect(client.middlewareStack.addRelativeTo).toHaveBeenNthCalledWith( |
| 69 | + 1, |
| 70 | + expect.any(Function), |
| 71 | + { |
| 72 | + relation: 'after', |
| 73 | + toMiddleware: 'getUserAgentMiddleware', |
| 74 | + name: 'addPowertoolsToUserAgent', |
| 75 | + tags: ['POWERTOOLS', 'USER_AGENT'], |
| 76 | + } |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('Function: customUserAgentMiddleware', () => { |
| 82 | + it('returns a middleware function', () => { |
| 83 | + // Prepare |
| 84 | + const feature = 'my-feature'; |
| 85 | + |
| 86 | + // Act |
| 87 | + const middleware = customUserAgentMiddleware(feature); |
| 88 | + |
| 89 | + // Assess |
| 90 | + expect(middleware).toBeInstanceOf(Function); |
| 91 | + }); |
| 92 | + |
| 93 | + it('adds the Powertools UA to the request headers', async () => { |
| 94 | + // Prepare |
| 95 | + const feature = 'my-feature'; |
| 96 | + const middleware = customUserAgentMiddleware(feature); |
| 97 | + const next = jest.fn(); |
| 98 | + const args = { |
| 99 | + request: { |
| 100 | + headers: { |
| 101 | + 'user-agent': 'foo', |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + // Act |
| 107 | + await middleware(next)(args); |
| 108 | + |
| 109 | + // Assess |
| 110 | + expect(args.request.headers['user-agent']).toEqual( |
| 111 | + `foo PT/my-feature/${version} PTEnv/NA` |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('Function: isSdkClient', () => { |
| 117 | + it('returns true if the client is a valid AWS SDK v3 client', () => { |
| 118 | + // Prepare |
| 119 | + const client = { |
| 120 | + send: jest.fn(), |
| 121 | + config: { |
| 122 | + defaultSigningName: 'bar', |
| 123 | + }, |
| 124 | + middlewareStack: { |
| 125 | + identify: () => '', |
| 126 | + addRelativeTo: jest.fn(), |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + // Act |
| 131 | + const result = isSdkClient(client); |
| 132 | + |
| 133 | + // Assess |
| 134 | + expect(result).toEqual(true); |
| 135 | + }); |
| 136 | + |
| 137 | + it('returns false if the client is not a valid AWS SDK v3 client', () => { |
| 138 | + // Prepare |
| 139 | + const client = {}; |
| 140 | + |
| 141 | + // Act |
| 142 | + const result = isSdkClient(client); |
| 143 | + |
| 144 | + // Assess |
| 145 | + expect(result).toEqual(false); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments