项目作者: maizy

项目描述 :
Http server for stubing backends and emulate some errors
高级语言: Python
项目地址: git://github.com/maizy/zaglushka.git
创建时间: 2014-05-05T14:42:14Z
项目社区:https://github.com/maizy/zaglushka

开源协议:MIT License

下载


Zaglushka

Http server for stubing backends and emulate some errors.

For use only in dev or testing environments.

Usage

zaglushka.py [path/to/config.json] [port1,port2,...] [--other_options]

If you want to bind ports <= 1024 use sudo.

All options

  • zaglushka.py path/to/config.json or --config=path/to/config.json – stubs config path.
    See sample config definition bellow

  • zaglushka.py config.json 5000,5001 or --ports=5000,5001 – bind ports

  • --watch=false – don’t watch config and stubs for changes (true by default).

  • --help – display help for all available options.

Requirements

  • python 3.5+
  • tornado 5.1 - 6

Installation

pip install zaglushka (recommended)

or

pip install git+https://github.com/maizy/zaglushka.git

or

  1. git clone https://github.com/maizy/zaglushka.git
  2. cd zaglushka
  3. python setup.py install

CI status

codecov

Some use cases

Simple error stub for any request url

  1. //config.json
  2. {
  3. "urls": [
  4. {
  5. "path_regexp": ".*",
  6. "code": 500,
  7. "response": "<error>Pechal'ka exception: db connection error</error>",
  8. "headers" : {
  9. "Server": "MyBackendServer/0.5"
  10. }
  11. }
  12. ]
  13. }
  1. zaglushka.py config.json 5000
  1. curl -v http://127.0.0.1:5000/any_url
  2. * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
  3. > GET /any_url HTTP/1.1
  4. > User-Agent: curl/7.37.1
  5. > Host: 127.0.0.1:5000
  6. > Accept: */*
  7. >
  8. < HTTP/1.1 500 Internal Server Error
  9. < Content-Length: 55
  10. * Server MyBackendServer/0.5 is not blacklisted
  11. < Server: MyBackendServer/0.5
  12. <
  13. * Connection #0 to host 127.0.0.1 left intact
  14. <error>Pechal'ka exception: db connection error</error>

Server watched for any changes in stub files or config and reload automatically.

Emulate backend responses with response_file and headers_file

  1. //config.json
  2. {
  3. "stubs_base_path": "./stubs", //by default dirname of config.json used
  4. "urls": [
  5. {
  6. "path": "/some/page.html",
  7. "response_file": "page1.html",
  8. "headers_file": "page1.headers"
  9. }
  10. ]
  11. }

More complicated match rules

  1. {
  2. "urls": [
  3. {
  4. "path": "/users/maizy/repos",
  5. "response_file": "stubs/page1.json",
  6. "headers_file": "stubs/page1.headers",
  7. "query": {
  8. "required": {
  9. "page": "1",
  10. "per_page": "100"
  11. },
  12. "other_allowed": true // also match if any additional param exists
  13. }
  14. },
  15. {
  16. "path": "/users",
  17. "response_code": 404,
  18. "response": "not found",
  19. "query": {
  20. "required": {
  21. "user_id": "117",
  22. "field": ["name", "email"] // multiple param value
  23. },
  24. "other_allowed": false // strict match
  25. }
  26. }
  27. ]
  28. }

Proxy responses to real server, but stub some with error

  1. {
  2. "urls": [
  3. {
  4. "path": "/user/m/maizy",
  5. "response": "forbidden",
  6. "response_code": 403
  7. },
  8. {
  9. "path_regexp": "^/user/(\\w+)/(.*)$",
  10. "response_proxy": "http://example.com/app/users/$1/$2" // $1, $2 ... - reg exp matches
  11. }
  12. ]
  13. }

Issues

Build release

  • update version in setup.py
  • add git tag
  • setup ~/.pypirc
  • python setup.py sdist
  • twine upload dist/*