This repository was archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
50 lines (44 loc) · 1.71 KB
/
test.js
File metadata and controls
50 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const fs = require('fs').promises;
const path = require('path');
const Cloudworker = require('@dollarshaveclub/cloudworker');
const wasm = require('@dollarshaveclub/cloudworker/lib/wasm');
const test = require('ava');
const scriptPath = path.join(__dirname, 'build', 'webpack', 'worker.js');
const wasmPath = path.join(__dirname, 'build', 'webpack', 'worker.wasm');
test.beforeEach(t => {
return Promise
.all([fs.readFile(scriptPath, 'utf8'), wasm.loadPath(wasmPath)])
.then(values => {
const [script, wasm] = values;
const bindings = {wasm: wasm};
t.context.cw = new Cloudworker(script, {bindings});
})
});
test('get: successful responses', async t => {
const getReq = () => new Cloudworker.Request('https://example.com/')
const res1 = await t.context.cw.dispatch(getReq());
t.is(res1.status, 200);
t.is(await res1.text(), 'Hello from Haskell')
const res2 = await t.context.cw.dispatch(getReq());
t.is(res2.status, 200);
t.is(await res2.text(), 'Hello from Haskell');
});
test('post: successful responses', async t => {
const postReq = (name) => new Cloudworker.Request(
'https://example.com/',
{method: "POST", body: JSON.stringify({name: name})});
const res1 = await t.context.cw.dispatch(postReq('cloudflare'));
t.is(res1.status, 200);
t.is(await res1.text(), 'Hello cloudflare');
const res2 = await t.context.cw.dispatch(postReq('asterius'));
t.is(res2.status, 200);
t.is(await res2.text(), 'Hello asterius');
});
test("post: missing name", async t => {
const req = new Cloudworker.Request(
'https://example.com/',
{method: "POST", body: '{"hello": "cloudflare"}'})
const res = await t.context.cw.dispatch(req);
t.is(res.status, 400);
});