-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bin): add the full feature and some e2e test
- Loading branch information
1 parent
c389839
commit e7c5d78
Showing
12 changed files
with
243 additions
and
19 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,9 @@ | ||
/** | ||
* @type {import('@tonjs/ton').TonRoute} | ||
*/ | ||
const route = { | ||
methods: 'get', | ||
pattern: '/', | ||
handler: () => 'Hi There!' | ||
} | ||
module.exports = route |
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,8 @@ | ||
import { TonRoute } from '@tonjs/ton' | ||
|
||
const route: TonRoute = { | ||
methods: 'get', | ||
pattern: '/', | ||
handler: () => 'Hi There!' | ||
} | ||
export default route |
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,27 @@ | ||
const { sendText, sendEmpty } = require('@tonjs/ton') | ||
|
||
/** | ||
* @type {import('@tonjs/ton').TonRoutes} | ||
*/ | ||
const routes = [ | ||
{ | ||
methods: 'get', | ||
pattern: '/', | ||
handler: (req, res) => sendText(res, 200, 'TonJS') | ||
}, | ||
{ | ||
methods: 'get', | ||
pattern: '/empty', | ||
handler: (req, res) => sendEmpty(res) | ||
}, | ||
{ | ||
methods: 'any', | ||
pattern: '/ping', | ||
handler: function pong() { | ||
return { | ||
result: 'pong' | ||
} | ||
} | ||
} | ||
] | ||
module.exports = routes |
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,24 @@ | ||
import { TonRoutes, sendEmpty, sendText } from '@tonjs/ton' | ||
|
||
const routes: TonRoutes = [ | ||
{ | ||
methods: 'get', | ||
pattern: '/', | ||
handler: (req, res) => sendText(res, 200, 'TonJS') | ||
}, | ||
{ | ||
methods: 'get', | ||
pattern: '/empty', | ||
handler: (req, res) => sendEmpty(res) | ||
}, | ||
{ | ||
methods: 'any', | ||
pattern: '/ping', | ||
handler: function pong() { | ||
return { | ||
result: 'pong' | ||
} | ||
} | ||
} | ||
] | ||
export default routes |
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,5 @@ | ||
/** | ||
* @type {import('@tonjs/ton').TonHandler} | ||
*/ | ||
const handler = () => 'Hi There!' | ||
module.exports = handler |
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,4 @@ | ||
import { TonHandler } from '@tonjs/ton' | ||
|
||
const single: TonHandler = () => 'Hi There!' | ||
export default single |
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,122 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import axios from 'axios' | ||
import path from 'path' | ||
import { TonApp, TonListenSocket, close } from '@tonjs/ton' | ||
import bin from './index' | ||
|
||
let logLevel = process.env.LOG_LEVEL | ||
let instance: { app: TonApp; token: TonListenSocket } | ||
|
||
beforeAll(() => { | ||
logLevel = process.env.LOG_LEVEL | ||
process.env.LOG_LEVEL = 'silent' | ||
}) | ||
|
||
afterAll(() => { | ||
if (logLevel) { | ||
process.env.LOG_LEVEL = logLevel | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
close(instance.token) | ||
}) | ||
|
||
describe('e2e', () => { | ||
it('should import TonHandler TS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/single.ts'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
const { data, status } = await axios.get('http://0.0.0.0:4000') | ||
expect(status).toBe(200) | ||
expect(data).toBe('Hi There!') | ||
}) | ||
|
||
it('should import TonHandler JS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/single.js'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
const { data, status } = await axios.get('http://0.0.0.0:4000') | ||
expect(status).toBe(200) | ||
expect(data).toBe('Hi There!') | ||
}) | ||
|
||
it('should import TonRoute TS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/route.ts'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
const { data, status } = await axios.get('http://0.0.0.0:4000') | ||
expect(status).toBe(200) | ||
expect(data).toBe('Hi There!') | ||
}) | ||
|
||
it('should import TonRoute JS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/route.js'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
const { data, status } = await axios.get('http://0.0.0.0:4000') | ||
expect(status).toBe(200) | ||
expect(data).toBe('Hi There!') | ||
}) | ||
|
||
it('should import TonRoutes TS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/routes.ts'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
|
||
const resRoot = await axios.get('http://0.0.0.0:4000') | ||
expect(resRoot.status).toBe(200) | ||
expect(resRoot.data).toBe('TonJS') | ||
|
||
const resEmpty = await axios.get('http://0.0.0.0:4000/empty') | ||
expect(resEmpty.status).toBe(204) | ||
|
||
const resPing = await axios.get('http://0.0.0.0:4000/ping') | ||
expect(resPing.status).toBe(200) | ||
expect(resPing.data).toEqual({ result: 'pong' }) | ||
}) | ||
|
||
it('should import TonRoutes JS', async () => { | ||
instance = await bin({ | ||
host: '0.0.0.0', | ||
port: 4000, | ||
_: ['packages/bin/e2e/routes.js'] | ||
}) | ||
expect(instance.token).not.toBe(undefined) | ||
|
||
const resRoot = await axios.get('http://0.0.0.0:4000') | ||
expect(resRoot.status).toBe(200) | ||
expect(resRoot.data).toBe('TonJS') | ||
|
||
const resEmpty = await axios.get('http://0.0.0.0:4000/empty') | ||
expect(resEmpty.status).toBe(204) | ||
|
||
const resPing = await axios.get('http://0.0.0.0:4000/ping') | ||
expect(resPing.status).toBe(200) | ||
expect(resPing.data).toEqual({ result: 'pong' }) | ||
}) | ||
|
||
it('should use index.js as default entry', async () => { | ||
try { | ||
await bin({ host: '0.0.0.0', port: 4000, _: [] }) | ||
} catch (err) { | ||
// eslint-disable-next-line jest/no-try-expect | ||
expect(err.moduleName).toBe(path.resolve(process.cwd(), 'index.js')) | ||
} | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -2016,6 +2016,13 @@ aws4@^1.8.0: | |
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.1.tgz#7e33d8f7d449b3f673cd72deb9abdc552dbe528e" | ||
integrity sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug== | ||
|
||
axios@^0.19.2: | ||
version "0.19.2" | ||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27" | ||
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA== | ||
dependencies: | ||
follow-redirects "1.5.10" | ||
|
||
babel-jest@^25.5.1: | ||
version "25.5.1" | ||
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-25.5.1.tgz#bc2e6101f849d6f6aec09720ffc7bc5332e62853" | ||
|
@@ -2864,7 +2871,7 @@ dateformat@^3.0.0: | |
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae" | ||
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q== | ||
|
||
[email protected]: | ||
[email protected], debug@=3.1.0: | ||
version "3.1.0" | ||
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" | ||
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== | ||
|
@@ -3650,6 +3657,13 @@ flush-write-stream@^1.0.0: | |
inherits "^2.0.3" | ||
readable-stream "^2.3.6" | ||
|
||
[email protected]: | ||
version "1.5.10" | ||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" | ||
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== | ||
dependencies: | ||
debug "=3.1.0" | ||
|
||
for-in@^1.0.2: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" | ||
|
@@ -7793,9 +7807,9 @@ ts-jest@^25.4.0: | |
yargs-parser "18.x" | ||
|
||
ts-node@^8.6.2: | ||
version "8.9.1" | ||
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.9.1.tgz#2f857f46c47e91dcd28a14e052482eb14cfd65a5" | ||
integrity sha512-yrq6ODsxEFTLz0R3BX2myf0WBCSQh9A+py8PBo1dCzWIOcvisbyH6akNKqDHMgXePF2kir5mm5JXJTH3OUJYOQ== | ||
version "8.10.1" | ||
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.1.tgz#77da0366ff8afbe733596361d2df9a60fc9c9bd3" | ||
integrity sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw== | ||
dependencies: | ||
arg "^4.1.0" | ||
diff "^4.0.1" | ||
|