Giter VIP home page Giter VIP logo

axios-mock-router's Introduction

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

axios-mock-router's People

Contributors

greenkeeper[bot] avatar sqlwwx avatar

Watchers

 avatar  avatar

Forkers

pwburd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.