Socket
Socket
Sign inDemoInstall

axios-mock-router

Package Overview
Dependencies
19
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    axios-mock-router

mock axios like koa-router


Version published
Weekly downloads
7
Maintainers
1
Install size
467 kB
Created
Weekly downloads
 

Readme

Source

axios-mock-router

mock axios like koa-router

Build Status Coverage Status Greenkeeper badge npm codebeat badge

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] {}'))

FAQs

Last updated on 19 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc