首页  >  文章  >  后端开发  >  Mock server

Mock server

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-09-22 06:22:08274浏览

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

以上是Mock server的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn