57 lines
2.3 KiB
JavaScript
57 lines
2.3 KiB
JavaScript
import s from 'apigo.cloud/git/apigo/service'
|
|
import file from 'apigo.cloud/git/apigo/plugins/file'
|
|
import runtime from 'apigo.cloud/git/apigo/plugins/runtime'
|
|
import console from "console";
|
|
|
|
// test register api manually
|
|
file.write('api/echo.js', 'import service from "apigo.cloud/git/apigo/service"\nreturn service.getArgs().data')
|
|
s.register({
|
|
actionFile: 'api/echo.js',
|
|
method: 'POST',
|
|
path: '/echo',
|
|
})
|
|
|
|
// start server asynchronously for test
|
|
let c = s.asyncStart()
|
|
|
|
// test /hello without access token
|
|
if(c.get('/hello?name=World', {Host:'localhost'}).statusCode !== 403) throw new Error('test hello World 403 failed')
|
|
|
|
// test /hello without host (api limited host is localhost)
|
|
if(c.get('/hello?name=World').statusCode !== 404) throw new Error('test hello World 404 failed')
|
|
|
|
// test /hello with access token (auth level 1)
|
|
if(c.get('/hello?name=World', {Host:'localhost', 'Access-Token':'huDWA-78we2-89uji1da'}).string() !== 'Hello World') throw new Error('test hello World failed')
|
|
|
|
// test /hello with access token (auth level 2)
|
|
if(c.get('/hello?name=ABC', {Host:'localhost', 'Access-Token':'89dsj-aADSsn-Uds1dad'}).string() !== 'Hello ABC') throw new Error('test hello ABC failed')
|
|
|
|
// test /echo by get (will response 404)
|
|
if(c.get('/echo?data=World').statusCode !== 404) throw new Error('test echo over get failed')
|
|
|
|
// test /echo by post
|
|
if(c.post('/echo', {data:'World'}).string() !== 'World') throw new Error('test echo over post failed')
|
|
|
|
// test api hot load
|
|
file.write('api/echo.js', 'import service from "apigo.cloud/git/apigo/service"\nreturn service.getArgs().data+"!"')
|
|
runtime.sleep(200)
|
|
if(c.post('/echo', {data:'World'}).string() !== 'World!') throw new Error('t est echo over post failed')
|
|
file.remove('api/echo.js')
|
|
|
|
// test websocket
|
|
let wc = c.open('/ws', {Host:'localhost', 'Access-Token':'huDWA-78we2-89uji1da'})
|
|
wc.writeJSON({aaa:'hello', bbb:222})
|
|
let msg = wc.readJSON()
|
|
if(msg.aaa !== 111) throw new Error('test echo over websocket failed')
|
|
|
|
// test discover
|
|
s.addDiscoverAppForTest('test', 'http:huDWA-78we2-89uji1da')
|
|
s.setDiscoverNode('test', c.addr, 100)
|
|
let caller = s.getCaller()
|
|
if(caller.get('test', '/hello?name=World', {Host:'localhost'}).string() !== 'Hello World') throw new Error('test hello World 403 failed')
|
|
|
|
// stop server
|
|
c.stop()
|
|
|
|
return true
|