-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* t * wip
- Loading branch information
Showing
3 changed files
with
111 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
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,48 @@ | ||
/* | ||
* Tests for launce server | ||
* | ||
* How to run the tests: | ||
* > TS_NODE_FILES=true npx mocha test/launch.ts --require ts-node/register | ||
* | ||
* To specify test: | ||
* > TS_NODE_FILES=true npx mocha test/launch.ts --require ts-node/register -g 'test name' | ||
*/ | ||
|
||
process.env.NODE_ENV = 'test'; | ||
|
||
import * as assert from 'assert'; | ||
import * as childProcess from 'child_process'; | ||
import { async, launchServer, api, get } from './utils'; | ||
|
||
describe('Launch server', () => { | ||
let p: childProcess.ChildProcess; | ||
|
||
before(launchServer(g => p = g, async () => { | ||
})); | ||
|
||
after(() => { | ||
p.kill(); | ||
}); | ||
|
||
it('meta', async(async () => { | ||
const res = await api('meta', { | ||
}); | ||
|
||
assert.strictEqual(res.status, 200); | ||
})); | ||
|
||
it('GET root', async(async () => { | ||
const res = await get('/'); | ||
assert.strictEqual(res.status, 200); | ||
})); | ||
|
||
it('GET docs', async(async () => { | ||
const res = await get('/docs/ja-JP/about'); | ||
assert.strictEqual(res.status, 200); | ||
})); | ||
|
||
it('GET api-doc', async(async () => { | ||
const res = await get('/api-doc'); | ||
assert.strictEqual(res.status, 200); | ||
})); | ||
}); |
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,7 +1,57 @@ | ||
import * as childProcess from 'child_process'; | ||
import fetch from 'node-fetch'; | ||
|
||
export const async = (fn: Function) => (done: Function) => { | ||
fn().then(() => { | ||
done(); | ||
}, (err: Error) => { | ||
done(err); | ||
}); | ||
}; | ||
|
||
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) { | ||
return (done: (err?: Error) => any) => { | ||
const p = childProcess.spawn('node', [__dirname + '/../index.js'], { | ||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'], | ||
env: { NODE_ENV: 'test', PATH: process.env.PATH } | ||
}); | ||
callbackSpawnedProcess(p); | ||
p.on('message', message => { | ||
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e)); | ||
}); | ||
}; | ||
} | ||
|
||
export const api = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => { | ||
const auth = me ? { | ||
i: me.token | ||
} : {}; | ||
|
||
const res = await fetch('http://localhost:8080/api/' + endpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(Object.assign(auth, params)) | ||
}); | ||
|
||
const status = res.status; | ||
const body = res.status !== 204 ? await res.json().catch() : null; | ||
|
||
return { | ||
status, | ||
body | ||
}; | ||
}; | ||
|
||
export const get = async (path: string): Promise<{ status: number }> => { | ||
const res = await fetch(`http://localhost:8080${path}`, { | ||
method: 'GET', | ||
}); | ||
|
||
const status = res.status; | ||
|
||
return { | ||
status | ||
}; | ||
}; |