[Test] Add streaming test

#3955
This commit is contained in:
syuilo 2019-01-22 18:30:49 +09:00
parent c49ae672f2
commit 26c327145f
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69

View file

@ -1,10 +1,17 @@
/* /*
* Tests of API * Tests of API
*
* How to run the tests:
* > mocha test/api.ts --require ts-node/register
*
* To specify test:
* > mocha test/api.ts --require ts-node/register -g 'test name'
*/ */
import * as http from 'http'; import * as http from 'http';
import * as fs from 'fs'; import * as fs from 'fs';
import * as assert from 'chai'; import * as assert from 'chai';
import * as WebSocket from 'ws';
assert.use(require('chai-http')); assert.use(require('chai-http'));
const expect = assert.expect; const expect = assert.expect;
@ -20,6 +27,7 @@ process.on('unhandledRejection', console.dir);
//#endregion //#endregion
const app = require('../built/server/api').default; const app = require('../built/server/api').default;
require('../built/server').default();
const db = require('../built/db/mongodb').default; const db = require('../built/db/mongodb').default;
const server = http.createServer(app.callback()); const server = http.createServer(app.callback());
@ -1231,4 +1239,39 @@ describe('API', () => {
expect(res).have.status(400); expect(res).have.status(400);
})); }));
}); });
describe('streaming', () => {
it('投稿がタイムラインに流れる', done => {
const post = {
text: 'foo'
};
signup().then(me => {
const ws = new WebSocket(`ws://localhost/streaming?i=${me.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'note') {
expect(msg.body.body.text).eql(post.text);
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', post, me);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: 'a',
pong: true
}
}));
});
});
});
});
}); });