-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] add tests to edge handler template (#9528)
The Edge Handler wrapping logic we add during `vc dev` was not testable. This PR refactors it to be testable and adds some basic tests.
- Loading branch information
1 parent
765ec05
commit 66fe3db
Showing
5 changed files
with
158 additions
and
56 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["*.test.ts"] | ||
"include": ["**/*.test.ts"] | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/node/test/unit/edge-functions/edge-handler-template.test.ts
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,57 @@ | ||
import { Response, Request } from 'node-fetch'; | ||
import { | ||
buildUrl, | ||
respond, | ||
// @ts-ignore - this is a special patch file to allow importing from the template | ||
} from '../../../src/edge-functions/edge-handler-template.js'; | ||
|
||
describe('edge-handler-template', () => { | ||
describe('buildUrl()', () => { | ||
test('works with basic proto', async () => { | ||
const url = buildUrl({ | ||
url: '/api/add', | ||
headers: { | ||
'x-forwarded-proto': 'https', | ||
'x-forwarded-host': 'somewhere.com', | ||
}, | ||
}); | ||
expect(url).toBe('https://somewhere.com/api/add'); | ||
}); | ||
}); | ||
|
||
describe('respond()', () => { | ||
test('works', async () => { | ||
const request = { | ||
url: '/api/add', | ||
headers: { | ||
'x-forwarded-proto': 'https', | ||
'x-forwarded-host': 'somewhere.com', | ||
}, | ||
}; | ||
|
||
function userEdgeHandler(req: Request) { | ||
return new Response(`hello from: ${req.url}`); | ||
} | ||
|
||
const event = {}; | ||
const isMiddleware = false; | ||
const entrypointLabel = 'api/add.js'; | ||
const response = await respond( | ||
userEdgeHandler, | ||
request, | ||
event, | ||
{ | ||
isMiddleware, | ||
entrypointLabel, | ||
}, | ||
{ | ||
Request, | ||
Response, | ||
} | ||
); | ||
expect(await response.text()).toBe( | ||
'hello from: https://somewhere.com/api/add' | ||
); | ||
}); | ||
}); | ||
}); |