Giter VIP home page Giter VIP logo

httplaceholder's Introduction

HttPlaceholder
Quickly stub away any HTTP service.

Report a Bug · Request a Feature · Ask a Question

license

PRs welcome code with hearth by dukeofharen Docker pulls Nuget downloads

Table of Contents

About

HttPlaceholder lets you stub away any HTTP webservice.

It was thought up while I was working at a company which had many interconnected webservices. The development process was very painful, because it meant all the other webservices should also be started on my local dev PC, or I had to use the "development" environment, which did not work for half of the time. HttPlaceholder fills this gap nicely.

HttPlaceholder:

  • can easily be installed on your dev PC.
  • can be used to easily create stubs, either as separate YAML files, through the API or through the user interface.
  • can easily be hosted on any server.
  • can use many different HTTP condition checkers and response writers to make your HTTP stubs as specific as you need.

Also visit the website: https://httplaceholder.org/

Built With

Getting Started

Prerequisites

Linux, Windows and Mac are supported.

Installation

Follow these steps to install / update HttPlaceholder. If you update HttPlaceholder, make sure to read the changelog to see if there are no breaking changes.

Install on Windows

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/dukeofharen/httplaceholder/master/scripts/Install-Windows.ps1'))

Install on Linux

curl -o- https://raw.githubusercontent.com/dukeofharen/httplaceholder/master/scripts/install-linux.sh | bash

Install on Mac

curl -o- https://raw.githubusercontent.com/dukeofharen/httplaceholder/master/scripts/install-mac.sh | bash

Docker

HttPlaceholder has a Docker image; it can be found here.

Run the following command to run a basic HttPlaceholder container: docker run -p 5000:5000 dukeofharen/httplaceholder:latest

Install as .NET tool

dotnet tool install --global HttPlaceholder

Example

  • Install HttPlaceholder (see Installation).
  • Create a new .yaml file (e.g. stub.yaml).
  • Copy and paste these contents in your new file:
- id: situation-01
  conditions:
    method: GET
    url:
      path:
        equals: /users
      query:
        id:
          equals: 12
        filter:
          equals: first_name
  response:
    statusCode: 200
    text: |
      {
        "first_name": "John"
      }
    headers:
      Content-Type: application/json
  • Open the terminal in the folder you've added the stub.yaml file and run the following command: httplaceholder. HttPlaceholder will now start and will load the stubs in the current folder.

  • Perform a specific HTTP call to HttPlaceholder so your provided response will be returned.

    • For Linux / Mac (cURL needs to be installed):
    curl "http://localhost:5000/users?id=12&filter=first_name" -D-
    • For Windows (uses Powershell):
    (Invoke-WebRequest "http://localhost:5000/users?id=12&filter=first_name").RawContent
  • You can view and inspect the performed requests in the user interface at http://localhost:5000/ph-ui.

For more sophisticated examples, go to the samples to view samples for all supported HTTP condition checkers and response writers. Learn more about HttPlaceholder by reading the documentation.

Documentation

Tools

Roadmap

See the open issues for a list of proposed features (and known issues).

Support

Reach out to the maintainer at one of the following places:

Project assistance

If you want to say thank you or/and support active development of HttPlaceholder:

Contributing

First off, thanks for taking the time to contribute! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated.

We have set up a separate document containing our contribution guidelines.

Thank you for being involved!

Authors & contributors

The original setup of this repository is by dukeofharen (https://ducode.org).

For a full list of all authors and contributors, check the contributor's page.

Security

HttPlaceholder follows good practices of security, but 100% security can't be granted in software. HttPlaceholder is provided "as is" without any warranty. Use at your own risk.

For more info, please refer to the security.

License

This project is licensed under the MIT license.

See LICENSE for more information.

httplaceholder's People

Contributors

dukeofharen avatar

Stargazers

Anthony Belshi avatar  avatar  avatar Charles-Xiao avatar  avatar  avatar Enrico Lüdecke avatar  avatar  avatar Fred avatar Marcin Burgknap avatar  avatar LogicException avatar GameDesert avatar weldeon avatar  avatar  avatar  avatar Khasan Sharapov avatar  avatar Grzegorz Kot avatar Augusto Finardi Gallo avatar  avatar  avatar Jean-Christophe Amiel avatar Vysix avatar Silvio Delgado avatar  avatar Daniel Suárez Bobes avatar Rodrigo Carvalho avatar  avatar Steve Szabo avatar Artemis Resende avatar Kristof Buts avatar robertsprinkle sunsan avatar  avatar  avatar Rico avatar Miles Maddox avatar Braunson Yager avatar  avatar windstrikeONE avatar  avatar Kai avatar Christsanctus chinedu avatar  avatar Cumsoft avatar Harold avatar Jasper Scheper avatar Supan Adit Pratama avatar  avatar nopeno avatar Paras Daryanani avatar  avatar Max Knutsson avatar Mike avatar Daniel avatar Tomer Shvueli avatar basemind avatar  avatar  avatar Jonas Dambacher avatar Mike Killey avatar Jan Olbrich avatar Tim Römisch avatar Rutger Roffel avatar Daniel Burkholder avatar José Haro Peralta avatar  avatar Marco Acierno avatar Marco Burro avatar Dearain avatar Sam Kopp avatar Abdullah Karaarslan avatar Fateh Lebbier avatar Giuseppe avatar  avatar Guilherme Branco Stracini avatar Vinícius Mamoré avatar Michael Vonck avatar RaminMT avatar Frank Alvarez avatar Marcel Hofer avatar Kevin Brechbühl avatar Mingzhi Yi avatar Valentin Dide avatar Jacob Struiksma avatar Stephan Scheidl avatar Rui Pedro Silva avatar Robert Gonek avatar Jeff West avatar  avatar  avatar Selmir avatar Ceituna avatar Elgars Logins avatar James Smith avatar Gaurav Narkhede avatar Alistair Chapman avatar Martin Mol avatar

Watchers

James Cloos avatar Khasan Sharapov avatar  avatar Justin Rocco avatar Martin Mol avatar  avatar

httplaceholder's Issues

When providing multiple paths in, separate them by "," instead of "%%"

This was an ugly workaround I've built a few years ago. When you want to provide multiple paths for the inputFile parameter, you do it like this:

httplaceholder --inputFile /var/stubs1%%/var/stubs2

I've did it like this because PowerShell didn't allow comma's, but it will allow comma's if the inputFile string is wrapped with double quotes. So for PowerShell, this doesn't work:

httplaceholder --inputFile /var/stubs1,/var/stubs2

But this does:

httplaceholder --inputFile "/var/stubs1,/var/stubs2"

In all other terminals, using comma's without quotes seems to work just fine.

Long story short, this will fix a very ugly workaround I've built some time ago. Using "%%" will still work though for backwards compatability.

Build something like a .yml schema

Find out if there is such a thing as a YAML schema. If not, perhaps use JSON schema, since YAML is fully convertable to JSON. This schema can be used in VSCode for example or when adding a stub from the UI (to verify if the stub is valid).

Add response writer for content-type header

Instead of specifying the Content-Type header for every request, introduce a specific response writer to do this instead. This is a nice shorthand notation.

A stub with the new response writer might look like this:

- id: csv
  conditions:
    method: GET
    url:
      path: /result.csv
  response:
    statusCode: 200
    text: 'id,name,amount\n1,DukeOfHaren,20'
    contentType: text/csv

Paging in requests overview

Right now, there is no paging in the UI on the requests screen. It might take a while to load if for example 2000 requests are returned.

Add default --inputFile configuration to Dockerfile

Right now, if you want to run the HttPlaceholder Docker container with a set of .yml files, you need to pass the "inputFile" environment variable to the Docker container.

So instead of this: docker run -p 5000:5000 --env inputFile=/var/httplaceholder -v /home/dukeofharen/httplaceholder/docs/samples:/var/httplaceholder dukeofharen/httplaceholder:latest
it can be simplified to: docker run -p 5000:5000 -v /home/dukeofharen/httplaceholder/docs/samples:/var/httplaceholder dukeofharen/httplaceholder:latest
because the folder /var/httplaceholder will be used by default in the Docker container for the "inputFile" environment variable.

Condition checker based on date/time

Create a condition checker for checking the date time. You need to be able to select UTC/local datetime, the from date/time and to date/time.
If type is not selected, the time will be considered as "local". At least from or to has to be set and can also be set both.

It might look like this:

- id: datetime
  conditions:
    url:
      path: /datetime
    datetime:
      type: utc
      from: 2020-12-24 18:00:00
      to: 2021-01-01 01:00:00
  response:
    text: Happy holidays!

Disable CORS

Hi!
Is it possibile to disable CORS with a single flag, instead of repeat the headers in every mock?

Matteo

configJsonLocation config key is case sensitive

The configuration key configJsonLocation is case sensitive.

This works: httplaceholder --configjsonlocation /var/httplaceholder/config.json
This doesn't work: httplaceholder --configJsonLocation /var/httplaceholder/config.json

These should both work.

UI: add categories

Would it be possible to have stub categories and group stubs by categorie in the UI ? Nice to have: make these categories collapsible/extendable in the UI.

Header variable parser: case insensitive

The current header variable parser is case sensitive. So when specifying ((request_header:Content-Type)) in your request, but the actual header is called content-type, it will not be inserted in the response. This parser needs to be case insensitive.

Add Docker (Compose) hosting instructions

HttPlaceholder has an official Docker image. No documentation or examples have not been written yet. Some Docker (Compose) hosting instructions and examples need to be added so people can choose their favourite way of hosting HttPlaceholder with Docker (Compose).

Variable parser for HttPlaceholder root URL

Add a variable handler for inserting the HttPlaceholder root URL in the response. Here is a possible example:

The stub

id: root-url-stub
conditions:
  method: GET
  url:
    path: /root-url
response:
  html: <html>((root_url))/sub/path</html>

The request

GET http://localhost:5000/root-url

The response
<html>http://localhost:5000/sub/path</html>

Make it possible to add a folder with static resources to a .yml file

This might be handy for serving some static files from HttPlaceholder. It might look like this:

The folder

  • static_files
    -- img1.jpg
    -- img2.jpg
    -- subfiles
    --- img3.jpg

The stub

- id: static-files
  conditions:
    url:
      path: /static-files
  response:
    staticFilesDir: /home/user/static_files

The request
GET http://localhost:5000/static-files/subfiles/img3.jpg

The response
The file img3.jpg.

Add possibility for (temporarily) disabling stub

Right now, if I want to disable a stub (if it is through a .yml file or through the UI), I delete / comment out / lower the priority of the stub. A friendlier option might be to introduce an "enabled" flag per stub for enabling / disabling the stub. If no "enabled" flag is provided, the stub is enabled by default.

Also make it clear in the UI if a stub is disabled or not.

Bearer (JWT) token condition checker

For a first scenario, make it so that the fields in the Bearer token can be checked, just like you would do with for example a query string. Keep in mind that a Bearer token value can be an array.

Also find out how easy / hard it is to verify the JWT hash.

Variable parser for parsing JSON with JSONPath and adding it to response

Add more flexibility to a stub by providing a variable parser which parses (valid) JSON, queries that JSON using JSONPath and replacing the variable in the response with the result of the JSONPath query. Here is a possible example:

The stub

- id: jsonpath-example
  conditions:
    method: POST
    url:
      path: /jsonpath-example
  response:
    statusCode: 200
    text: The result: ((jsonpath_query:$.phoneNumbers[?(@.type=='iPhone')].number))

The request

POST http://localhost:5000/jsonpath-example
Content-Type: application/json
{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

The response text
The result: 0123-4567-8888

Cookie condition checker

Make a simple condition checker to check the cookies in the Cookie header in the request. This might look something like this:

- id: cookie-request-test
  conditions:
    cookie:
      - key: id
        value: 123
      - key: name
        value: (.*)
  response:
    text: OK cookie!

So, with the example above, the condition is correct if the following Cookie header is sent: id=123; name=anyvalue

Value can contain a regular string or a regex.

Variable parser for parsing XML with XPath and adding it to response

Add more flexibility to a stub by providing a variable parser which parses (valid) XML, queries that XML using XPath and replacing the variable in the response with the result of the XPath query. Also take XML namespaces in account; this doesn't really work that well in .NET. Here is a possible example:

The stub

- id: xpath-example
  conditions:
    method: POST
    url:
      path: /xpath-example
  response:
    statusCode: 200
    text: The result: ((xpath_query:/test/body/id/text()))

The request

POST http://localhost:5000/xpath-example
Content-Type: text/xml
<test>
    <body>
        <id>114</id>
        <name>Test XML string</name>
    </body>
</test>

The response text
The result: 114

Host in IIS, where is _web.config ?

Installed httplaceholder with:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/dukeofharen/httplaceholder/master/scripts/Install-Windows.ps1'))

The documentation says:

Additionally, two additional files are also installed on the machine: _config.json and _web.config. If you want to host the application in IIS, you have to remove the _ from the filename. The _ is added to the files so the actual configuration files won't be overwritten if a new version of HttPlaceholder is installed

Where are these files located ?

Add possibility to provide a parent for a stub

Right now, if you have multiple stubs with the same conditions / response variables, you'll have to copy/paste those variables to the stubs. Let's take a look at the following example:

- id: stub1
  conditions:
    url:
      path: /stub1
    method: GET
  response:
    headers:
      CustomHeader1: value
      Header1: value1
      Header2: value2
      Header3: value3
    text: OK1

- id: stub2
  conditions:
    url:
      path: /stub2
    method: GET
  response:
    headers:
      CustomHeader2: value
      Header1: value1
      Header2: value2
      Header3: value3
    text: OK2

- id: stub3
  conditions:
    url:
      path: /stub3
    method: GET
  response:
    headers:
      CustomHeader3: value
      Header1: value1
      Header2: value2
      Header3: value3
    text: OK3

Here you have 3 stubs with the same method condition and the same response headers. It would be nice if you could provide a parent where the method condition and response headers are set. This parent can then be set on the stubs. This might look something like this:

- id: stub-parent
  conditions:
    method: GET
  response:
    headers:
      Header1: value1
      Header2: value2
      Header3: value3

- id: stub1
  parent: stub-parent
  conditions:
    url:
      path: /stub1
  response:
    headers:
      CustomHeader1: value
    text: OK1

- id: stub2
  parent: stub-parent
  conditions:
    url:
      path: /stub2
  response:
    headers:
      CustomHeader2: value
    text: OK2

- id: stub3
  parent: stub-parent
  conditions:
    url:
      path: /stub3
  response:
    headers:
      CustomHeader3: value
    text: OK3

In this case, the method condition and response headers are set on the stub stub-parent and the other stubs take that as parent. If you want to override a specific value, you can. The values defined in the child stub are always the truth.

The parent stub should not be taken into account when determining a stub to execute though.

Resolving stub yaml path fails if multiple yaml paths are provided

Let's say you have the following stub

- id: stub1
  conditions:
    method: GET
    url:
      path: /img1
  response:
    file: 1.jpg
    headers:
      Content-Type: image/jpeg

This stub is located in folder /var/stubs/1. There is also a folder /var/stubs/2 that you want to read with HttPlaceholder.

When you start the application like this: httplaceholder --inputFile /var/stubs/1%%/var/stubs/2 and you go to http://httplaceholder-host:5000/img1, the image will not be resolved because the provided inputFile parameter is not a valid path. This should be the case for HttPlaceholder to find the file 1.jpg (which is located besides the stub file).

In short, what needs to happen, every path specified in inputFile should be checked iterated to find the specified file.

Conditions on file uploads

Right now, file uploads are completely skipped. Create a condition checker which can check on file name(s), mime type(s) and in the case of a non-binary upload, file contents.

Add possibility for authenticating with JWT to API

When authentication is enabled, the user needs to authenticate using basic authentication. In order for making the SignalR stuff work, a cookie needs to be set. This is an ugly hack and can be done nicer by doing authentication with a JWT. The UI will also use the JWT for authentication and authorization. The basic authentication stuff will remain for backwards compatability, but the cookie stuff will be deleted as it no longer serves a purpose after implementing the JWT auth.

Re-add NSIS installer

The NSIS installer is not built right now. This was the case in AppVeyor, but GitLab CI doesn't support Windows shared agents out of the box.

Remove full stub form from UI

There are 2 forms in the HttPlaceholder UI right now: one form where you can fill in the stub in YAML format and one form where you can fill in a stub in a full form. The full form will be deleted as it is very complex and a pain to maintain. The YAML stub form will get an update which makes adding a stub through that form a much better experience.

Add response writer for returning random image

You should be able to select dimensions, background color, optional text (+ font size) and file type (jpg, png etc.).

For JPG, you need to be able to select quality (95 by default).

Cookie response writer

Create a response writer for HTTP cookies. It should be able to sent multiple cookies. It might look something like this:

- id: cookie-test
  conditions:
    method: GET
    url:
      path: /cookie-test
  response:
    cookie:
      - key: id
        value: 1122
        expires: 2021-01-01 00:00:00 # Optional, will be formatted by HttPlaceholder in the correct way
        secure: true # Optional
        httpOnly: true # Optional
        path: /subpath # Optional
        sameSite: strict # Optional: can be "lax", "strict" or "none"
        domain: localhost:5000 # Optional
    text: OK Cookies!

In order to send multiple cookies to the client, HttPlaceholder needs to be able to send back multiple headers. For this, #84 needs to be fixed first.

In the example above, when you send a request to http://localhost:5000/cookie-test, a Set-Cookie header with the following value will be returned: id=1122; Expires=Fri, 01 Jan 2021 00:00:00 GMT; Secure; HttpOnly; Path=/subpath; SameSite=strict; Domain=localhost:5000

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.