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 ov…


This content originally appeared on DEV Community and was authored by Alex Lunkov

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


This content originally appeared on DEV Community and was authored by Alex Lunkov


Print Share Comment Cite Upload Translate Updates
APA

Alex Lunkov | Sciencx (2024-09-21T16:35:50+00:00) Mock server. Retrieved from https://www.scien.cx/2024/09/21/mock-server/

MLA
" » Mock server." Alex Lunkov | Sciencx - Saturday September 21, 2024, https://www.scien.cx/2024/09/21/mock-server/
HARVARD
Alex Lunkov | Sciencx Saturday September 21, 2024 » Mock server., viewed ,<https://www.scien.cx/2024/09/21/mock-server/>
VANCOUVER
Alex Lunkov | Sciencx - » Mock server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/21/mock-server/
CHICAGO
" » Mock server." Alex Lunkov | Sciencx - Accessed . https://www.scien.cx/2024/09/21/mock-server/
IEEE
" » Mock server." Alex Lunkov | Sciencx [Online]. Available: https://www.scien.cx/2024/09/21/mock-server/. [Accessed: ]
rf:citation
» Mock server | Alex Lunkov | Sciencx | https://www.scien.cx/2024/09/21/mock-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.