@@ -10,6 +10,7 @@ import { createServer } from '../../src/runtime/server';
10
10
const TEST_DIR = resolve ( __dirname , '../../fixtures' ) ;
11
11
12
12
const TEST_FUNCTIONS_DIR = resolve ( TEST_DIR , 'functions' ) ;
13
+ const TEST_ASSETS_DIR = resolve ( TEST_DIR , 'assets' ) ;
13
14
const TEST_ENV = { } ;
14
15
15
16
const availableFunctions = readdirSync ( TEST_FUNCTIONS_DIR ) . map (
@@ -19,6 +20,11 @@ const availableFunctions = readdirSync(TEST_FUNCTIONS_DIR).map(
19
20
return { name, url, path } ;
20
21
}
21
22
) ;
23
+ const availableAssets = readdirSync ( TEST_ASSETS_DIR ) . map ( ( name : string ) => {
24
+ const path = resolve ( TEST_ASSETS_DIR , name ) ;
25
+ const url = `/${ name } ` ;
26
+ return { name, url, path } ;
27
+ } ) ;
22
28
23
29
type InternalResponse = request . Response & {
24
30
statusCode : number ;
@@ -30,6 +36,7 @@ type InternalResponse = request.Response & {
30
36
function responseToSnapshotJson ( response : InternalResponse ) {
31
37
let { statusCode, type, body, text, headers } = response ;
32
38
delete headers [ 'date' ] ;
39
+ delete headers [ 'last-modified' ] ;
33
40
34
41
if ( text && text . startsWith ( 'Error' ) ) {
35
42
// stack traces are different in every environment
@@ -48,7 +55,7 @@ function responseToSnapshotJson(response: InternalResponse) {
48
55
} ;
49
56
}
50
57
51
- describe ( 'Function integration tests ' , ( ) => {
58
+ describe ( 'with an express app ' , ( ) => {
52
59
let app : Express ;
53
60
54
61
beforeAll ( async ( ) => {
@@ -59,15 +66,68 @@ describe('Function integration tests', () => {
59
66
} as StartCliConfig ) ;
60
67
} ) ;
61
68
62
- for ( const testFnCode of availableFunctions ) {
63
- test ( `${ testFnCode . name } should match snapshot` , async ( ) => {
64
- const response = await request ( app ) . get ( testFnCode . url ) ;
65
- if ( response . status === 500 ) {
66
- expect ( response . text ) . toMatch ( / E r r o r / ) ;
67
- } else {
69
+ describe ( 'Function integration tests' , ( ) => {
70
+ for ( const testFnCode of availableFunctions ) {
71
+ test ( `${ testFnCode . name } should match snapshot` , async ( ) => {
72
+ const response = await request ( app ) . get ( testFnCode . url ) ;
73
+ if ( response . status === 500 ) {
74
+ expect ( response . text ) . toMatch ( / E r r o r / ) ;
75
+ } else {
76
+ const result = responseToSnapshotJson ( response as InternalResponse ) ;
77
+ expect ( result ) . toMatchSnapshot ( ) ;
78
+ }
79
+ } ) ;
80
+ }
81
+ } ) ;
82
+
83
+ describe ( 'Assets integration tests' , ( ) => {
84
+ for ( const testAsset of availableAssets ) {
85
+ test ( `${ testAsset . name } should match snapshot` , async ( ) => {
86
+ const response = await request ( app ) . get ( testAsset . url ) ;
68
87
const result = responseToSnapshotJson ( response as InternalResponse ) ;
69
88
expect ( result ) . toMatchSnapshot ( ) ;
70
- }
71
- } ) ;
72
- }
89
+ } ) ;
90
+
91
+ test ( `OPTIONS request to ${ testAsset . name } should return CORS headers and no body` , async ( ) => {
92
+ const response = ( await request ( app ) . options (
93
+ testAsset . url
94
+ ) ) as InternalResponse ;
95
+ expect ( response . headers [ 'access-control-allow-origin' ] ) . toEqual ( '*' ) ;
96
+ expect ( response . headers [ 'access-control-allow-headers' ] ) . toEqual (
97
+ 'Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since'
98
+ ) ;
99
+ expect ( response . headers [ 'access-control-allow-methods' ] ) . toEqual (
100
+ 'GET, POST, OPTIONS'
101
+ ) ;
102
+ expect ( response . headers [ 'access-control-expose-headers' ] ) . toEqual (
103
+ 'ETag'
104
+ ) ;
105
+ expect ( response . headers [ 'access-control-max-age' ] ) . toEqual ( '86400' ) ;
106
+ expect ( response . headers [ 'access-control-allow-credentials' ] ) . toEqual (
107
+ 'true'
108
+ ) ;
109
+ expect ( response . text ) . toEqual ( '' ) ;
110
+ } ) ;
111
+
112
+ test ( `GET request to ${ testAsset . name } should not return CORS headers` , async ( ) => {
113
+ const response = ( await request ( app ) . get (
114
+ testAsset . url
115
+ ) ) as InternalResponse ;
116
+ expect ( response . headers [ 'access-control-allow-origin' ] ) . toBeUndefined ( ) ;
117
+ expect (
118
+ response . headers [ 'access-control-allow-headers' ]
119
+ ) . toBeUndefined ( ) ;
120
+ expect (
121
+ response . headers [ 'access-control-allow-methods' ]
122
+ ) . toBeUndefined ( ) ;
123
+ expect (
124
+ response . headers [ 'access-control-expose-headers' ]
125
+ ) . toBeUndefined ( ) ;
126
+ expect ( response . headers [ 'access-control-max-age' ] ) . toBeUndefined ( ) ;
127
+ expect (
128
+ response . headers [ 'access-control-allow-credentials' ]
129
+ ) . toBeUndefined ( ) ;
130
+ } ) ;
131
+ }
132
+ } ) ;
73
133
} ) ;
0 commit comments