Skip to content

Commit

Permalink
Added static_test
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Jan 4, 2019
1 parent dae135c commit 20b8a1b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dinatra.ts
Expand Up @@ -60,8 +60,10 @@ export class App {
}
return [
200,
// Add content length
detectedContentType(staticFilePath),
{
'Content-Length': fileInfo.len.toString(),
...detectedContentType(staticFilePath),
},
await open(staticFilePath),
];
}
Expand Down
65 changes: 65 additions & 0 deletions static_test.ts
@@ -0,0 +1,65 @@
import { test } from 'https://deno.land/x/testing/testing.ts';
import { exit } from 'deno';
import { assertEqual } from 'https://deno.land/x/pretty_assert@0.1.1/index.ts';
import { App } from './dinatra.ts';

const testPort = '8376';
const host = `http://localhost:${testPort}`;

const app = new App(testPort, 'testdata/static');
app.serve();

const sleep = ms => new Promise(r => setTimeout(r, ms));

(async () => {
interface testCase {
name: string;
path: string;
expected: string;
}

const testCases: Array<testCase> = [
{
name: 'valid normal path',
path: 'index.js',
expected: 'ok',
},
{
name: 'valid index path',
path: '',
expected: 'ok',
},
{
name: 'valid nested normal path',
path: 'nested/index.js',
expected: 'ok',
},
{
name: 'valid nested index path',
path: 'nested',
expected: 'ok',
},
{
name: 'invalid not found',
path: 'nonExistencePath',
expected: 'not found',
},
];

for (const tc of testCases) {
test({
name: tc.name,
async fn() {
await sleep(50);
const res = await fetch(`${host}/${tc.path}`);
const actual = await res.text();
const contentLength = res.headers.get('content-length');
assertEqual(actual, tc.expected);
assertEqual(contentLength, tc.expected.length.toString());
},
});
}

await sleep(500);
exit(0);
})();
1 change: 1 addition & 0 deletions testdata/static/index.html
@@ -0,0 +1 @@
ok
1 change: 1 addition & 0 deletions testdata/static/index.js
@@ -0,0 +1 @@
ok
1 change: 1 addition & 0 deletions testdata/static/nested/index.html
@@ -0,0 +1 @@
ok
1 change: 1 addition & 0 deletions testdata/static/nested/index.js
@@ -0,0 +1 @@
ok

0 comments on commit 20b8a1b

Please sign in to comment.