Hello community, here is the log from the commit of package python-httmock for openSUSE:Factory checked in at 2019-03-05 12:23:24 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/python-httmock (Old) and /work/SRC/openSUSE:Factory/.python-httmock.new.28833 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "python-httmock" Tue Mar 5 12:23:24 2019 rev:3 rq:681408 version:1.3.0 Changes: -------- --- /work/SRC/openSUSE:Factory/python-httmock/python-httmock.changes 2018-12-24 11:38:10.377623506 +0100 +++ /work/SRC/openSUSE:Factory/.python-httmock.new.28833/python-httmock.changes 2019-03-05 12:23:31.792874220 +0100 @@ -1,0 +2,8 @@ +Mon Mar 4 13:29:03 UTC 2019 - Tomáš Chvátal <[email protected]> + +- Update to 1.3.0: + * Minor fixes collection +- Switch to github for tests +- Run tests + +------------------------------------------------------------------- Old: ---- httmock-1.2.6.tar.gz New: ---- 1.3.0.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ python-httmock.spec ++++++ --- /var/tmp/diff_new_pack.qgXegX/_old 2019-03-05 12:23:32.312874061 +0100 +++ /var/tmp/diff_new_pack.qgXegX/_new 2019-03-05 12:23:32.312874061 +0100 @@ -1,7 +1,7 @@ # # spec file for package python-httmock # -# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany. +# Copyright (c) 2019 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -18,13 +18,13 @@ %{?!python_module:%define python_module() python-%{**} python3-%{**}} Name: python-httmock -Version: 1.2.6 +Version: 1.3.0 Release: 0 Summary: A mocking library for requests License: Apache-2.0 Group: Development/Languages/Python Url: https://github.com/patrys/httmock -Source: https://files.pythonhosted.org/packages/source/h/httmock/httmock-%{version}.tar.gz +Source: https://github.com/patrys/httmock/archive/%{version}.tar.gz BuildRequires: %{python_module setuptools} BuildRequires: fdupes BuildRequires: python-rpm-macros @@ -53,6 +53,9 @@ %python_install %python_expand %fdupes %{buildroot}%{$python_sitelib} +%check +%python_expand PYTHONPATH=%{buildroot}%{$python_sitelib} $python -m unittest discover + %files %{python_files} %doc README.md %license LICENSE ++++++ httmock-1.2.6.tar.gz -> 1.3.0.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/.coveragerc new/httmock-1.3.0/.coveragerc --- old/httmock-1.2.6/.coveragerc 1970-01-01 01:00:00.000000000 +0100 +++ new/httmock-1.3.0/.coveragerc 2019-01-28 18:19:10.000000000 +0100 @@ -0,0 +1,8 @@ +[run] +omit = tests.py +source = httmock + +[report] +exclude_lines = + pragma: no cover + raise NotImplementedError diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/.gitignore new/httmock-1.3.0/.gitignore --- old/httmock-1.2.6/.gitignore 1970-01-01 01:00:00.000000000 +0100 +++ new/httmock-1.3.0/.gitignore 2019-01-28 18:19:10.000000000 +0100 @@ -0,0 +1,35 @@ +*.py[cod] + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/.travis.yml new/httmock-1.3.0/.travis.yml --- old/httmock-1.2.6/.travis.yml 1970-01-01 01:00:00.000000000 +0100 +++ new/httmock-1.3.0/.travis.yml 2019-01-28 18:19:10.000000000 +0100 @@ -0,0 +1,10 @@ +language: python +python: + - 2.7 + - pypy + - 3.4 + - 3.5 + - 3.6 +install: + - "pip install requests" +script: nosetests diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/PKG-INFO new/httmock-1.3.0/PKG-INFO --- old/httmock-1.2.6/PKG-INFO 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/PKG-INFO 1970-01-01 01:00:00.000000000 +0100 @@ -1,94 +0,0 @@ -Metadata-Version: 1.1 -Name: httmock -Version: 1.2.6 -Summary: A mocking library for requests. -Home-page: https://github.com/patrys/httmock -Author: Patryk Zawadzki -Author-email: [email protected] -License: Copyright 2013 Patryk Zawadzki - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -Description: httmock - ======= - - A mocking library for `requests` for Python 2.6, 2.7, 3.2, 3.3 and 3.4. - - Installation - ------------ - - pip install httmock - - Or, if you are a Gentoo user: - - emerge dev-python/httmock - - Usage - ----- - You can use it to mock third-party APIs and test libraries that use `requests` internally, conditionally using mocked replies with the `urlmatch` decorator: - - ```python - from httmock import urlmatch, HTTMock - import requests - - @urlmatch(netloc=r'(.*\.)?google\.com$') - def google_mock(url, request): - return 'Feeling lucky, punk?' - - with HTTMock(google_mock): - r = requests.get('http://google.com/') - print r.content # 'Feeling lucky, punk?' - ``` - - The `all_requests` decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the `requests.Response` object returned: - - ```python - from httmock import all_requests, HTTMock - import requests - - @all_requests - def response_content(url, request): - return {'status_code': 200, - 'content': 'Oh hai'} - - with HTTMock(response_content): - r = requests.get('https://foo_bar') - - print r.status_code - print r.content - ``` - - If you pass in `Set-Cookie` headers, `requests.Response.cookies` will contain the values. You can also use `response` method directly instead of returning a dict: - - ```python - from httmock import all_requests, response, HTTMock - import requests - - @all_requests - def response_content(url, request): - headers = {'content-type': 'application/json', - 'Set-Cookie': 'foo=bar;'} - content = {'message': 'API rate limit exceeded'} - return response(403, content, headers, None, 5, request) - - with HTTMock(response_content): - r = requests.get('https://api.github.com/users/whatever') - - print r.json().get('message') - print r.cookies['foo'] - ``` -Keywords: requests,testing,mock -Platform: UNKNOWN -Classifier: Programming Language :: Python -Classifier: Intended Audience :: Developers -Classifier: Topic :: Software Development :: Testing -Classifier: Operating System :: OS Independent diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/README.md new/httmock-1.3.0/README.md --- old/httmock-1.2.6/README.md 2016-08-31 16:57:55.000000000 +0200 +++ new/httmock-1.3.0/README.md 2019-01-28 18:19:10.000000000 +0100 @@ -1,7 +1,7 @@ httmock ======= -A mocking library for `requests` for Python 2.6, 2.7, 3.2, 3.3 and 3.4. +A mocking library for `requests` for Python 2.7 and 3.4+. Installation ------------ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.egg-info/PKG-INFO new/httmock-1.3.0/httmock.egg-info/PKG-INFO --- old/httmock-1.2.6/httmock.egg-info/PKG-INFO 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/httmock.egg-info/PKG-INFO 1970-01-01 01:00:00.000000000 +0100 @@ -1,94 +0,0 @@ -Metadata-Version: 1.1 -Name: httmock -Version: 1.2.6 -Summary: A mocking library for requests. -Home-page: https://github.com/patrys/httmock -Author: Patryk Zawadzki -Author-email: [email protected] -License: Copyright 2013 Patryk Zawadzki - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -Description: httmock - ======= - - A mocking library for `requests` for Python 2.6, 2.7, 3.2, 3.3 and 3.4. - - Installation - ------------ - - pip install httmock - - Or, if you are a Gentoo user: - - emerge dev-python/httmock - - Usage - ----- - You can use it to mock third-party APIs and test libraries that use `requests` internally, conditionally using mocked replies with the `urlmatch` decorator: - - ```python - from httmock import urlmatch, HTTMock - import requests - - @urlmatch(netloc=r'(.*\.)?google\.com$') - def google_mock(url, request): - return 'Feeling lucky, punk?' - - with HTTMock(google_mock): - r = requests.get('http://google.com/') - print r.content # 'Feeling lucky, punk?' - ``` - - The `all_requests` decorator doesn't conditionally block real requests. If you return a dictionary, it will map to the `requests.Response` object returned: - - ```python - from httmock import all_requests, HTTMock - import requests - - @all_requests - def response_content(url, request): - return {'status_code': 200, - 'content': 'Oh hai'} - - with HTTMock(response_content): - r = requests.get('https://foo_bar') - - print r.status_code - print r.content - ``` - - If you pass in `Set-Cookie` headers, `requests.Response.cookies` will contain the values. You can also use `response` method directly instead of returning a dict: - - ```python - from httmock import all_requests, response, HTTMock - import requests - - @all_requests - def response_content(url, request): - headers = {'content-type': 'application/json', - 'Set-Cookie': 'foo=bar;'} - content = {'message': 'API rate limit exceeded'} - return response(403, content, headers, None, 5, request) - - with HTTMock(response_content): - r = requests.get('https://api.github.com/users/whatever') - - print r.json().get('message') - print r.cookies['foo'] - ``` -Keywords: requests,testing,mock -Platform: UNKNOWN -Classifier: Programming Language :: Python -Classifier: Intended Audience :: Developers -Classifier: Topic :: Software Development :: Testing -Classifier: Operating System :: OS Independent diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.egg-info/SOURCES.txt new/httmock-1.3.0/httmock.egg-info/SOURCES.txt --- old/httmock-1.2.6/httmock.egg-info/SOURCES.txt 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/httmock.egg-info/SOURCES.txt 1970-01-01 01:00:00.000000000 +0100 @@ -1,10 +0,0 @@ -LICENSE -MANIFEST.in -README.md -httmock.py -setup.py -httmock.egg-info/PKG-INFO -httmock.egg-info/SOURCES.txt -httmock.egg-info/dependency_links.txt -httmock.egg-info/requires.txt -httmock.egg-info/top_level.txt \ No newline at end of file diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.egg-info/dependency_links.txt new/httmock-1.3.0/httmock.egg-info/dependency_links.txt --- old/httmock-1.2.6/httmock.egg-info/dependency_links.txt 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/httmock.egg-info/dependency_links.txt 1970-01-01 01:00:00.000000000 +0100 @@ -1 +0,0 @@ - diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.egg-info/requires.txt new/httmock-1.3.0/httmock.egg-info/requires.txt --- old/httmock-1.2.6/httmock.egg-info/requires.txt 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/httmock.egg-info/requires.txt 1970-01-01 01:00:00.000000000 +0100 @@ -1 +0,0 @@ -requests >= 1.0.0 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.egg-info/top_level.txt new/httmock-1.3.0/httmock.egg-info/top_level.txt --- old/httmock-1.2.6/httmock.egg-info/top_level.txt 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/httmock.egg-info/top_level.txt 1970-01-01 01:00:00.000000000 +0100 @@ -1 +0,0 @@ -httmock diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/httmock.py new/httmock-1.3.0/httmock.py --- old/httmock-1.2.6/httmock.py 2016-12-19 13:22:37.000000000 +0100 +++ new/httmock-1.3.0/httmock.py 2019-01-28 18:19:10.000000000 +0100 @@ -4,7 +4,7 @@ import json import re import requests -from requests import structures +from requests import structures, utils import sys try: import urlparse @@ -49,6 +49,7 @@ res._content = content res._content_consumed = content res.headers = structures.CaseInsensitiveDict(headers or {}) + res.encoding = utils.get_encoding_from_headers(res.headers) res.reason = reason res.elapsed = datetime.timedelta(elapsed) res.request = request @@ -106,7 +107,8 @@ def handler_init_call(handler): setattr(handler, 'call', { 'count': 0, - 'called': False + 'called': False, + 'requests': [] }) @@ -114,7 +116,8 @@ if hasattr(handler, 'call'): handler.call.update({ 'count': 0, - 'called': False + 'called': False, + 'requests': [] }) @@ -124,7 +127,7 @@ finally: handler.call['count'] += 1 handler.call['called'] = True - + handler.call['requests'].append(args[1]) def remember_called(func): handler_init_call(func) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/setup.cfg new/httmock-1.3.0/setup.cfg --- old/httmock-1.2.6/setup.cfg 2016-12-19 13:23:43.000000000 +0100 +++ new/httmock-1.3.0/setup.cfg 1970-01-01 01:00:00.000000000 +0100 @@ -1,5 +0,0 @@ -[egg_info] -tag_build = -tag_date = 0 -tag_svn_revision = 0 - diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/setup.py new/httmock-1.3.0/setup.py --- old/httmock-1.2.6/setup.py 2016-12-19 13:22:54.000000000 +0100 +++ new/httmock-1.3.0/setup.py 2019-01-28 18:19:10.000000000 +0100 @@ -11,7 +11,7 @@ setup( name='httmock', - version='1.2.6', + version='1.3.0', description='A mocking library for requests.', author='Patryk Zawadzki', author_email='[email protected]', @@ -19,7 +19,8 @@ py_modules=['httmock'], keywords=['requests', 'testing', 'mock'], classifiers=[ - 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 3', 'Intended Audience :: Developers', 'Topic :: Software Development :: Testing', 'Operating System :: OS Independent'], diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/httmock-1.2.6/tests.py new/httmock-1.3.0/tests.py --- old/httmock-1.2.6/tests.py 1970-01-01 01:00:00.000000000 +0100 +++ new/httmock-1.3.0/tests.py 2019-01-28 18:19:10.000000000 +0100 @@ -0,0 +1,370 @@ +# -*- coding: utf-8 -*- +import requests +import unittest + +from httmock import (all_requests, response, urlmatch, with_httmock, HTTMock, + remember_called, text_type, binary_type) + + +@urlmatch(scheme='swallow') +def unmatched_scheme(url, request): + raise AssertionError('This is outrageous') + + +@urlmatch(path=r'^never$') +def unmatched_path(url, request): + raise AssertionError('This is outrageous') + + +@urlmatch(method='post') +def unmatched_method(url, request): + raise AssertionError('This is outrageous') + + +@urlmatch(netloc=r'(.*\.)?google\.com$', path=r'^/$') +def google_mock(url, request): + return 'Hello from Google' + + +@urlmatch(netloc=r'(.*\.)?google\.com$', path=r'^/$') +@remember_called +def google_mock_count(url, request): + return 'Hello from Google' + + +@urlmatch(scheme='http', netloc=r'(.*\.)?facebook\.com$') +def facebook_mock(url, request): + return 'Hello from Facebook' + + +@urlmatch(scheme='http', netloc=r'(.*\.)?facebook\.com$') +@remember_called +def facebook_mock_count(url, request): + return 'Hello from Facebook' + +@urlmatch(netloc=r'(.*\.)?google\.com$', path=r'^/$', method='POST') +@remember_called +def google_mock_store_requests(url, request): + return 'Posting at Google' + + +@all_requests +def charset_utf8(url, request): + return { + 'content': u'Motörhead'.encode('utf-8'), + 'status_code': 200, + 'headers': { + 'Content-Type': 'text/plain; charset=utf-8' + } + } + + +def any_mock(url, request): + return 'Hello from %s' % (url.netloc,) + + +def dict_any_mock(url, request): + return { + 'content': 'Hello from %s' % (url.netloc,), + 'status_code': 200 + } + + +def example_400_response(url, response): + r = requests.Response() + r.status_code = 400 + r._content = b'Bad request.' + return r + + +class MockTest(unittest.TestCase): + + def test_return_type(self): + with HTTMock(any_mock): + r = requests.get('http://domain.com/') + self.assertTrue(isinstance(r, requests.Response)) + self.assertTrue(isinstance(r.content, binary_type)) + self.assertTrue(isinstance(r.text, text_type)) + + def test_scheme_fallback(self): + with HTTMock(unmatched_scheme, any_mock): + r = requests.get('http://example.com/') + self.assertEqual(r.content, b'Hello from example.com') + + def test_path_fallback(self): + with HTTMock(unmatched_path, any_mock): + r = requests.get('http://example.com/') + self.assertEqual(r.content, b'Hello from example.com') + + def test_method_fallback(self): + with HTTMock(unmatched_method, any_mock): + r = requests.get('http://example.com/') + self.assertEqual(r.content, b'Hello from example.com') + + def test_netloc_fallback(self): + with HTTMock(google_mock, facebook_mock): + r = requests.get('http://google.com/') + self.assertEqual(r.content, b'Hello from Google') + with HTTMock(google_mock, facebook_mock): + r = requests.get('http://facebook.com/') + self.assertEqual(r.content, b'Hello from Facebook') + + def test_400_response(self): + with HTTMock(example_400_response): + r = requests.get('http://example.com/') + self.assertEqual(r.status_code, 400) + self.assertEqual(r.content, b'Bad request.') + + def test_real_request_fallback(self): + with HTTMock(any_mock): + with HTTMock(google_mock, facebook_mock): + r = requests.get('http://example.com/') + self.assertEqual(r.status_code, 200) + self.assertEqual(r.content, b'Hello from example.com') + + def test_invalid_intercept_response_raises_value_error(self): + @all_requests + def response_content(url, request): + return -1 + with HTTMock(response_content): + self.assertRaises(TypeError, requests.get, 'http://example.com/') + + def test_encoding_from_contenttype(self): + with HTTMock(charset_utf8): + r = requests.get('http://example.com/') + self.assertEqual(r.encoding, 'utf-8') + self.assertEqual(r.text, u'Motörhead') + self.assertEqual(r.content, r.text.encode('utf-8')) + + +class DecoratorTest(unittest.TestCase): + + @with_httmock(any_mock) + def test_decorator(self): + r = requests.get('http://example.com/') + self.assertEqual(r.content, b'Hello from example.com') + + @with_httmock(any_mock) + def test_iter_lines(self): + r = requests.get('http://example.com/') + self.assertEqual(list(r.iter_lines()), + [b'Hello from example.com']) + + +class AllRequestsDecoratorTest(unittest.TestCase): + + def test_all_requests_response(self): + @all_requests + def response_content(url, request): + return {'status_code': 200, 'content': 'Oh hai'} + with HTTMock(response_content): + r = requests.get('https://example.com/') + self.assertEqual(r.status_code, 200) + self.assertEqual(r.content, b'Oh hai') + + def test_all_str_response(self): + @all_requests + def response_content(url, request): + return 'Hello' + with HTTMock(response_content): + r = requests.get('https://example.com/') + self.assertEqual(r.content, b'Hello') + + +class AllRequestsMethodDecoratorTest(unittest.TestCase): + @all_requests + def response_content(self, url, request): + return {'status_code': 200, 'content': 'Oh hai'} + + def test_all_requests_response(self): + with HTTMock(self.response_content): + r = requests.get('https://example.com/') + self.assertEqual(r.status_code, 200) + self.assertEqual(r.content, b'Oh hai') + + @all_requests + def string_response_content(self, url, request): + return 'Hello' + + def test_all_str_response(self): + with HTTMock(self.string_response_content): + r = requests.get('https://example.com/') + self.assertEqual(r.content, b'Hello') + + +class UrlMatchMethodDecoratorTest(unittest.TestCase): + @urlmatch(netloc=r'(.*\.)?google\.com$', path=r'^/$') + def google_mock(self, url, request): + return 'Hello from Google' + + @urlmatch(scheme='http', netloc=r'(.*\.)?facebook\.com$') + def facebook_mock(self, url, request): + return 'Hello from Facebook' + + @urlmatch(query=r'.*page=test') + def query_page_mock(self, url, request): + return 'Hello from test page' + + def test_netloc_fallback(self): + with HTTMock(self.google_mock, facebook_mock): + r = requests.get('http://google.com/') + self.assertEqual(r.content, b'Hello from Google') + with HTTMock(self.google_mock, facebook_mock): + r = requests.get('http://facebook.com/') + self.assertEqual(r.content, b'Hello from Facebook') + + def test_query(self): + with HTTMock(self.query_page_mock, self.google_mock): + r = requests.get('http://google.com/?page=test') + r2 = requests.get('http://google.com/') + self.assertEqual(r.content, b'Hello from test page') + self.assertEqual(r2.content, b'Hello from Google') + + +class ResponseTest(unittest.TestCase): + + content = {'name': 'foo', 'ipv4addr': '127.0.0.1'} + content_list = list(content.keys()) + + def test_response_auto_json(self): + r = response(0, self.content) + self.assertTrue(isinstance(r.content, binary_type)) + self.assertTrue(isinstance(r.text, text_type)) + self.assertEqual(r.json(), self.content) + r = response(0, self.content_list) + self.assertEqual(r.json(), self.content_list) + + def test_response_status_code(self): + r = response(200) + self.assertEqual(r.status_code, 200) + + def test_response_headers(self): + r = response(200, None, {'Content-Type': 'application/json'}) + self.assertEqual(r.headers['content-type'], 'application/json') + + def test_response_cookies(self): + @all_requests + def response_content(url, request): + return response(200, 'Foo', {'Set-Cookie': 'foo=bar;'}, + request=request) + with HTTMock(response_content): + r = requests.get('https://example.com/') + self.assertEqual(len(r.cookies), 1) + self.assertTrue('foo' in r.cookies) + self.assertEqual(r.cookies['foo'], 'bar') + + def test_response_session_cookies(self): + @all_requests + def response_content(url, request): + return response(200, 'Foo', {'Set-Cookie': 'foo=bar;'}, + request=request) + session = requests.Session() + with HTTMock(response_content): + r = session.get('https://foo_bar') + self.assertEqual(len(r.cookies), 1) + self.assertTrue('foo' in r.cookies) + self.assertEqual(r.cookies['foo'], 'bar') + self.assertEqual(len(session.cookies), 1) + self.assertTrue('foo' in session.cookies) + self.assertEqual(session.cookies['foo'], 'bar') + + def test_python_version_encoding_differences(self): + # Previous behavior would result in this test failing in Python3 due + # to how requests checks for utf-8 JSON content in requests.utils with: + # + # TypeError: Can't convert 'bytes' object to str implicitly + @all_requests + def get_mock(url, request): + return {'content': self.content, + 'headers': {'content-type': 'application/json'}, + 'status_code': 200, + 'elapsed': 5} + + with HTTMock(get_mock): + response = requests.get('http://example.com/') + self.assertEqual(self.content, response.json()) + + def test_mock_redirect(self): + @urlmatch(netloc='example.com') + def get_mock(url, request): + return {'status_code': 302, + 'headers': {'Location': 'http://google.com/'}} + + with HTTMock(get_mock, google_mock): + response = requests.get('http://example.com/') + self.assertEqual(len(response.history), 1) + self.assertEqual(response.content, b'Hello from Google') + + +class StreamTest(unittest.TestCase): + @with_httmock(any_mock) + def test_stream_request(self): + r = requests.get('http://domain.com/', stream=True) + self.assertEqual(r.raw.read(), b'Hello from domain.com') + + @with_httmock(dict_any_mock) + def test_stream_request_with_dict_mock(self): + r = requests.get('http://domain.com/', stream=True) + self.assertEqual(r.raw.read(), b'Hello from domain.com') + + @with_httmock(any_mock) + def test_non_stream_request(self): + r = requests.get('http://domain.com/') + self.assertEqual(r.raw.read(), b'') + + +class RememberCalledTest(unittest.TestCase): + + @staticmethod + def several_calls(count, method, *args, **kwargs): + results = [] + for _ in range(count): + results.append(method(*args, **kwargs)) + return results + + def test_several_calls(self): + with HTTMock(google_mock_count, facebook_mock_count): + results = self.several_calls( + 3, requests.get, 'http://facebook.com/') + + self.assertTrue(facebook_mock_count.call['called']) + self.assertEqual(facebook_mock_count.call['count'], 3) + + self.assertFalse(google_mock_count.call['called']) + self.assertEqual(google_mock_count.call['count'], 0) + + for r in results: + self.assertEqual(r.content, b'Hello from Facebook') + + # Negative case: cleanup call data + with HTTMock(facebook_mock_count): + results = self.several_calls( + 1, requests.get, 'http://facebook.com/') + + self.assertEquals(facebook_mock_count.call['count'], 1) + + @with_httmock(google_mock_count, facebook_mock_count) + def test_several_call_decorated(self): + results = self.several_calls(3, requests.get, 'http://facebook.com/') + + self.assertTrue(facebook_mock_count.call['called']) + self.assertEqual(facebook_mock_count.call['count'], 3) + + self.assertFalse(google_mock_count.call['called']) + self.assertEqual(google_mock_count.call['count'], 0) + + for r in results: + self.assertEqual(r.content, b'Hello from Facebook') + + self.several_calls(1, requests.get, 'http://facebook.com/') + self.assertEquals(facebook_mock_count.call['count'], 4) + + def test_store_several_requests(self): + with HTTMock(google_mock_store_requests): + payload = {"query": "foo"} + requests.post('http://google.com', data=payload) + + self.assertTrue(google_mock_store_requests.call['called']) + self.assertEqual(google_mock_store_requests.call['count'], 1) + request = google_mock_store_requests.call['requests'][0] + self.assertEqual(request.body, 'query=foo')
