-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* http server * update error * support only POST * lint * handle OPTIONS * fix headers * fix response * lint
- Loading branch information
1 parent
a8750f4
commit 350b6e2
Showing
3 changed files
with
227 additions
and
30 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
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,119 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { request } from 'http' | ||
|
||
import { env, setupApi, ws } from './helper.js' | ||
|
||
setupApi(env.acala) | ||
|
||
describe('http.server', () => { | ||
it('works', async () => { | ||
const port = /:(\d+$)/.exec(ws.endpoint)?.[1] | ||
if (!port) { | ||
throw new Error('cannot found port') | ||
} | ||
|
||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'chain_getBlockHash', params: [] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
"result": "0x0df086f32a9c3399f7fa158d3d77a1790830bd309134c5853718141c969299c7", | ||
} | ||
`, | ||
) | ||
} | ||
|
||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'system_health', params: [] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot(` | ||
{ | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
"result": { | ||
"isSyncing": false, | ||
"peers": 0, | ||
"shouldHavePeers": false, | ||
}, | ||
} | ||
`) | ||
} | ||
|
||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'system_invalid', params: [] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"error": { | ||
"message": "Method not found: system_invalid", | ||
}, | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
} | ||
`, | ||
) | ||
} | ||
|
||
{ | ||
const res = await fetch(`http://localhost:${port}`, { | ||
method: 'POST', | ||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'chain_subscribeNewHeads', params: [] }), | ||
}) | ||
expect(await res.json()).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"error": { | ||
"message": "Subscription is not supported", | ||
}, | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
} | ||
`, | ||
) | ||
} | ||
|
||
{ | ||
const body = JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'chain_getBlockHash', params: [] }) | ||
request( | ||
{ | ||
method: 'GET', | ||
host: 'localhost', | ||
port: port, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': body.length.toString(), | ||
}, | ||
}, | ||
(res) => { | ||
let data = '' | ||
res.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
res.on('end', () => { | ||
expect(JSON.parse(data)).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"error": { | ||
"message": "Only POST method is supported", | ||
}, | ||
"id": 1, | ||
"jsonrpc": "2.0", | ||
} | ||
`, | ||
) | ||
}) | ||
}, | ||
).end(body) | ||
} | ||
}) | ||
}) |