Coder Social home page Coder Social logo

eexit / mirror-http-server Goto Github PK

View Code? Open in Web Editor NEW
38.0 1.0 7.0 328 KB

A dummy HTTP server that responds whatever you told it to

Home Page: https://mirror-http-server.web.app

License: MIT License

JavaScript 94.70% Dockerfile 5.30%
dummy-server http-mirror mirroring http http-server docker docker-container docker-image

mirror-http-server's Introduction

logo

Mirror HTTP Server DockerHub Firebase function

A dummy HTTP server that responds whatever you told it to.

Testing URL: https://mirror-http-server.web.app

Built to play with HTTP or test your API. Make a HTTP call to the dummy server with the specified headers you want the server responds with.

Usage

Pull the Docker container:

docker pull eexit/mirror-http-server

Start the container:

$ docker run -itp 8080:8080 eexit/mirror-http-server
2015-11-05T20:59:57.353Z]  INFO: mirror-http-server/17 on ccc867df5980: Listening on http://0.0.0.0:8080

For this README examples, I use the great HTTPie tool.

Send request against it:

http :8080
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 0
Date: Wed, 13 Mar 2019 12:38:07 GMT
X-Powered-By: Express

You can use any HTTP verbs with any path, any request body and any header.

Server behavioural request headers

You can change the server response code and body by setting specific X-Mirror-* headers to your request.

X-Mirror-Code

Change the server response status code.

Examples that simulates a 301 redirection and a Content-Type change:

http :8080 \
    X-Mirror-Code:301 \
    X-Mirror-Location:http://www.eexit.net \
    X-Mirror-Content-Type:"text/plain; charset=ISO-8859-1"
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 0
Content-Type: text/plain; charset=ISO-8859-1
Date: Wed, 13 Mar 2019 12:41:35 GMT
Location: http://www.eexit.net
X-Powered-By: Express

If you add the --follow option, it will output my website HTML source.

If you check the container logs:

[2019-03-13T12:41:35.567Z]  INFO: mirror-http-server/18 on f9c1a773d75a:
    request: {
      "ip": "172.17.0.1",
      "ips": [],
      "method": "GET",
      "url": "/",
      "headers": {
        "host": "localhost",
        "user-agent": "HTTPie/1.0.2",
        "accept-encoding": "gzip, deflate",
        "accept": "*/*",
        "connection": "keep-alive",
        "x-mirror-code": "301",
        "x-mirror-location": "http://www.eexit.net",
        "x-mirror-content-type": "text/plain; charset=ISO-8859-1"
      },
      "body": {}
    }

X-Mirror-Delay

If you need to test timeouts or errors handling like 503 HTTP responses, you can pass the X-Mirror-Delay header with a number in milliseconds before the server responds.

time http :8080 X-Mirror-Code:503 X-Mirror-Delay:2000
HTTP/1.1 503 Service Unavailable
Connection: keep-alive
Content-Length: 0
Date: Fri, 20 May 2022 09:52:04 GMT
Keep-Alive: timeout=5
X-Powered-By: Express



http :8080 X-Mirror-Code:503 X-Mirror-Delay:2000  0.12s user 0.03s system 7% cpu 2.163 total

X-Mirror-Request

If you can't access to the container log or want to exploit what's logged under the hood, set the X-Mirror-Request to receive the logged entry (as JSON):

$ http POST :80/resource \
    X-Mirror-Code:201 \
    X-Mirror-Request:true \
    key1=value1 key2=value2
HTTP/1.1 201 Created
Connection: keep-alive
Content-Length: 371
Content-Type: application/json; charset=utf-8
Date: Wed, 13 Mar 2019 12:43:02 GMT
ETag: W/"173-rgXpQ/N7aKeAq+URc1y3vQypNZk"
X-Powered-By: Express

{
    "request": {
        "body": {
            "key1": "value1",
            "key2": "value2"
        },
        "headers": {
            "accept": "application/json, */*",
            "accept-encoding": "gzip, deflate",
            "connection": "keep-alive",
            "content-length": "36",
            "content-type": "application/json",
            "host": "localhost",
            "user-agent": "HTTPie/1.0.2",
            "x-mirror-code": "201",
            "x-mirror-request": "true"
        },
        "ip": "172.17.0.1",
        "ips": [],
        "method": "POST",
        "url": "/resource"
    }
}

Note: if you don't specify the true value for the header, it'll ignored.

X-Mirror-Body

Instead, if you wish the dummy server to return you the body you sent to it, set the X-Mirror-Body header.

Note: the X-Mirror-Request header will override X-Mirror-Body header.

$ http PUT :80/resource \
    X-Mirror-Code:400 \
    X-Mirror-Body:true \
    key1=value1 key2=value2
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 33
Content-Type: application/json; charset=utf-8
Date: Wed, 13 Mar 2019 12:43:45 GMT
ETag: W/"21-SWsq4vawbQc/koBuf3CC1L6ssws"
X-Powered-By: Express

{
    "key1": "value1",
    "key2": "value2"
}

Note: if you don't specify the true value for the header, it'll ignored.

Works with any headers

Aside to the previous three special headers, you can set your wanted response header by prepending your header name by X-Mirror-.

In the request:

Content-Type: application/json
X-Mirror-Content-Type: text/html

You'll get in your response:

Content-Type: text/html

You can even override Express headers or any other default header:

X-Mirror-X-Powered-By: eexit-engine
X-Mirror-Date: some date

Will turn into:

X-Powered-By: eexit-engine
Date: some date

Development

You can either use Docker to run the server locally or emulate the function using Firebase CLI:

In one terminal, run this command:

firebase emulators:start

In another terminal, test it:

http http://localhost:5001/mirror-http-server/us-central1/mirror X-Mirror-Body:true message="Hello world\!"
HTTP/1.1 200 OK
connection: keep-alive
content-length: 26
content-type: application/json; charset=utf-8
date: Sun, 06 Nov 2022 10:56:21 GMT
etag: W/"1a-T7vCLEZV7pLSyUzkr9XBdG32YU8"
keep-alive: timeout=5
x-powered-by: Express

{
    "message": "Hello world!"
}

Deployment

This service is host as Google Clound Platform Cloud Function.

firebase deploy --only functions

Todo

  • Functional testing

mirror-http-server's People

Contributors

eexit avatar greenkeeper[bot] avatar snyk-bot 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

Watchers

 avatar

mirror-http-server's Issues

Internal server error when setting access control headers

I'm getting a 500 response when I try to mirror the Access-Control-Allow-Origin header.

$ docker run -ditp 8000:80 eexit/mirror-http-server
f7023a922ac60cea9c580abb3898a6f4536a7001a5720f06ab0fa8b62809833f
$ wget -Sq http://localhost:8000
  HTTP/1.1 200 OK
  X-Powered-By: Express
  Date: Wed, 10 Nov 2021 14:25:09 GMT
  Connection: keep-alive
  Content-Length: 0
$ wget -Sq http://localhost:8000 --header='X-Mirror-Access-Control-Allow-Origin: *'
  HTTP/1.1 500 Internal Server Error
  X-Powered-By: Express
  Content-Type: application/json; charset=utf-8
  Content-Length: 33
  ETag: W/"21-3errh/Cxu3miebVOHFA5cuyn784"
  Date: Wed, 10 Nov 2021 14:25:37 GMT
  Connection: keep-alive

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.