Skip to content
Snippets Groups Projects
Commit ab5095b2 authored by Ugo Albanese's avatar Ugo Albanese Committed by Viktor Vorobev
Browse files

Merged in fix_error_500_wrong_url (pull request #153)

[NRRPLT-8866] Requests to unregistered URL paths results in error 404

* [NRRPLT-8866] Requests to unregistered URL paths results in error 404


Approved-by: Viktor Vorobev
parent ad69143c
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,7 @@ To mark a rest request as thread-safe, decorate the Resource function handling t
from builtins import object
from threading import Lock
import logging
from werkzeug.exceptions import NotFound
logger = logging.getLogger(__name__)
......@@ -57,9 +58,17 @@ class RestSyncMiddleware(object):
pathinfo = environ.get("PATH_INFO")
method = environ.get("REQUEST_METHOD")
viewfunction, _ = self.app.url_map.bind(
'localhost', default_method=method).match(pathinfo)
viewclass = self.app.view_functions[viewfunction].view_class
try:
viewfunction, _ = self.app.url_map.bind(
'localhost', default_method=method).match(pathinfo)
except NotFound:
return self.wsgi_app(environ, start_response)
try:
viewclass = self.app.view_functions[viewfunction].view_class
except AttributeError:
return self.wsgi_app(environ, start_response)
viewfn = getattr(viewclass, method.lower())
if not hasattr(viewfn, "is_threadsafe"):
......
......@@ -27,6 +27,9 @@ Tests for RestSyncMiddleware.py
import unittest
from unittest.mock import patch, MagicMock
import werkzeug.exceptions
from hbp_nrp_backend.rest_server.RestSyncMiddleware import RestSyncMiddleware
......@@ -36,9 +39,9 @@ class TestRestSyncMiddleWare(unittest.TestCase):
self.env_list = ["path_info", "test_method"]
self.mock_wsgi = MagicMock()
self.mock_app = MagicMock()
viewfunction ="viewfunction"
viewfunction = "viewfunction"
self.mock_map_adapter = MagicMock()
self.mock_map_adapter.match = MagicMock(return_value=(viewfunction,None))
self.mock_map_adapter.match = MagicMock(return_value=(viewfunction, None))
self.mock_app.url_map.bind = MagicMock(return_value=self.mock_map_adapter)
mock_view = MagicMock()
......@@ -47,7 +50,6 @@ class TestRestSyncMiddleWare(unittest.TestCase):
mock_view.view_class = x
self.mock_app.view_functions = {viewfunction: mock_view}
self.mock_env = MagicMock()
self.mock_env.get = MagicMock(side_effect=self.env_list)
self.mock_response = MagicMock()
......@@ -71,6 +73,29 @@ class TestRestSyncMiddleWare(unittest.TestCase):
self.mock_wsgi.assert_called_with(self.mock_env, self.mock_response)
self.assertTrue(rest.threadLock.release.called)
@patch('hbp_nrp_backend.rest_server.RestSyncMiddleware.Lock')
def test_call_unbound_address(self, patch_lock):
self.create_mocks()
self.mock_app.url_map.bind = MagicMock(side_effect=werkzeug.exceptions.NotFound)
# call the class
rest = RestSyncMiddleware(self.mock_wsgi, self.mock_app)
rest(self.mock_env, self.mock_response)
self.mock_app.url_map.bind.assert_called()
self.mock_wsgi.assert_called()
@patch('hbp_nrp_backend.rest_server.RestSyncMiddleware.Lock')
def test_call_no_viewclass(self, patch_lock):
self.create_mocks()
self.mock_app.view_functions = {"viewfunction": None}
# call the class
rest = RestSyncMiddleware(self.mock_wsgi, self.mock_app)
rest(self.mock_env, self.mock_response)
self.mock_app.url_map.bind.assert_called_with('localhost', default_method=self.env_list[1])
self.mock_wsgi.assert_called()
@patch('hbp_nrp_backend.rest_server.RestSyncMiddleware.Lock')
def test_call_is_threadsafe(self, patch_lock):
mock_function = MagicMock()
......@@ -86,7 +111,6 @@ class TestRestSyncMiddleWare(unittest.TestCase):
self.mock_wsgi.assert_called_with(self.mock_env, self.mock_response)
self.assertFalse(rest.threadLock.release.called)
@patch('hbp_nrp_backend.rest_server.RestSyncMiddleware.Lock')
def test_call_works_with_exception(self, patch_lock):
mock_wsgi = MagicMock(side_effect=KeyError)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment