-
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.
fix: fix security issue and add tests
- Loading branch information
Showing
4 changed files
with
105 additions
and
30 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
<h1>It works!</h1> |
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,64 @@ | ||
import { test, describe } from 'node:test'; | ||
import assert from 'node:assert/strict'; | ||
import { serve } from '../src/httpd.js'; | ||
|
||
|
||
|
||
describe('httpd tests', () => { | ||
|
||
let counter = 0; | ||
|
||
function getListenOptions() { | ||
const port = 11551 + counter++; // rolled a dice. | ||
const host = 'localhost'; | ||
const killSwitch = new AbortController(); | ||
return [killSwitch, {host, port, signal: killSwitch.signal}] | ||
} | ||
|
||
test('basic functionality', async () => { | ||
const [killSwitch, options] = getListenOptions(); | ||
await serve(null, 'tests/fixtures/httpd', options); | ||
|
||
const requestTests = [ | ||
{ | ||
url: '/', | ||
expected: [200, 'text/html', '<h1>It works!</h1>\n'], | ||
}, | ||
{ | ||
url: '/favicon.svg', | ||
expected: [200, 'image/svg+xml', '<svg></svg>\n'], | ||
}, | ||
{ | ||
url: '/../../../../etc/passwd', | ||
expected: [404, 'text/html', '404 Not Found'], | ||
}, | ||
{ | ||
url: '/_dev-events.js', | ||
expected: [200, 'text/javascript'], | ||
}, | ||
{ | ||
url: '/_dev-events', | ||
expected: [404, 'text/html', '404 Not Found'], | ||
} | ||
]; | ||
|
||
for (const reqTest of requestTests) { | ||
const response = await fetch(`http://${options.host}:${options.port}${reqTest.url}`); | ||
const text = await response.text(); | ||
reqTest.actual = [response.status, response.headers.get('content-type'), text]; | ||
} | ||
|
||
killSwitch.abort(); | ||
|
||
for (const reqTest of requestTests) { | ||
const [actualCode, actualType, actualBody] = reqTest.actual; | ||
const [expectedCode, expectedType, expectedBody] = reqTest.expected; | ||
|
||
assert.equal(actualCode, expectedCode, new Error(`request ${reqTest.url} should return ${expectedCode}, is ${actualCode}`)); | ||
assert.equal(actualType, expectedType); | ||
if (expectedBody) { | ||
assert.equal(actualBody, expectedBody); | ||
} | ||
} | ||
}); | ||
}); |