Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcusk19 committed Mar 1, 2024
1 parent 4085663 commit 98b365b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions endpoints/test/test_request_redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import unittest

from werkzeug.routing.exceptions import RequestRedirect

import endpoints.decorated
from app import app


class TestRequestRedirect(unittest.TestCase):
def setUp(self):

self.app = app.test_client()
self.app.application.config["PROPAGATE_EXCEPTIONS"] = True
self.app.application.config["TRAP_HTTP_EXCEPTIONS"] = True
self.app.base_url = "localhost:8080"
self.ctx = app.test_request_context()
self.ctx.__enter__()

def tearDown(self):
self.app.application.config["TRAP_HTTP_EXCEPTIONS"] = False
self.ctx.__exit__(True, None, None)

def test_handle_request_redirect(self):
# add a fake route to raise exception
@self.app.application.route("/raise-request-redirect")
def raise_request_redirect():
raise RequestRedirect("somepath")

# add error handler to test client
@self.app.application.errorhandler(RequestRedirect)
def handle_request_redirect(e):
return endpoints.decorated.handle_bad_redirect(e)

path = "/raise-request-redirect"
response = self.app.get(path)
print(response.get_data(as_text=True))
resp_data = json.loads(response.get_data(as_text=True))
self.assertEqual(response.status_code, 308)
self.assertEqual(resp_data["message"], "bad path, there may be a trailing slash")
self.assertEqual(resp_data["new_url"], "somepath")

0 comments on commit 98b365b

Please sign in to comment.