Skip to content

Commit

Permalink
Add test for Cloudflare Workers.
Browse files Browse the repository at this point in the history
Add test for Cloudflare Workers.
  • Loading branch information
dajiaji authored Nov 12, 2022
2 parents 2782936 + 92c0c61 commit 910c941
Show file tree
Hide file tree
Showing 6 changed files with 2,784 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci_cfw.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Cloudflare Workers CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/setup-node@v3
with:
node-version: v18.x
- run: deno bundle mod.ts test/wrangler/src/bhttp.js
- name: Run test
working-directory: ./test/wrangler
run: |
npm install
nohup npm start &
sleep 3
deno test bhttp.spec.ts --allow-net
103 changes: 103 additions & 0 deletions test/wrangler/bhttp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { assertEquals } from "testing/asserts.ts";
import { describe, it } from "testing/bdd.ts";

import { BHttpDecoder, BHttpEncoder } from "../../mod.ts";

describe("Cloudflare Workers", () => {
describe("GET", () => {
it("200 OK", async () => {
const encoder = new BHttpEncoder();
const req = new Request("https://ogr.example/query?foo=bar");
const bReq = await encoder.encodeRequest(req);
const res = await fetch("http://localhost:8787/test1", {
method: "POST",
headers: {
"Content-Type": "message/bhttp",
},
body: bReq,
});

assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), "message/bhttp");
const decoder = new BHttpDecoder();
const decodedRes = decoder.decodeResponse(
new Uint8Array(await res.arrayBuffer()),
);
assertEquals(200, decodedRes.status);
assertEquals("baz", await decodedRes.text());
});

it("404 Not Found", async () => {
const encoder = new BHttpEncoder();
const req = new Request("https://ogr.example/query?foo=bar");
const bReq = await encoder.encodeRequest(req);
const res = await fetch("http://localhost:8787/testx", {
method: "POST",
headers: {
"Content-Type": "message/bhttp",
},
body: bReq,
});

assertEquals(res.status, 404);
assertEquals(res.headers.get("content-type"), "message/bhttp");
const decoder = new BHttpDecoder();
const decodedRes = decoder.decodeResponse(
new Uint8Array(await res.arrayBuffer()),
);
assertEquals(404, decodedRes.status);
});
});

describe("POST", () => {
it("201 Created", async () => {
const encoder = new BHttpEncoder();
const req = new Request("https://ogr.example/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: '{"user": "me"}',
});
const bReq = await encoder.encodeRequest(req);
const res = await fetch("http://localhost:8787/test2", {
method: "POST",
headers: {
"Content-Type": "message/bhttp",
},
body: bReq,
});

assertEquals(res.status, 201);
assertEquals(res.headers.get("content-type"), "message/bhttp");
const decoder = new BHttpDecoder();
const decodedRes = decoder.decodeResponse(
new Uint8Array(await res.arrayBuffer()),
);
assertEquals(201, decodedRes.status);
});

it("404 Not Found", async () => {
const encoder = new BHttpEncoder();
const req = new Request("https://ogr.example/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: '{"user": "me"}',
});
const bReq = await encoder.encodeRequest(req);
const res = await fetch("http://localhost:8787/testx", {
method: "POST",
headers: {
"Content-Type": "message/bhttp",
},
body: bReq,
});

assertEquals(res.status, 404);
assertEquals(res.headers.get("content-type"), "message/bhttp");
const decoder = new BHttpDecoder();
const decodedRes = decoder.decodeResponse(
new Uint8Array(await res.arrayBuffer()),
);
assertEquals(404, decodedRes.status);
});
});
});
Loading

0 comments on commit 910c941

Please sign in to comment.