Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with description('Request') as self:
with before.all:
self.host = 'example.com'
self.verb = 'GET'
self.path = '/foo/bar'
self.params = {'foo': 'bar'}
self.bearer_token = 'Bearer 123'
self.client_version = '1.2.3'
self.lang_version = '2.3.4'
self.app_id = 'amadeus-cli'
self.app_version = '3.4.5'
self.ssl = True
self.port = 443
self.request = Request({
'host': self.host,
'verb': self.verb,
'path': self.path,
'params': self.params,
'bearer_token': self.bearer_token,
'client_version': self.client_version,
'language_version': self.lang_version,
'app_id': self.app_id,
'app_version': self.app_version,
'ssl': self.ssl,
'port': self.port
})
with context('.__init__'):
with it('should store the params'):
expect(self.request.host).to(equal(self.host))
from amadeus import Client, Response, Request
from amadeus.mixins.pagination import Pagination
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'}})
)
from mamba import description, context, it
from expects import expect, equal
from doublex import Stub
from amadeus import Response, Request
with description('Response') as self:
with context('.__init__'):
with it('should take a http response object and request'):
http_response = Stub('http_response')
request = Stub(Request)
response = Response(http_response, request)
expect(response.http_response).to(equal(http_response))
expect(response.request).to(equal(request))
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(
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, context, it, before
from expects import expect, equal, raise_error, be_a, be
from doublex import Stub
from amadeus import Response, Request, Client
from amadeus import NetworkError, ServerError, AuthenticationError
from amadeus import NotFoundError, ClientError, ParserError
with description('Mixins/Parser') as self:
with context('Response._parse'):
with context('with a JSON content type'):
with before.all:
self.request = Stub(Request)
with Stub(Client) as client:
client.log_level = 'silent'
self.client = client
with it('should parse the body'):
with Stub() as http_response:
http_response.status_code = '200'
http_response.getheaders().returns(
[('Content-Type', 'application/json')]
)
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))
'client_version': self.client_version,
'language_version': self.lang_version,
'app_id': self.app_id,
'app_version': self.app_version,
'port': self.port,
'ssl': self.ssl
})
expect(self.request.http_request).to(be_a(HTTPRequest))
expect(self.request.http_request.get_full_url()).to(equal(
'https://example.com/foo/bar'))
expect(self.request.http_request.data).to(
equal(b'{\"foo\": \"bar\"}'))
with it('should handle a custom scheme and port'):
self.request = Request({
'host': self.host,
'verb': 'POST',
'path': self.path,
'params': self.params,
'bearer_token': self.bearer_token,
'client_version': self.client_version,
'language_version': self.lang_version,
'app_id': self.app_id,
'app_version': self.app_version,
'port': 8080,
'ssl': False
})
expect(self.request.http_request.get_full_url()).to(equal(
'http://example.com:8080/foo/bar'))