drf-chaos-用于测试Django Rest框架API的装饰器工具集

网友投稿 817 2022-10-31

drf-chaos-用于测试Django Rest框架API的装饰器工具集

drf-chaos-用于测试Django Rest框架API的装饰器工具集

drf-chaos extension

drf-chaos is a small collection of decorators and middlewares for testing Django REST Framework API under unexpected circumstances.

Settings

DRF_CHAOS_ENABLED

Decorators

Delay

Delay response of a specific amount of milliseconds

@delay(rate, milliseconds)

Params:

rate: probability that an unexpected event hAPPens

milliseconds: suspend execution of the current thread for the given number of milliseconds

Error

Return a different response HTTP status code

@error(rate, status)

Params:

rate: probability that an unexpected event happens

status: integer corresponding to any valid HTTP status code. See DRF HTTP status codes

Mime

Return a random Mime Content-type

@mime(rate)

Params:

rate: probability that an unexpected event happens

Chaos

Apply a random unexpected event to the HTTP response. (A delay between 0 to 3 second and a random HTTP status code)

@chaos(rate)

Params:

rate: probability that an unexpected event happens

Middlewares

ChaosMiddleware

Example

from rest_framework import statusfrom rest_framework.response import Responsefrom rest_framework.views import APIViewfrom drf_chaos.decorators import delay, error, mime, chaosclass DelayApiView(APIView): @delay(rate=0.5, milliseconds=700) def get(self, request): return Response(status=status.HTTP_204_NO_CONTENT)class ErrorApiView(APIView): @error(rate=0.5, status=500) def get(self, request): return Response(status=status.HTTP_204_NO_CONTENT)class MimeTypeApiView(APIView): @mime(rate=0.5) def get(self, request): return Response(status=status.HTTP_204_NO_CONTENT)class ChaosApiView(APIView): @chaos(rate=0.5) def get(self, request): return Response(status=status.HTTP_204_NO_CONTENT)

Requirements

Python (2.7)Django (1.6+, 1.7+, 1.8)

Installation

Install using pip

pip install drf-chaos

Add 'drf_chaos' to your INSTALLED_APPS setting.

INSTALLED_APPS = ( ... 'drf_chaos',)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:管理后台脚手架,实现了RBAC, 和一个简单的SQLALCHEMY的Mapper工具,基于tornado框架
下一篇:Wx-tools是基于微信公众平台API的轻量级框架。
相关文章

 发表评论

暂时没有评论,来抢沙发吧~