Created
April 8, 2021 11:31
-
-
Save pimterry/720d1ab444285441dad507e1c584458c to your computer and use it in GitHub Desktop.
A tiny Mockttp script that creates a response-rewriting proxy using Mockttp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mockttp = require('mockttp'); | |
const server = mockttp.getLocal(); | |
server.start().then(async () => { | |
// This creates a proxy rule, which will forward all traffic to the real server: | |
await server.anyRequest().thenPassThrough({ | |
// Every time the real server gets a response, this callback will be called: | |
beforeResponse: (response) => { | |
console.log(`Got ${response.statusCode} response with body: ${response.body.text}`); | |
return { | |
// TODO: Any values set here replace parts of the response: | |
body: "replacement body" | |
}; | |
} | |
}); | |
console.log(`Server running on port ${server.port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
index.js
npm install mockttp
node index.js
to start the servercurl http://localhost:8000 -H"Host: example.com"
(this simulates transparent proxying: it sends a request to localhost on port 8000 whose headers say it's intended for example.com). It should print the HTML from example.com in the console, and return "replacement body" in the CURL response.beforeResponse
to change how the response is logged or rewritten.node index.js
to restart the server.