-
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.
- Loading branch information
Showing
7 changed files
with
216 additions
and
78 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,109 @@ | ||
import supertest from 'supertest'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { createApp, AppServer } from '../index'; | ||
|
||
describe('app', async () => { | ||
const rpc: AppServer = await createApp({ | ||
name: 'tomapp', | ||
base: import.meta.url, | ||
port: 3001, | ||
debug: false, | ||
mount: './f', | ||
}); | ||
|
||
rpc.fn('a', function (a) { | ||
// console.dir(rpc.fn); | ||
// console.dir(this); | ||
// this.render('user', { user: { name: 'alfred' } }); | ||
return { a: a }; | ||
}); | ||
|
||
rpc.fn('/a', function (a) { | ||
return { a: a }; | ||
}); | ||
|
||
rpc.fn('/b', function (a) { | ||
if (this.method === 'POST') { | ||
return { a: a, method: 'post' }; | ||
} | ||
if (this.method === 'GET') { | ||
return { a: a, method: 'get' }; | ||
} | ||
}); | ||
|
||
rpc.fn('com.yourcompany.a', function (a: string) { | ||
return { a: a }; | ||
}); | ||
|
||
rpc.fn('com.yourcompany.b', function (a: string) { | ||
return `${this.path} , ${a} `; | ||
}); | ||
|
||
rpc.add({ | ||
'/add': function (a: string, b: string) { | ||
return { a: a }; | ||
}, | ||
}); | ||
|
||
const request = supertest(rpc.callback()); | ||
|
||
it('should start === rpc.callback', async () => { | ||
const request2 = supertest(rpc.start(30001)); | ||
const res = await request2.get('/api/a?$p=["hello"]'); | ||
expect(res.type).toEqual('application/json'); | ||
expect(res.status).toEqual(200); | ||
expect(res.body).toEqual({ a: 'hello' }); | ||
}); | ||
|
||
it('should access /a', async () => { | ||
const res = await request.get('/api/a?$p=["hello"]'); | ||
expect(res.type).toEqual('application/json'); | ||
expect(res.status).toEqual(200); | ||
expect(res.body).toEqual({ a: 'hello' }); | ||
}); | ||
|
||
it('should access no routers[key]', async () => { | ||
// if (!['POST', 'PUT', 'PATCH'].includes(ctx.method) && !ctx.query.$p) { | ||
const res = await request.get('/api/a123'); | ||
expect(res.type).toEqual('text/plain'); | ||
expect(res.status).toEqual(200); | ||
expect(res.text).toEqual('not match $p param, no process'); | ||
}); | ||
|
||
it('should access no routers[key]', async () => { | ||
const res = await request.get('/api/a123?$p=["hello"]'); | ||
expect(res.type).toEqual('text/plain'); | ||
expect(res.status).toEqual(200); | ||
expect(res.text).toEqual('[fn plugin] no fn repsonse, please check your fn if exist'); | ||
}); | ||
|
||
it('should access /add', async () => { | ||
const res = await request.get('/api/add?$p=["hello"]'); | ||
expect(res.type).toEqual('application/json'); | ||
expect(res.status).toEqual(200); | ||
expect(res.body).toEqual({ a: 'hello' }); | ||
}); | ||
|
||
it('should access GET /b', async () => { | ||
const res = await request.get('/api/b?$p=["hello1"]'); | ||
expect(res.type).toEqual('application/json'); | ||
expect(res.status).toEqual(200); | ||
expect(res.body).toEqual({ a: 'hello1', method: 'get' }); | ||
}); | ||
|
||
it('should access POST /b', async () => { | ||
const res = await request.post('/api/b').send(['hello']).set('Accept', 'application/json'); | ||
|
||
expect(res.type).toEqual('application/json'); | ||
expect(res.status).toEqual(200); | ||
expect(res.body).toEqual({ a: 'hello', method: 'post' }); | ||
}); | ||
|
||
it('should access com.yourcompany.b', async () => { | ||
const res = await request.get('/api/com/yourcompany/b?$p=["hello"]'); | ||
expect(res.type).toEqual('text/plain'); | ||
expect(res.status).toEqual(200); | ||
expect(res.text).toEqual('/com/yourcompany/b , hello '); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
// import mount from '../mount'; | ||
|
||
describe('lib', () => { | ||
describe('app', async () => { | ||
it('should render lib', () => { | ||
// expect(lib()).toBe('lib'); | ||
expect('lib').toBe('lib'); | ||
}); | ||
|
||
it('mock console.dir', () => { | ||
const spy = vi.spyOn(console, 'dir'); | ||
console.dir('2323'); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
}); |
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
Oops, something went wrong.