Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Tests for main placebo interface and functionality."""
import json
import unittest
import requests
from placebo import Placebo
class GetMock(Placebo):
# Test related data that will be used in tests.
item = {'name': 'Huseyin', 'last_name': 'Yilmaz'}
item2 = {'name': 'Mert', 'last_name': 'Yilmaz'}
url2 = 'http://www.example2.com/api/item'
# Data for placebo interface
url = 'http://www.example.com/api/item'
body = json.dumps(item)
headers = {'content-type': 'application/json',
'custom-header': 'OK'}
class StringValuesTestCase(unittest.TestCase):
@GetMock.decorate
def test_get(self):
response = requests.get(GetMock.url)
import json
from placebo import Placebo
class GetMovieValidResponse(Placebo):
# mock related data. That will be used in tests.
title = 'matrix'
year = 1999
url = 'http://www.omdbapi.com/'
body = json.dumps({
'Actors': 'Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving', # noqa
'Awards': 'Won 4 Oscars. Another 33 wins & 44 nominations.',
'Country': 'USA',
'Director': 'Lana Wachowski, Lilly Wachowski',
'Genre': 'Action, Sci-Fi',
'Language': 'English',
'Metascore': '73',
'Plot': 'A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.', # noqa
'Poster': 'http://ia.media-imdb.com/images/M/MV5BMTkxNDYxOTA4M15BMl5BanBnXkFtZTgwNTk0NzQxMTE@._V1_SX300.jpg', # noqa
from placebo import Placebo
from placebo.utils.importutils import import_string
httpretty_path = 'placebo.backends.httprettybackend.get_decorator'
httmock_path = 'placebo.backends.httmockbackend.get_decorator'
# Get value from environment.
backend_str = os.environ.get('PLACEBO_BACKEND', httmock_path)
# There are some some unexpected behaviors we get from httpretty
# so some tests must be disabled for httpretty
is_httpretty = (backend_str == httpretty_path)
is_httmock = (backend_str == httmock_path)
backend = import_string(backend_str)
class BasePlacebo(Placebo):
backend = backend
def remove_common_headers(headers):
common_headers = ['content-type', 'content-length']
for header in common_headers:
if header in headers:
del headers[header]
return headers
if not response.ok:
raise ItemException('List API returned an error')
else:
return response.json()
###################
# PLACEBO OBJECTS #
###################
class SimplePlacebo(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
class SimplePlaceboWithAllFields(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
status = 200
method = 'GET'
headers = {'custom-header': 'custom'}
class DynamicPlacebo(Placebo):
backend = get_decorator
url_regex = re.compile('^http://www.acme.com/items/(?P\d+)/$')
def url(self):
return parse.ParseResult(
scheme='http',
class SimplePlacebo(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
class SimplePlaceboWithAllFields(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
status = 200
method = 'GET'
headers = {'custom-header': 'custom'}
class DynamicPlacebo(Placebo):
backend = get_decorator
url_regex = re.compile('^http://www.acme.com/items/(?P\d+)/$')
def url(self):
return parse.ParseResult(
scheme='http',
netloc=r'www\.acme\.com',
path=r'^/items/(\w+)/$',
params='',
query='',
fragment='')
def method(self):
return 'GET'
return response.json()
def get_item(self, item_id):
url = self.item_url % item_id
response = requests.get(url)
if not response.ok:
raise ItemException('List API returned an error')
else:
return response.json()
###################
# PLACEBO OBJECTS #
###################
class SimplePlacebo(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
class SimplePlaceboWithAllFields(Placebo):
url = 'http://www.acme.com/items/'
body = '[{"id": 1}, {"id": 2}, {"id": 3}]'
status = 200
method = 'GET'
headers = {'custom-header': 'custom'}
class DynamicPlacebo(Placebo):
backend = get_decorator