%global _empty_manifest_terminate_build 0 Name: python-httptest Version: 0.1.5 Release: 1 Summary: Add unit tests to your http client License: MIT URL: https://github.com/pdxjohnny/httptest Source0: https://mirrors.nju.edu.cn/pypi/web/packages/47/a9/88e894eb5e2af97d3673d0be0dfb692d823777d75cf89c186a360cfdbe79/httptest-0.1.5.tar.gz BuildArch: noarch Requires: python3-build Requires: python3-twine Requires: python3-setuptools-scm[toml] %description # httptest [![Tests](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/pdxjohnny/httptest/branch/master/graph/badge.svg)](https://codecov.io/gh/pdxjohnny/httptest) HTTP testing inspired by golang's httptest package. Supports wrapping asyncio coroutine functions (`async def`). ## Usage ### Context Manager ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) def main(): with httptest.Server(TestHTTPServer) as ts: with urllib.request.urlopen(ts.url()) as f: assert f.read().decode('utf-8') == "what up" if __name__ == '__main__': main() ``` ### Simple HTTP Server Handler ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server(TestHTTPServer) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` ### Serve Files ```python import pathlib import unittest import http.server import urllib.request import httptest FILE_PATH = pathlib.Path(__file__) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server( lambda *args: http.server.SimpleHTTPRequestHandler( *args, directory=FILE_PATH.parent ) ) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url() + FILE_PATH.name) as f: self.assertEqual(f.read().decode('utf-8'), FILE_PATH.read_text()) if __name__ == '__main__': unittest.main() ``` ### Asyncio Support Asyncio support for the unittest package hasn't yet landed in Python. [python/issue32972](https://bugs.python.org/issue32972). It should land in 3.8, check it out [here](https://github.com/python/cpython/pull/13386). If you want a quick way to add `asyncio` test cases you can import the helper from [intel/dffml](https://github.com/intel/dffml). ```python import sys import unittest import urllib.request if sys.version_info.minor == 3 \ and sys.version_info.minor <= 7: from dffml.util.asynctestcase import AsyncTestCase else: # In Python 3.8 from unittest import IsolatedAsyncioTestCase as AsyncTestCase import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(AsyncTestCase): @httptest.Server(TestHTTPServer) async def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` In your project's `setup.py`, add `dffml` in `tests_require`. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0', 'dffml>=0.4.0.post0' ] ) ``` ## Auto Install If you're making a python package, you'll want to add `httptest` to your `setup.py` file's `tests_require` section. This way, when your run `python setup.py test` setuptools will install `httptest` for you in a package local directory, if it's not already installed. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0' ] ) ``` %package -n python3-httptest Summary: Add unit tests to your http client Provides: python-httptest BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-httptest # httptest [![Tests](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/pdxjohnny/httptest/branch/master/graph/badge.svg)](https://codecov.io/gh/pdxjohnny/httptest) HTTP testing inspired by golang's httptest package. Supports wrapping asyncio coroutine functions (`async def`). ## Usage ### Context Manager ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) def main(): with httptest.Server(TestHTTPServer) as ts: with urllib.request.urlopen(ts.url()) as f: assert f.read().decode('utf-8') == "what up" if __name__ == '__main__': main() ``` ### Simple HTTP Server Handler ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server(TestHTTPServer) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` ### Serve Files ```python import pathlib import unittest import http.server import urllib.request import httptest FILE_PATH = pathlib.Path(__file__) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server( lambda *args: http.server.SimpleHTTPRequestHandler( *args, directory=FILE_PATH.parent ) ) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url() + FILE_PATH.name) as f: self.assertEqual(f.read().decode('utf-8'), FILE_PATH.read_text()) if __name__ == '__main__': unittest.main() ``` ### Asyncio Support Asyncio support for the unittest package hasn't yet landed in Python. [python/issue32972](https://bugs.python.org/issue32972). It should land in 3.8, check it out [here](https://github.com/python/cpython/pull/13386). If you want a quick way to add `asyncio` test cases you can import the helper from [intel/dffml](https://github.com/intel/dffml). ```python import sys import unittest import urllib.request if sys.version_info.minor == 3 \ and sys.version_info.minor <= 7: from dffml.util.asynctestcase import AsyncTestCase else: # In Python 3.8 from unittest import IsolatedAsyncioTestCase as AsyncTestCase import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(AsyncTestCase): @httptest.Server(TestHTTPServer) async def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` In your project's `setup.py`, add `dffml` in `tests_require`. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0', 'dffml>=0.4.0.post0' ] ) ``` ## Auto Install If you're making a python package, you'll want to add `httptest` to your `setup.py` file's `tests_require` section. This way, when your run `python setup.py test` setuptools will install `httptest` for you in a package local directory, if it's not already installed. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0' ] ) ``` %package help Summary: Development documents and examples for httptest Provides: python3-httptest-doc %description help # httptest [![Tests](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/pdxjohnny/httptest/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/pdxjohnny/httptest/branch/master/graph/badge.svg)](https://codecov.io/gh/pdxjohnny/httptest) HTTP testing inspired by golang's httptest package. Supports wrapping asyncio coroutine functions (`async def`). ## Usage ### Context Manager ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) def main(): with httptest.Server(TestHTTPServer) as ts: with urllib.request.urlopen(ts.url()) as f: assert f.read().decode('utf-8') == "what up" if __name__ == '__main__': main() ``` ### Simple HTTP Server Handler ```python import unittest import urllib.request import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server(TestHTTPServer) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` ### Serve Files ```python import pathlib import unittest import http.server import urllib.request import httptest FILE_PATH = pathlib.Path(__file__) class TestHTTPTestMethods(unittest.TestCase): @httptest.Server( lambda *args: http.server.SimpleHTTPRequestHandler( *args, directory=FILE_PATH.parent ) ) def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url() + FILE_PATH.name) as f: self.assertEqual(f.read().decode('utf-8'), FILE_PATH.read_text()) if __name__ == '__main__': unittest.main() ``` ### Asyncio Support Asyncio support for the unittest package hasn't yet landed in Python. [python/issue32972](https://bugs.python.org/issue32972). It should land in 3.8, check it out [here](https://github.com/python/cpython/pull/13386). If you want a quick way to add `asyncio` test cases you can import the helper from [intel/dffml](https://github.com/intel/dffml). ```python import sys import unittest import urllib.request if sys.version_info.minor == 3 \ and sys.version_info.minor <= 7: from dffml.util.asynctestcase import AsyncTestCase else: # In Python 3.8 from unittest import IsolatedAsyncioTestCase as AsyncTestCase import httptest class TestHTTPServer(httptest.Handler): def do_GET(self): contents = "what up".encode() self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("Content-length", len(contents)) self.end_headers() self.wfile.write(contents) class TestHTTPTestMethods(AsyncTestCase): @httptest.Server(TestHTTPServer) async def test_call_response(self, ts=httptest.NoServer()): with urllib.request.urlopen(ts.url()) as f: self.assertEqual(f.read().decode('utf-8'), "what up") if __name__ == '__main__': unittest.main() ``` In your project's `setup.py`, add `dffml` in `tests_require`. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0', 'dffml>=0.4.0.post0' ] ) ``` ## Auto Install If you're making a python package, you'll want to add `httptest` to your `setup.py` file's `tests_require` section. This way, when your run `python setup.py test` setuptools will install `httptest` for you in a package local directory, if it's not already installed. ```python setup( name='your_package', ... tests_require=[ 'httptest>=0.1.0' ] ) ``` %prep %autosetup -n httptest-0.1.5 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-httptest -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Wed Apr 12 2023 Python_Bot - 0.1.5-1 - Package Spec generated