Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_when_method_is_invalid(self):
request = RequestNode(
{"method": "xxx", "name": "foo", "path": "http:foo.com"},
endpoint=EndpointNode({"name": "foo", "requests": [{}]}),
)
with pytest.raises(HTTPMethodNotAllowedError) as excinfo:
request.http_method
assert (
str(excinfo.value) == "HTTP method not supported: XXX. "
"Supported methods: ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')."
def test_should_raise_http_method_not_allowed_error(self, api_tree):
requests_maker = RequestsMakerFactory(api_tree=api_tree)
request_node = requests_maker.api_tree.request_nodes[0]
with pytest.raises(HTTPMethodNotAllowedError) as excinfo:
requests_maker.make_request(request_node)
assert (
str(excinfo.value)
== "HTTP method not supported: PET. Supported methods: "
"('GET', 'POST', 'PUT', 'PATCH', 'DELETE')."
def http_method(self):
method = self.spec.get("method", "get").upper()
if method not in self.ALLOWED_HTTP_METHODS:
raise HTTPMethodNotAllowedError(method, self.ALLOWED_HTTP_METHODS)
return method
def make_request(self, request):
method = request.method.upper()
if method not in self.ALLOWED_HTTP_METHODS:
raise HTTPMethodNotAllowedError(method, self.ALLOWED_HTTP_METHODS)
return requests.request(
method,
request.url,
headers=request.headers,
params=request.params,
json=request.body,
allow_redirects=False,
)
def http_method(self):
method = self.spec.get(METHOD_KEY, "get").upper()
if method not in self.ALLOWED_HTTP_METHODS:
raise HTTPMethodNotAllowedError(method, self.ALLOWED_HTTP_METHODS)
return method
def make_request(self, request):
method = request.method.upper()
if method not in self.ALLOWED_HTTP_METHODS:
raise HTTPMethodNotAllowedError(method, self.ALLOWED_HTTP_METHODS)
return requests.request(
method,
request.url,
headers=request.headers,
params=request.params,
json=request.body,
allow_redirects=False,
)
def __init__(self, method, allowed_methos, *args):
message = (
f"HTTP method not supported: {method}. Supported methods: {allowed_methos}."
)
super(HTTPMethodNotAllowedError, self).__init__(message, *args)