Home >Backend Development >Golang >Mock server
Hi everyone
Recently I encountered with a need to have a mock http server for local development, where I can configure paths and responses. Definitely I found few interesting solutions, in some of them I have to write code, some of them were a bit overcomplicated and difficult to use. Definitely I did not find something what I can easily configure, like set a path with a specific HTTP method and set a response with a status code and response body. I need just easy configuration.
So... I had some time in the evening :)
I've prepared a repository go-mock-server
I used Go programming language for the implementation. To run a mock server it is just required to create a YAML file and specify list of endpoints with desired HTTP methods on an endpoint and specify a response, like a predefined string or a file in a storage. There are two ways to launch the go-mock-server. The simplest is to use Docker - the repo contains a Docker file, so it is not needed to install Go on your machine, just mount a folder with your configuration file and that is it. Another way is to use Go to run the server.
An example of configuration
port: 8081 endpoints: - path: /{$} response-body: file:model/responses/index.html headers: content-type: - text/html; charset=utf-8 - path: /test # no method or empty array equals to all methods method: [get, post, put, delete] response-body: > {"test": 1} headers: content-type: - application/json - path: /download method: [get] response-body: file:model/responses/download/file.txt headers: content-type: - application/octet-stream - path: /redirect method: [get] status-code: 301 headers: location: - https://google.com
The above is the detailed content of Mock server. For more information, please follow other related articles on the PHP Chinese website!