Home > Backend Development > Golang > Mock server

Mock server

Mary-Kate Olsen
Release: 2024-09-22 06:22:08
Original
315 people have browsed it

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
Copy after login

The above is the detailed content of Mock server. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template