Coder Social home page Coder Social logo

chinesedfan / parrot-mocker Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 2.0 124 KB

Intercept requests to the mock server

Home Page: https://chrome.google.com/webstore/detail/parrotmocker/hdhamekapmnmceohfdbfelofidflfelm

License: MIT License

JavaScript 91.56% HTML 8.39% Shell 0.06%
chrome-extension mock-responses parrot-mocker fetch-mock xhr-mock jsonp-mock

parrot-mocker's Introduction

parrot-mocker npm version Build Status Coverage Status License

This package supports to intercept XHR/JSONP/Fetch requests and forward to the specified mock server.

How to use

Chrome plugin

Install from Chrome web store.

Or load the plugin by following Google's development guide. The plugin folder is crx.

If the development mode plugin is disabled by Chrome, you can uninstall and install it again.

Standalone

<head>
    <!-- Make sure to be loaded first -->
    <script type="text/javascript" src="dist/parrot.js"></script>
</head>

Webpack

// Modifiy webpack.config.js to prepend this package to dependencies of each entry
module.exports = {
    entry: ['parrot-mocker', './yours/src/entry.js']  
};

Node.js

We export a function to rewrite the request url.

wrapUrl(urlStr, options)

  • urlStr string the url of the API request
  • options
    • reqType string the type of request, like jsonp
    • pageUrl string the url of the page
    • cookie string the cookie of the page request
    • shouldSkip function skip rewritting if returns true. By default, no host or including local will be filtered. Its arguments are:
      • host string the API host

For example, let's make a simple middleware for Koa,

var fetch = require('node-fetch');
var wrapUrl = require('parrot-mocker').wrapUrl;

module.exports = function*(next) {
    this.fetch = (url, options) => {
        return fetch(wrapUrl(url, {
            pageUrl: this.url,
            cookie: this.header.cookie
        }));
    };

    yield* next;
};

License

MIT

Acknowledgement

parrot-mocker's People

Contributors

chinesedfan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

parrot-mocker's Issues

Shadow cookies

Only support to enable by url parameters. Don't write cookies to the page domain any more, but pass fake cookies to the mock server.

HTMLScriptElement.src was changed

The original setter/getter were replaced by setAttribute/getAttribute. But those 2 pairs of methods are not completely identical. It causes our internal knb.js throws an error.

// create a script tag
var script = document.createElement('script');
script.src = 'xx.js';

// in xx.js
document.currentScript.src // returns undefined

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.