Skip to content
File

Blob: src/workerd/server/tests/extensions/extensions-test.js

javascript48 lines
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import * as assert from 'node:assert';
6import { openDoor } from 'test:module';
7 
8export const test_module_api = {
9 test() {
10 assert.throws(() => openDoor('test key'));
11 assert.equal(openDoor('0p3n s3sam3'), true);
12 },
13};
14 
15export const test_builtin_dynamic_import = {
16 async test() {
17 await assert.doesNotReject(import('test:module'));
18 },
19};
20 
21// internal modules can't be imported
22export const test_builtin_internal_dynamic_import = {
23 async test() {
24 await assert.rejects(import('test-internal:internal-module'));
25 },
26};
27 
28export const test_wrapped_binding = {
29 async test(ctr, env) {
30 assert.ok(env.door, 'binding is not present');
31 assert.equal(typeof env.door, 'object');
32 assert.ok(env.door.tryOpen);
33 assert.equal(typeof env.door.tryOpen, 'function');
34 
35 // binding uses a different secret specified in the config
36 assert.ok(env.door.tryOpen('open sesame'));
37 assert.ok(!env.door.tryOpen('bad secret'));
38 
39 // check there are no other properties available
40 assert.deepEqual(Object.keys(env.door), ['tryOpen']);
41 assert.deepEqual(Object.getOwnPropertyNames(env.door), ['tryOpen']);
42 
43 assert.ok(env.customDoor, 'custom binding is not present');
44 assert.ok(!env.customDoor.tryOpen('open sesame'));
45 assert.ok(env.customDoor.tryOpen('custom open sesame'));
46 },
47};