Dummy Server
Setting up a Dummy Server: Definition and Tutorial
The short definition: A "dummy server" refers to a simple server program created for testing or development purposes. It's designed to simulate certain behaviors of a real server system, without carrying out all the functions of the actual server.
For instance, a dummy HTTP server might only respond to requests with predefined responses, or it might respond to all requests in the same way, regardless of the specifics of the request. This kind of server can be used for testing client-side code and checking how the client system behaves when it interacts with a server, without the need to set up and maintain a full production server.
Is a Dummy Server the Same as a Mock Server?
The terms "dummy server" and "mock server" are often used interchangeably, and if you run across them on the internet or IRL they are quite likely to mean the same thing. However, if we’re being a bit pedantic, there are differences between them.
A dummy server, as described above, is a simple server implementation with limited functionality. It provides predefined responses to client requests and can be used for early-stage testing, where the focus is on the client-side code and not the interactions with the actual server.
A mock server, on the other hand, is a more advanced tool for simulating server behavior. It can be customized and configured to return dynamic responses based on specified conditions, which allows developers to test various aspects of their applications more comprehensively. With a mock server, developers can mimic different server scenarios such as success, error, or unexpected responses to simulate real-world conditions more accurately. This aids in API mocking and thorough testing of client-side error handling and the ability to handle a variety of server responses.
How to Set up a Dummy Server with Open Source WireMock
Before we start, you need to have Java installed on your system, as WireMock runs as a standalone server with a Java process. (You can skip the coding and use the free WireMock Cloud UI instead.)
Step 1: Download WireMock
First, download the standalone WireMock server from the WireMock website. You can do this either through your browser, or with the curl command, like so:
curl -O http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/2.31.0/wiremock-standalone-2.31.0.jar
Make sure to replace 2.31.0 with the version number you wish to download if it's different.
Step 2: Run WireMock
Now that you have WireMock, you can start it up. Run the following command in your terminal:
java -jar wiremock-standalone-2.31.0.jar
This will start up the WireMock server on port 8080 by default.
Step 3: Set Up a Stub
To set up a stub, we can use WireMock's RESTful API. In this example, let's create a stub that will respond to all GET requests at the root path (/). Open a new terminal window (don't close the one running WireMock) and enter:
curl -X POST -d '{
"request": {
"method": "GET",
"url": "/"
},
"response": {
"status": 200,
"body": "Hello, I am a dummy server!"
}
}' http://localhost:8080/__admin/mappings --header "Content-Type: application/json"
This will create a new stub that responds to all GET requests to the root path with a 200 OK status and a simple message.
Step 4: Test Your Server
Now that the stub is set up, you can test it by sending a GET request to http://localhost:8080. You can do this in your browser, or with curl:
curl http://localhost:8080
You should see the message "Hello, I am a dummy server!"
Congratulations! You've set up a dummy server with WireMock.