-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: specify cors options via cli or js
- Loading branch information
1 parent
ac44657
commit 05f12cd
Showing
10 changed files
with
281 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,91 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import {describe, expect, it} from "@jest/globals"; | ||
|
||
import http from 'http'; | ||
import expectedJson from '../app.json'; | ||
import { spawn } from 'child_process'; | ||
import path from 'path'; | ||
import fetch from "node-fetch"; | ||
import * as crypto from "crypto"; | ||
import {startServer} from "./utils"; | ||
|
||
const PORT = '9000'; | ||
|
||
async function makeRequest() { | ||
return new Promise<object>((resolve, reject) => { | ||
http.get(`http://localhost:${PORT}/`, resp => { | ||
let data = ''; | ||
return new Promise<object>((resolve, reject) => { | ||
http.get(`http://localhost:${PORT}/`, resp => { | ||
let data = ''; | ||
|
||
resp.on('data', chunk => { | ||
data += chunk; | ||
}); | ||
resp.on('data', chunk => { | ||
data += chunk; | ||
}); | ||
|
||
resp.on('end', () => { | ||
resolve(JSON.parse(data)); | ||
}); | ||
resp.on('end', () => { | ||
resolve(JSON.parse(data)); | ||
}); | ||
|
||
}).on("error", err => { | ||
console.log("Error: " + err.message); | ||
reject(err); | ||
}).on("error", err => { | ||
console.log("Error: " + err.message); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
describe('Check bin/peerjs', () => { | ||
it('should return content of app.json file', async () => { | ||
expect.assertions(1); | ||
let resolver: () => void; | ||
let rejecter: (err: unknown) => void; | ||
const promise = new Promise<void>((resolve, reject) => { | ||
resolver = resolve; | ||
rejecter = reject; | ||
it('should return content of app.json file', async () => { | ||
expect.assertions(1); | ||
|
||
const ls = await startServer() | ||
try { | ||
const resp = await makeRequest(); | ||
expect(resp).toEqual(expectedJson); | ||
} finally { | ||
ls.kill(); | ||
} | ||
|
||
}); | ||
|
||
const ls = spawn('node', [path.join(__dirname, '../', 'dist/bin/peerjs.js'), '--port', PORT]); | ||
ls.stdout.on('data', async (data: string) => { | ||
if (!data.includes('Started')) return; | ||
|
||
try { | ||
const resp = await makeRequest(); | ||
expect(resp).toEqual(expectedJson); | ||
resolver(); | ||
} catch (error) { | ||
rejecter(error); | ||
} finally { | ||
ls.kill('SIGKILL'); | ||
} | ||
it('should reflect the origin header in CORS by default', async () => { | ||
expect.assertions(1); | ||
|
||
const ls = await startServer() | ||
const origin = crypto.randomUUID(); | ||
try { | ||
const res = await fetch(`http://localhost:${PORT}/peerjs/id`, { | ||
headers: { | ||
Origin: origin | ||
} | ||
}) | ||
expect(res.headers.get("access-control-allow-origin")).toBe(origin) | ||
} finally { | ||
ls.kill() | ||
} | ||
}); | ||
it('should respect the CORS parameters', async () => { | ||
expect.assertions(3); | ||
|
||
return promise; | ||
}); | ||
const origin1 = crypto.randomUUID(); | ||
const origin2 = crypto.randomUUID(); | ||
const origin3 = crypto.randomUUID(); | ||
const ls = await startServer(["--cors", origin1, "--cors", origin2]) | ||
try { | ||
const res1 = await fetch(`http://localhost:${PORT}/peerjs/id`, { | ||
headers: { | ||
Origin: origin1 | ||
} | ||
}) | ||
expect(res1.headers.get("access-control-allow-origin")).toBe(origin1) | ||
const res2 = await fetch(`http://localhost:${PORT}/peerjs/id`, { | ||
headers: { | ||
Origin: origin2 | ||
} | ||
}) | ||
expect(res2.headers.get("access-control-allow-origin")).toBe(origin2) | ||
const res3 = await fetch(`http://localhost:${PORT}/peerjs/id`, { | ||
headers: { | ||
Origin: origin3 | ||
} | ||
}) | ||
expect(res3.headers.get("access-control-allow-origin")).toBe(null) | ||
} finally { | ||
ls.kill() | ||
} | ||
}); | ||
}); |
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 +1,15 @@ | ||
import {ChildProcessWithoutNullStreams, spawn} from "child_process"; | ||
import path from "path"; | ||
|
||
export const wait = (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
export const startServer = (params: string[] = []) => { | ||
return new Promise<ChildProcessWithoutNullStreams>((resolve, reject)=> { | ||
const ls = spawn('node', [path.join(__dirname, '../', 'dist/bin/peerjs.js'), '--port', "9000", ...params]); | ||
ls.stdout.once("data", ()=> resolve(ls)) | ||
ls.stderr.once("data", ()=>{ | ||
ls.kill() | ||
reject() | ||
}) | ||
}) | ||
} |
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,9 +1,13 @@ | ||
/** @type {import('jest').Config} */ | ||
const config = { | ||
transform: { | ||
"^.+\\.(t|j)sx?$": "@swc/jest", | ||
}, | ||
collectCoverageFrom: ["./src/**"] | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+\\.(t|j)sx?$": "@swc/jest", | ||
}, | ||
transformIgnorePatterns: [ | ||
// "node_modules" | ||
], | ||
collectCoverageFrom: ["./src/**"] | ||
}; | ||
|
||
export default config; |
Oops, something went wrong.