用于测试环境的http服务器

spoof的Python项目详细描述


https://img.shields.io/pypi/v/spoof.svghttps://img.shields.io/pypi/wheel/spoof.svghttps://img.shields.io/pypi/pyversions/spoof.svghttps://img.shields.io/github/license/lexsca/spoof.svghttps://codecov.io/gh/lexsca/spoof/branch/master/graph/badge.svg

spoof是一个用python编写的http服务器,用于测试环境中 模拟底层调用不是一个选项,或者在需要 在套接字上侦听的实际http服务器。你好,功能测试!

与典型的http服务器不同,其中特定的方法和路径组合是 预先配置,spoof接受all请求并发送队列 响应,如果队列为空则为默认响应,如果没有则为错误响应 已配置默认响应。请求可以在发送响应后进行检查。

兼容性

Spoof在Python2.7、3.4到3.7上运行,并且没有外部依赖项。

多个欺骗http服务器可以同时运行,默认情况下,端口 number是下一个可用的未使用端口。安装了openssl后,spoof可以 还提供一个ssl/tls http服务器。完全支持IPv6。

快速启动

将多个响应排队、验证内容和请求路径:

importrequestsimportspoofwithspoof.HTTPServer()ashttpd:responses=[[200,[('Content-Type','application/json')],'{"id": 1111}'],[200,[('Content-Type','application/json')],'{"id": 2222}'],]httpd.queueResponse(*responses)httpd.defaultResponse=[404,[],'Not found']assertrequests.get(httpd.url+'/path').json()=={'id':1111}assertrequests.get(httpd.url+'/alt/path').json()=={'id':2222}assertrequests.get(httpd.url+'/oops').status_code==404assert[r.pathforrinhttpd.requests]==['/path','/alt/path','/oops']

将回调设置为默认响应:

importrequestsimportspoofwithspoof.HTTPServer()ashttpd:httpd.defaultResponse=lambdarequest:[200,[],request.path]assertrequests.get(httpd.url+'/alt').content==b'/alt'

使用ssl测试排队响应:

importrequestsimportspoofwithspoof.SelfSignedSSLContext()asselfSigned:withspoof.HTTPServer(sslContext=selfSigned.sslContext)ashttpd:httpd.queueResponse([200,[],'No self-signed cert warning!'])response=requests.get(httpd.url+'/path',verify=selfSigned.certFile)asserthttpd.requests[-1].method=='GET'asserthttpd.requests[-1].path=='/path'assertresponse.content==b'No self-signed cert warning!'

SSL警告

有些图书馆喜欢 Requests会抱怨的 大声或拒绝使用自签名ssl连接到http服务器 证书。处理此问题的首选方法是使用验证 属性在请求会话中信任证书:

importrequestsimportspoofcert,key=spoof.SSLContext.createSelfSignedCert()sslContext=spoof.SSLContext.fromCertChain(cert,key)httpd=spoof.HTTPServer(sslContext=sslContext)httpd.queueResponse([200,[],'OK'])httpd.start()# trust self-signed certificatesession=requests.Session()session.verify=certresponse=session.get(httpd.url+'/uri/path')print(response.status_code,response.content)httpd.stop()

如果验证证书不是一个选项,则另一种解决方法 这是monkeypatch测试代码中的请求库。例如:

importrequestscertVerify=requests.adapters.HTTPAdapter.cert_verifydefcertNoVerify(self,conn,url,verify,cert):returncertVerify(self,conn,url,False,cert)requests.adapters.HTTPAdapter.cert_verify=certNoVerifyrequests.packages.urllib3.disable_warnings()

另一个常见的例子是直接利用ssl的库。单向 要解决此问题,请将默认上下文全局设置为 未经证实。例如:

importssltry:createUnverifiedHttpsContext=ssl._create_unverified_contextexceptAttributeError:# ignore if ssl context not verified by defaultpasselse:ssl._create_default_https_context=createUnverifiedHttpsContext

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java:关于Eclipse的几个问题   java在Apache Camel Netty组件中使用SSL,并带有JVM默认密钥库文件   Java解析未格式化字符串   java前缀字符串资源标识符,用于在运行时选择备用版本   java为什么三元表达式在if语句正常工作时不更新值?   java我需要帮助程序以特定格式输出文本文件。   java Spring Tomcat启动问题setCatalinaBase(Ljava/io/File;)   递归java数独回溯递归   java如何解决构造函数上的错误?   getEngineByName(“JavaScript”)在Java 11上返回null   java如何将对象添加到数组并打印它?   java我在层泄漏反模式中吗?我该怎么办?   java动态移动带有ImageIcon的JLabel时,会在帧周围跳跃   JavaXSLT转换释放了特殊字符   java BuffereImage会降低性能   DL4J中的java回归预测下一个时间步   Java 11中的spring boot复制JAR导致以下错误   如何通过编程将图像编码到Java视频文件中?   如何使用Java更新Pentaho转换连接数据?