|
| 1 | +import { Context } from 'aws-lambda'; |
| 2 | +import o from 'ospec'; |
| 3 | +import { LambdaUrlRequest } from '../http/request.function.js'; |
| 4 | +import { AlbExample, ApiGatewayExample, clone, CloudfrontExample, UrlExample } from './examples.js'; |
| 5 | +import { fakeLog } from './log.js'; |
| 6 | + |
| 7 | +o.spec('FunctionUrl', () => { |
| 8 | + const fakeContext = {} as Context; |
| 9 | + |
| 10 | + o('should match the event', () => { |
| 11 | + o(LambdaUrlRequest.is(ApiGatewayExample)).equals(false); |
| 12 | + o(LambdaUrlRequest.is(CloudfrontExample)).equals(false); |
| 13 | + o(LambdaUrlRequest.is(AlbExample)).equals(false); |
| 14 | + o(LambdaUrlRequest.is(UrlExample)).equals(true); |
| 15 | + }); |
| 16 | + |
| 17 | + o('should extract headers', () => { |
| 18 | + const req = new LambdaUrlRequest(UrlExample, fakeContext, fakeLog); |
| 19 | + |
| 20 | + o(req.header('accept-encoding')).equals('br,gzip'); |
| 21 | + o(req.header('Accept-Encoding')).equals('br,gzip'); |
| 22 | + }); |
| 23 | + |
| 24 | + o('should extract methods', () => { |
| 25 | + const req = new LambdaUrlRequest(UrlExample, fakeContext, fakeLog); |
| 26 | + o(req.method).equals('GET'); |
| 27 | + }); |
| 28 | + |
| 29 | + o('should upper case method', () => { |
| 30 | + const newReq = clone(UrlExample); |
| 31 | + newReq.requestContext.http.method = 'post'; |
| 32 | + const req = new LambdaUrlRequest(newReq, fakeContext, fakeLog); |
| 33 | + o(req.method).equals('POST'); |
| 34 | + }); |
| 35 | + |
| 36 | + o('should extract query parameters', () => { |
| 37 | + const newReq = clone(UrlExample); |
| 38 | + newReq.rawQueryString = 'api=abc123'; |
| 39 | + |
| 40 | + const req = new LambdaUrlRequest(newReq, fakeContext, fakeLog); |
| 41 | + o(req.query.get('api')).deepEquals('abc123'); |
| 42 | + o(req.query.getAll('api')).deepEquals(['abc123']); |
| 43 | + }); |
| 44 | + |
| 45 | + o('should support utf8 paths and query', () => { |
| 46 | + const req = new LambdaUrlRequest(UrlExample, fakeContext, fakeLog); |
| 47 | + o(req.path).equals('/v1/🦄/🌈/🦄.json'); |
| 48 | + o(req.query.get('🦄')).equals('abc123'); |
| 49 | + }); |
| 50 | +}); |
0 commit comments