[rpc/http]: properly accomplish tests, fix http sender returning trash

This commit is contained in:
41666 2019-06-04 17:57:40 -05:00
parent 36819e33af
commit 75473ad4f7
3 changed files with 27 additions and 21 deletions

View file

@ -58,7 +58,7 @@ export default class HTTPTransport extends Transport {
req.on('end', async () => { req.on('end', async () => {
const o = await this.receiver({ buffer: txtEnc.encode(buf), ctx: this.getContext(req, res) }) const o = await this.receiver({ buffer: txtEnc.encode(buf), ctx: this.getContext(req, res) })
res.statusCode = 200 res.statusCode = 200
res.end(o) res.end(txtDec.decode(o))
}) })
} }
@ -143,6 +143,7 @@ export default class HTTPTransport extends Transport {
.set('User-Agent', 'roleypoly/2.0 bento http client (+https://roleypoly.com)') .set('User-Agent', 'roleypoly/2.0 bento http client (+https://roleypoly.com)')
.withCredentials() .withCredentials()
.set(this.injectHeaders) .set(this.injectHeaders)
.ok(() => true)
if (c.length > 0) { if (c.length > 0) {
r.set('Cookie', c) r.set('Cookie', c)
@ -150,6 +151,6 @@ export default class HTTPTransport extends Transport {
const res = await r const res = await r
return Buffer.from(res.body) return txtEnc.encode(res.text)
} }
} }

View file

@ -3,32 +3,34 @@ import Bento, { JSONSerializer } from '@kayteh/bento'
import { MockBackendClient } from './mock.bento' import { MockBackendClient } from './mock.bento'
import MockBackendServer from './mock-server' import MockBackendServer from './mock-server'
import * as http from 'http' import * as http from 'http'
import * as sinon from 'sinon' import sinon from 'sinon'
describe('HTTPTransport', () => { const getCC = () => {
const NOW = Date.now() const NOW = Date.now()
const PORT = 20000 + (+(('' + NOW).slice(-4))) const PORT = 20000 + (+(('' + NOW).slice(-4)))
const bento = new Bento() const bento = new Bento()
const tt = new HTTPTransport( const bento2 = new Bento()
bento, const tt = new HTTPTransport(bento, new JSONSerializer({ verbose: true }), `http://localhost:${PORT}/api/_rpc`, {})
new JSONSerializer(),
`https://localhost:${PORT}/api/_rpc`,
{}
)
const h = tt.handler() const h = tt.handler()
const spy = sinon.spy(h) const spy = sinon.spy(h)
const s = http.createServer(spy) const s = http.createServer(spy)
s.listen(PORT) s.listen(PORT)
bento.transport = tt bento.transport = tt
bento2.transport = tt
bento.service(MockBackendClient.__SERVICE__, MockBackendServer) bento.service(MockBackendClient.__SERVICE__, MockBackendServer)
const cc = bento.client(MockBackendClient) const cc = bento2.client(MockBackendClient)
return { cc, spy }
}
describe('HTTPTransport', async () => {
it('handles full flow properly', async () => { it('handles full flow properly', async () => {
const { cc, spy } = getCC()
const out = await cc.helloBackend({ hello: 'yes!' }) const out = await cc.helloBackend({ hello: 'yes!' })
expect(out.message).toBe(`hello, yes!! i'm bot!!`) expect(out.message).toBe(`hello, yes!! i'm bot!!`)
expect(spy.called).toBe(true) // oof
}) })
s.close()
}) })

View file

@ -3,23 +3,26 @@ import Bento, { JSONSerializer } from '@kayteh/bento'
import { MockBackendClient } from './mock.bento' import { MockBackendClient } from './mock.bento'
import MockBackendServer from './mock-server' import MockBackendServer from './mock-server'
describe('NATSTransport', () => { const getCC = () => {
const NOW = Date.now() const NOW = Date.now()
const bento = new Bento() const bento = new Bento()
const bento2 = new Bento()
const tt = new NATSTransport( const tt = new NATSTransport(bento, new JSONSerializer({ verbose: true }), 'nats://localhost:4222/', '' + NOW)
bento,
new JSONSerializer(),
'nats://localhost:4222/',
'' + NOW
)
bento.transport = tt bento.transport = tt
bento2.transport = tt
bento.service(MockBackendClient.__SERVICE__, MockBackendServer) bento.service(MockBackendClient.__SERVICE__, MockBackendServer)
const cc = bento.client(MockBackendClient) const cc = bento2.client(MockBackendClient)
return cc
}
describe('NATSTransport', () => {
it('handles full flow properly', async () => { it('handles full flow properly', async () => {
const cc = getCC()
const out = await cc.helloBackend({ hello: 'yes!' }) const out = await cc.helloBackend({ hello: 'yes!' })
expect(out.message).toBe(`hello, yes!! i'm bot!!`) expect(out.message).toBe(`hello, yes!! i'm bot!!`)
}) })