Switch API endpoint from Production to localhost environment for testing
Here is a list of what we achive at the end of this tutorial:
- Setup Proxyman with your Mac
- Capture Request/Response from your Web Browser
- ✅ Map from Production to localhost environment
Why we need to switch API endpoint from production to local environment?
If you're a developer or QA, it's common to test the API endpoint on both production environment. But sometimes, you need to test the API endpoint on local environment with new changes.
Prerequisites
- Proxyman macOS 5.14.0 or later
- or Download Proxyman BETA for OpenAPI feature.
Before starting, make sure you have Proxyman installed on your Mac you have installed and trust Proxyman Certificate on your Mac.
If you're a new users, please follow the Getting Started Guide for macOS which helps you set up your Mac.
Finally, try opening www.producthunt.com or anywebsite in your browser and capture the traffic. Proxyman now can capture and decrypt HTTPS traffic from your browser.
Scenario 1: Switch from Production to localhost environment
Let's say we have a API endpoint that is hosted on production environment at https://httpbin.org, We want to test the website on localhost environment with a local HTTPBin server at http://localhost:8080. We can switch the API endpoint from production to localhost environment by using Map Remote Tool on Proxyman app.
For example:
- https://httpbin.org/get?id=123 -> http://localhost:8080/get?id=123
- https://httpbin.org/post -> http://localhost:8080/post
- https://httpbin.org/put -> http://localhost:8080/put
- https://httpbin.org/delete -> http://localhost:8080/delete
Let's see how to do it.
Step 1: Capture HTTPS traffic from production environment
Visit https://httpbin.org/get?id=123 in your browser and capture the traffic. Make sure at this point, Proxyman can capture and decrypt HTTPS traffic from your browser.
If not, please enable SSL/TLS Interception on Proxyman.
and resent the request again.
Step 2: Prepare our local HTTPBin server
It can be your NodeJS or Python HTTPBin server, but in this tutorial, I will use HTTPBin as a local HTTPBin server.
Let's run the HTTPBin server on localhost:8080.
docker run -p 8080:80 kennethreitz/httpbin
If you don't have Docker installed, please install it from here.
Verify that the HTTPBin server is running on localhost:8080.
curl http://localhost:8080/get?id=123
If you see the response from the HTTPBin server, that means the HTTPBin server is running on localhost:8080.
Step 3: Map from Production to localhost environment
Right click on the request on step 1 -> Tools -> Map Remote. Use the following configuration:
Map Rule
- Name: Map from Production to localhost
- Rule: https://httpbin.org/*
- Select ANY Method and Including subpath checkbox
Map To
- Protocol: http
- host: localhost
- port: 8080
By creating this rule, all requests from https://httpbin.org/* will be mapped to http://localhost:8080/* and sent to the local HTTPBin server.
Step 4: Test the API endpoint
Visit https://httpbin.org/get?id=123 in your browser again. You will see the response from the local HTTPBin server.
You can open the Summary Tab -> Debugging Tools section and see what Rule is applied to the request and how it is mapped.
Conclusion
Map Remote Tools is useful for testing API endpoint without changing the code. You can switch API endpoint from production to localhost environment on the fly.
What's next?
- Download Proxyman: Proxyman Download
- Read more about Proxyman: Proxyman Documentation
- Read more about Map Remote Tools: Map Remote Tools Documentation