https://github.com/sqlwwx/axios-mock-router
https://github.com/sqlwwx/axios-mock-router
axios koa-router mocking
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sqlwwx/axios-mock-router
- Owner: sqlwwx
- Created: 2018-11-10T09:20:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-31T00:07:12.000Z (about 6 years ago)
- Last Synced: 2025-09-21T00:20:25.719Z (9 months ago)
- Topics: axios, koa-router, mocking
- Language: JavaScript
- Size: 387 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# axios-mock-router
mock axios like koa-router
[](https://travis-ci.org/sqlwwx/axios-mock-router)
[](https://coveralls.io/github/sqlwwx/axios-mock-router?branch=master)
[](https://greenkeeper.io/)

[](https://codebeat.co/projects/github-com-sqlwwx-axios-mock-router-master)
## example
```
import AxiosMockRouter from 'axios-mock-router'
import axios from 'axios'
const router = new AxiosMockRouter(axios);
router
.get('https://github.com/', (ctx, next) => {
ctx.body = 'github';
})
.get('/', (ctx, next) => {
ctx.body = 'home';
})
.get('/users', (ctx, next) => {
ctx.body = `get users ${JSON.stringify(ctx.query)}`;
})
.post('/users', (ctx, next) => {
ctx.body = `post users ${ctx.request.body}`;
})
.get('/users/:id', (ctx, next) => {
ctx.body = `get user [${ctx.params.id}] ${JSON.stringify(ctx.query)}`;
})
.put('/users/:id', (ctx, next) => {
ctx.body = `put user [${ctx.params.id}] ${ctx.request.body}`;
})
.del('/users', (ctx, next) => {
ctx.body = `del users ${JSON.stringify(ctx.query)}`;
})
.del('/users/:id', (ctx, next) => {
ctx.body = `del user [${ctx.params.id}] ${JSON.stringify(ctx.query)}`;
});
axios.get('https://github.com/')
.then(res => console.log(res.data === 'github'))
axios.get('https://github.com/sqlwwx')
.then(res => console.log(res.data !== 'github'))
axios.get('/')
.then(res => console.log(res.data === 'home'))
axios.get('/users')
.then(res => console.log(res.data === 'get users {}'))
axios.get('/users?limit=1')
.then(res => console.log(res.data === 'get users {"limit":"1"}'))
axios.post('/users', { name: 'wwx' })
.then(res => console.log(res.data === `post users {"name":"wwx"}`))
axios.put('/users/1', { name: 'wwx' })
.then(res => console.log(res.data === 'put user [1] {"name":"wwx"}'))
axios.get('/users/1?includeStart=true')
.then(res => console.log(res.data === 'get user [1] {"includeStart":"true"}'))
axios.delete('/users?idList=[1,2]')
.then(res => console.log(res.data === 'del users {"idList":"[1,2]"}'))
axios.delete('/users/1')
.then(res => console.log(res.data === 'del user [1] {}'))
```