|
| 1 | + |
| 2 | + |
| 3 | +# Mirror HTTP Server [](https://travis-ci.org/eexit/mirror-http-server) |
| 4 | + |
| 5 | +*A dummy HTTP server that responds whatever you told him to.* |
| 6 | + |
| 7 | +Build 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. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Pull the [Docker](https://www.docker.com) container: |
| 12 | + |
| 13 | + $ docker pull eexit/mirror-http-server |
| 14 | + |
| 15 | +Start the container: |
| 16 | + |
| 17 | + $ docker run -itp 80:80 eexit/mirror-http-server |
| 18 | + 2015-11-05T20:59:57.353Z] INFO: mirror-http-server/17 on ccc867df5980: Listening on http://0.0.0.0:80 |
| 19 | + |
| 20 | +For this README examples, I use the great [HTTPie](https://github.com/jkbrzt/httpie) tool. |
| 21 | + |
| 22 | +Send request againt it: |
| 23 | + |
| 24 | + http $(docker-machine ip default) |
| 25 | + |
| 26 | +```http |
| 27 | +HTTP/1.1 200 OK |
| 28 | +Connection: keep-alive |
| 29 | +Content-Length: 0 |
| 30 | +Date: Thu, 05 Nov 2015 21:33:20 GMT |
| 31 | +X-Powered-By: Express |
| 32 | +``` |
| 33 | + |
| 34 | +You can use any [HTTP verbs](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) with any path, any request body and any header. |
| 35 | + |
| 36 | +### Behavioural request headers |
| 37 | + |
| 38 | +You can change the server response code and body by setting specific `X-Mirror-*` headers to your request. |
| 39 | + |
| 40 | +### `X-Mirror-Code` |
| 41 | + |
| 42 | +Change the server response [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). |
| 43 | +Here, simulate a server error: |
| 44 | + |
| 45 | + $ http $(docker-machine ip default) X-Mirror-Code:503 |
| 46 | + |
| 47 | +```http |
| 48 | +HTTP/1.1 503 Service Unavailable |
| 49 | +Connection: keep-alive |
| 50 | +Content-Length: 0 |
| 51 | +Date: Thu, 05 Nov 2015 22:30:11 GMT |
| 52 | +X-Powered-By: Express |
| 53 | +``` |
| 54 | + |
| 55 | +Here, simulate a `301` redirection: |
| 56 | + |
| 57 | + http $(docker-machine ip default) \ |
| 58 | + X-Mirror-Code:301 \ |
| 59 | + X-Mirror-Location:http://www.eexit.net \ |
| 60 | + X-Mirror-Content-Type:"text/plain; charset=ISO-8859-1" |
| 61 | + |
| 62 | +```http |
| 63 | +HTTP/1.1 301 Moved Permanently |
| 64 | +Connection: keep-alive |
| 65 | +Content-Length: 0 |
| 66 | +Content-Type: text/plain; charset=ISO-8859-1 |
| 67 | +Date: Thu, 05 Nov 2015 22:40:02 GMT |
| 68 | +Location: http://www.eexit.net |
| 69 | +X-Powered-By: Express |
| 70 | +``` |
| 71 | + |
| 72 | +If you add the `--follow` option, it will output my website HTML source. |
| 73 | + |
| 74 | +If you check the container logs: |
| 75 | + |
| 76 | +```json |
| 77 | +[2015-11-05T22:48:59.564Z] INFO: mirror-server/18 on 6cb74ed853b0: |
| 78 | + request: { |
| 79 | + "ip": "192.168.99.1", |
| 80 | + "ips": [], |
| 81 | + "method": "GET", |
| 82 | + "url": "/", |
| 83 | + "headers": { |
| 84 | + "host": "192.168.99.100", |
| 85 | + "x-mirror-code": "301", |
| 86 | + "accept-encoding": "gzip, deflate", |
| 87 | + "x-mirror-location": "http://www.eexit.net", |
| 88 | + "accept": "*/*", |
| 89 | + "user-agent": "HTTPie/0.9.2", |
| 90 | + "connection": "keep-alive", |
| 91 | + "x-mirror-content-type": "text/plain; charset=ISO-8859-1" |
| 92 | + }, |
| 93 | + "body": {} |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | +### `X-Mirror-Request` |
| 98 | + |
| 99 | +If you access to the server logs or want to exploit the what's logged, set the `X-Mirror-Request` to receive what's logged in a JSON format: |
| 100 | + |
| 101 | + $ http POST $(docker-machine ip default)/resource \ |
| 102 | + X-Mirror-Code:201 \ |
| 103 | + X-Mirror-Request:true \ |
| 104 | + key1=value1 key2=value2 |
| 105 | + |
| 106 | +```http |
| 107 | +HTTP/1.1 201 Created |
| 108 | +Connection: keep-alive |
| 109 | +Content-Length: 373 |
| 110 | +Content-Type: application/json; charset=utf-8 |
| 111 | +Date: Thu, 05 Nov 2015 22:57:17 GMT |
| 112 | +ETag: W/"175-3rxm7gM5Zwu88cZOABP92A" |
| 113 | +X-Powered-By: Express |
| 114 | +
|
| 115 | +{ |
| 116 | + "request": { |
| 117 | + "body": { |
| 118 | + "key1": "value1", |
| 119 | + "key2": "value2" |
| 120 | + }, |
| 121 | + "headers": { |
| 122 | + "accept": "application/json", |
| 123 | + "accept-encoding": "gzip, deflate", |
| 124 | + "connection": "keep-alive", |
| 125 | + "content-length": "36", |
| 126 | + "content-type": "application/json", |
| 127 | + "host": "192.168.99.100", |
| 128 | + "user-agent": "HTTPie/0.9.2", |
| 129 | + "x-mirror-code": "201", |
| 130 | + "x-mirror-request": "true" |
| 131 | + }, |
| 132 | + "ip": "192.168.99.1", |
| 133 | + "ips": [], |
| 134 | + "method": "POST", |
| 135 | + "url": "/resource" |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### `X-Mirror-Body` |
| 141 | + |
| 142 | +Instead, if you with the dummy server to return you the same body you requested to it, set the `X-Mirror-Body` header. |
| 143 | + |
| 144 | +Note: the `X-Mirror-Request` header will override `X-Mirror-Body` header. |
| 145 | + |
| 146 | + $ http PUT $(docker-machine ip default)/resource \ |
| 147 | + X-Mirror-Code:400 \ |
| 148 | + X-Mirror-Body:true \ |
| 149 | + key1=value1 key2=value2 |
| 150 | + |
| 151 | +```http |
| 152 | +HTTP/1.1 400 Bad Request |
| 153 | +Connection: keep-alive |
| 154 | +Content-Length: 33 |
| 155 | +Content-Type: application/json; charset=utf-8 |
| 156 | +Date: Thu, 05 Nov 2015 23:52:34 GMT |
| 157 | +ETag: W/"21-/0XMODUWUwfvQUwjyixvZw" |
| 158 | +X-Powered-By: Express |
| 159 | +
|
| 160 | +{ |
| 161 | + "key1": "value1", |
| 162 | + "key2": "value2" |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Works for all headers |
| 167 | + |
| 168 | +Aside to the previous three special headers, you can set your wanted response header by prepending your header name by `X-Mirror-`. |
| 169 | + |
| 170 | +In the request: |
| 171 | + |
| 172 | +```http |
| 173 | +Content-Type: application/json |
| 174 | +X-Mirror-Content-Type: text/html |
| 175 | +``` |
| 176 | + |
| 177 | +You'll get in your response: |
| 178 | + |
| 179 | +```http |
| 180 | +Content-Type: text/html |
| 181 | +``` |
| 182 | + |
| 183 | +You can even override Express headers or any other default header: |
| 184 | + |
| 185 | +```http |
| 186 | +X-Mirror-X-Powered-By: eexit-engine |
| 187 | +X-Mirror-Date: some date |
| 188 | +``` |
| 189 | + |
| 190 | +Will turn into: |
| 191 | + |
| 192 | +```http |
| 193 | +X-Powered-By: eexit-engine |
| 194 | +Date: some date |
| 195 | +``` |
0 commit comments