Giter Site home page Giter Site logo

oh-my-mock's Introduction

Master workflow Feature workflow

Buy me a coffee

OhMyMock

OhMyMock is a Chrome Extension that can mock API responses in the browser. It works as follows: The first time an API request is made it only caches the response, but the next time it will mock the request and reply with the cached response. It is also possible to modify responses or status codes. This extension is especially useful for Frontend development and works well with frameworks like Angular, React or Vue. OhMyMock can serve JSON, HTML, Images and a lot more. Please make a feature request if you have a response which is not supported!!

There is an article on medium.com which describes OhMyMock in great detail here.

If you have the extension install you can see how it works with XMLHttpRequest and Fetch request on the demo page.

By default OhMyMock is disabled and you need to enabled it, otherwise it will not cache or mock API responses.

alt text alt text

Installation

OhMyMock can be install from the chrome extension store, but you can also compile the source and use that instead if you like. This is what you would do if you want to do OhMyMock develop.

To install it from source checkout this repository and run the following commands

$> yarn
$> yarn build

The compiled code is stored in "./dist"

Navigate in Chrome to chrome://extensions and enable development mode and upload the "./dist" folder via the Load unpacked button. Thats it.

Setup for development

First install all dependencies

$> yarn

Start the development web server and watchers

$> yarn watch

The test page will be available at http://localhost:8000

Everytime you hit save the project will rebuild, but after each rebuild you have to reload the extension and test page your self!

Project structure

This project consists of a couple of different part, each with a specific task. Those parts are:

  • ./scripts - Tooling
  • ./test-site - Webserver (test/demo site)
  • ./libs/nodejs-sdk - sdk for serving mocks from files
  • ./src/app - angular app, the OhMyMock popup
  • ./src/content - the extension content script, needed to pass messages between the angular app and the injected script
  • ./src/injected - this is where mocking takes place (e.g. patching of Fetch and XmlHttpRequest)
  • ./src/shared - code shared between all parts
  • ./src/background - the extension's background script
OhMyMock CDK

The cdk enables you to create a NodeJs server which connects with the chrome extension. This allows you to serve responses in an easy way using a NodeJs server. This way it is possible to serve file content as responses. If the server doesn't have a response for a specific request, OhMyMock will then look in the cache and serve that if it exists.

background.js

For all tabs in the chrome browser there will be just one instance running of this script. It is always active and when the OhMyMock icons is clicked it will open the OhMyMock popup (angular app). If you are using the OhMyMock SDK, the background script will establish the websocket connection.

content script

Each tab has its own instance of the content script. It takes care of two things

  1. Inject code into the context of the website

  2. It receives every request from the injected script. It is the task of the content script to find a mock (the response that will be served). This is a two step process:

    1. If there is a websocket connection (in case you use the SDK) the request is forwarded to the NodeJs.
    2. If there is no websocket connection or the NodeJs doesn't serve a response, it looks inside the cached responses.
injected script

The injected script remains dormant until OhMyMock is enabled. If active it will patch window.fetch and window.XmlHttpRequest. This way it is in full control of all requests. When a request is made 3 things can happen:

  1. Every request is dispatched to the content script. If the content script doesn't find a response the call will pass through to the API. OhMyMock will cache the response.
  2. If there is a cached response, but it is not active, the call will pass through to the API.
  3. There is an active cache and it will be served as the response

Finally, if OhMyMock is disabled or the popup is closed, the original Fetch and XmlHttpRequest objects are restored, as if nothing ever happend.

Angular app

The angular application is where you can interact with OhMyMock. Here you can see which responses are cached, which are active, etc. Here you can also create responses manually.

Contributors


Lucas Calje

Cong Yu

Alexandr Plokhih

Known chrome issues

oh-my-mock's People

Contributors

imcuttle avatar plohoj avatar remco75 avatar scaljeri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

oh-my-mock's Issues

refactor store into testable utils

Some of the NGXS store functions are huge and hard to tests and some functionality is duplicated

Output: no duplications, good code coverage of utils and store functions

research mocking from devTools

currently we mock calls by mocking the XMLHttpRequest. Would be nice to do this from devTools / or intercept requests ourselves and mock there.

Support async code for mocking

Currently dynamic mocking is possible if you provide your own javascript code. Currently this has to be synchronous. With this story we want to support asynchronous code as well.

Acceptance criteria:

  • It supports asynchronous execution of code only when it returns a promise
  • The code should have access to the original Fetch and XMLHttpRequest objects

Update Readme

The readme should describe what OhMyMock is and how to setup a development enviroment

reference each response by name + status

It should be possible to have the same status-codes, each, for example, with a different response. Add a name field which should be unique together with the statuscode

Handle uninstalls

Like the welcome splash screen, show a goodbye screen with a google docs form to ask why they uninstalled it

Manifest v3

Investigate what is needed to migrate to v3.

outcome: not much is needed, but it turns out that "eval" will not work anymore. This is a huge issue. Problem is that almost all documentation and issues are v2, so lets wait with this one!

NgApiMock settings page

Setting page, link through sidebar for now

  • create form
  • api mock settings
  • server location
  • integrate checkbox
  • store data

Reset global state

This PR fixes the following issues:

  1. reset global state breaks connection with the target website
  2. Disable mocking on close popup
  3. Use TabId to send messages to, this to make sure that if multiple popups are active the communicate with the correct target
  4. Auto select mock when visiting the fist time

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.