HttpTestServer:用于测试的HTTP(s)和SMTP服务器,可以在Python程序中运行

网友投稿 552 2022-10-31

HttpTestServer:用于测试的HTTP(s)和SMTP服务器,可以在Python程序中运行

HttpTestServer:用于测试的HTTP(s)和SMTP服务器,可以在Python程序中运行

HttpTestServer

HTTP(s) and SMTP servers which can be run within a Python process. Serving from different thread along with application and tests, exposing a simple thread-safe API, so the calling code can control how the server behaves.

Sometimes integration tests cannot do with mocking the socket.socket function avoiding real networking, this partially solves the problem by providing a real server which is easy to use and can perform real network communication in a controlled and reliable way.

Features:

Runs in a different thread along with your tests.Control server responses and behaviour.Access to server internal state and data after or during the request.HTTPs support, it bundles a self-signed certificate useful for testing.SMTP support which will collect and parse all your outgoing email.History of all performed requests/responses.

Supports python 2.7 3.2, 3.3 and 3.4

Functions

Functions that return a running server instance:

>>> server = start_server()>>> server.host'127.0.0.1'

Or context managers for limited use:

>>> with http_server() as server:... server.host'127.0.0.1'

>>> with smtp_server() as server:... server.inbox[]

Mixin classes

Mixins that include an working server as self.server.

import requestsfrom httptestserver import HttpsTestServerclass TestApplication(HttpsTestServer): # Test what was actually get by the server def test_it_should_send_headers(self): headers = {'key': 'value'} requests.get(self.default_url, headers=headers) assert self.server.data['headers']['key'] == 'value' # Control server responses def test_it_should_parse_json_response(self): self.server.data['headers'] = {'Content-Type': 'application/json'} self.server.data['response_content'] = "{'key': 'value'}" response = requests.get(self.default_url) assert response.json() == {'key': 'value'} # Make the server behave as you want def test_it_should_raise_timeout_at_2s_wait(self): self.server.data['response_timeout'] = 2 try: requests.get(self.default_url, timeout=1) except requests.exceptions.Timeout: pass else: assert False # Access to server's requests/responses history def test_it_should_make_two_requests(self): requests.get(self.default_url) requests.get(self.default_url + '2') assert len(self.server.history) == 2 assert self.server.history[-1]['path'] == self.default_url + '2'

Development

In order get a development environment, create a virtualenv and install the desired requirements.

virtualenv envenv/bin/activatepip install -r dev-requirements.txt

The included certificate was generated using SSL:

openssl req -new -x509 -keyout server.pem -out server.pem -days 40000 -nodes

Tests

To run the tests just use tox or nose:

tox

nosetests

Documentation

To generate the documentation change to the docs directory and run make. You need to install the sphinx and changelog packages in order to be able to run the makefile.

cd docsmake htmlopen build/html/index.html

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

上一篇:【1032】Sharing(25分)
下一篇:【1002】A+B for Polynomials (25 分)
相关文章

 发表评论

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