Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
)
with context('Response._detect_error'):
with before.all:
http_response = Stub()
request = Stub(Request)
self.client = Stub(Client)
self.client.log_level = 'silent'
self.response = Response(http_response, request)
with it('should raise a network error if no status code was found'):
self.response.status_code = None
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(NetworkError)
)
with it('should raise a server error if a 500 occured'):
self.response.status_code = 500
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(ServerError)
)
from mamba import description, it
from expects import expect, be_none, equal
from amadeus import Client, Location
with description('Client') as self:
with it('should exist'):
expect(Client).not_to(be_none)
with it('should have helper locations'):
expect(Location).not_to(be_none)
expect(Location.ANY).to(equal('AIRPORT,CITY'))
http_response.read().returns('{ "data" : { "a" : 1 } }')
self.client.http = method_returning(http_response)
response = self.client._unauthenticated_request(
'GET', '/foo', {}, None
)
expect(self.client.http).to(have_been_called.once)
expect(self.response).to(equal(self.response))
with it('should catch a HTTPError'):
self.client.http = method_raising(URLError('Error'))
expect(
lambda: self.client._unauthenticated_request(
'GET', '/foo', {}, None
)
).to(raise_error(ResponseError))
with it('should log when in debug mode'):
with Stub() as http_response:
http_response.code = 200
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{ "data" : { "a" : 1 } }')
self.client.http = method_returning(http_response)
self.client.logger = Spy()
self.client.log_level = 'debug'
response = self.client._unauthenticated_request(
'GET', '/foo', {}, None
)
expect(self.client.logger.debug).to(have_been_called.twice)
from mamba import description, context, it, before
from expects import expect, equal
from doublex import Stub, Spy
from doublex_expects import have_been_called_with, have_been_called
from amadeus import NetworkError, Response, Client
with description('ResponseError') as self:
with context('str(error)'):
with it('should be undefine if no response is present'):
error = NetworkError(None)
expect(str(error)).to(equal('[---]'))
with it('should just return the code if no data is present'):
response = Stub(Response)
response.parsed = True
response.result = {}
response.status_code = 400
error = NetworkError(response)
expect(str(error)).to(equal('[400]'))
with it('should be rich if errors are present'):
response = Stub(Response)
response.parsed = True
response.result = {
'errors': [
{
'detail': 'This field must be filled.',
'source': {'parameter': 'departureDate'}
},
try:
from urllib.error import URLError
except ImportError: # pragma: no cover
from urllib2 import Request as URLError # pragma: no cover
from amadeus import Client, Response, ResponseError
with description('HTTP') as self:
with before.each:
self.client = Client(
client_id='123',
client_secret='234',
log_level='silent'
)
self.response = Stub(Response)
self.request_method = method_returning(self.response)
with context('Client.get'):
with it('should pass all details to the request method'):
self.client.request = self.request_method
response = self.client.get('/foo', foo='bar')
expect(response).to(equal(self.response))
expect(self.client.request).to(
have_been_called_with('GET', '/foo', {'foo': 'bar'})
)
with context('Client.post'):
with it('should pass all details to the request method'):
self.client.request = self.request_method
response = self.client.post('/foo', {'foo': 'bar'})
expect(response).to(equal(self.response))
with description('Pagination') as self:
with before.all:
# Initialize the client and pagination object
self.next_response = Stub(Response)
with Spy(Client) as client:
client.request(ANY_ARG).returns(self.next_response)
self.client = client
self.pagination = Pagination()
self.pagination.request = self.client.request
# Initialize the previous request made and response received
request = Stub(Request)
request.verb = self.verb = 'GET'
request.path = self.path = '/a'
request.params = self.params = {'a': 'b'}
self.response = Response(Stub('http_response'), request)
with context('Client.previous'):
with it('should create a new request with the page and call it'):
self.response.result = {
'meta': {'links': {'previous': 'http://f.co?page=1'}}
}
next_response = self.pagination.previous(self.response)
expect(self.client.request).to(
have_been_called_with('GET', '/a',
{'a': 'b', 'page': {'offset': '1'}})
)
expect(next_response).to(equal(self.next_response))
with it('should return nil if the page was not found'):
self.response.result = {'meta': {'links': {}}}
)
http_response.read().returns(u'Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should parse no body for a missing content type'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns([])
http_response.read().returns('Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should raise a ParseError if it could not be parsed'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response = response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
expect(response).to(be_a(Response))
with it('should parse the body for content type vnd.amadeus+json'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/vnd.amadeus+json')]
)
http_response.read().returns('{ "data" : { "a" : 1 } }')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.data).to(equal({'a': 1}))
with it('should parse no body for other content types'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'plain/text')]
)
http_response.read().returns(u'Hello World')
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
response = Response(http_response, self.request)
response._parse(self.client)
expect(response.body).to(equal('Hello World'))
expect(response.result).to(be(None))
expect(response.data).to(be(None))
with it('should raise a ParseError if it could not be parsed'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
)
with context('Response._detect_error'):
with before.all:
http_response = Stub()
request = Stub(Request)
self.client = Stub(Client)
self.client.log_level = 'silent'
self.response = Response(http_response, request)
with it('should raise a network error if no status code was found'):
self.response.status_code = None
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
[('Content-Type', 'application/json')]
)
http_response.read().returns('{')
response = Response(http_response, self.request)
expect(lambda: response._parse(self.client)).to(
raise_error(ParserError)
)
with context('Response._detect_error'):
with before.all:
http_response = Stub()
request = Stub(Request)
self.client = Stub(Client)
self.client.log_level = 'silent'
self.response = Response(http_response, request)
with it('should raise a network error if no status code was found'):
self.response.status_code = None
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(NetworkError)
)
with it('should raise a server error if a 500 occured'):
self.response.status_code = 500
self.response.parsed = False
expect(lambda: self.response._detect_error(self.client)).to(
raise_error(ServerError)
)
with it('should raise a authentication error if a 401 happened'):