-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for Cloudflare Workers.
- Loading branch information
Showing
6 changed files
with
2,784 additions
and
0 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
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 |
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,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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.