Skip to content

Commit

Permalink
fixes #508
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Sep 7, 2022
1 parent 389616e commit 25990dd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jpcore/justpy_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def check_demo(self):
self.is_demo = False
if "Demo(" or "Demo (" in self.source:
endpoint_match = re.search(
"""Demo[ ]?[(]["'](.*)["'],(.*?)(,.*)*[)]""", self.source
"""Demo[ ]?[(]["'](.*)["'],\s*(.*?)(,.*)*[)]""", self.source
)
if endpoint_match:
self.description = endpoint_match.group(1)
Expand Down
9 changes: 5 additions & 4 deletions justpy/justpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from starlette.applications import Starlette
from starlette.responses import JSONResponse, Response
from starlette.responses import HTMLResponse, JSONResponse, Response
from starlette.responses import PlainTextResponse
from starlette.endpoints import WebSocketEndpoint
from starlette.endpoints import HTTPEndpoint
Expand Down Expand Up @@ -82,7 +82,7 @@
AGGRID_ENTERPRISE = config("AGGRID_ENTERPRISE", cast=bool, default=False)

NO_INTERNET = config("NO_INTERNET", cast=bool, default=True)

HTML_404_PAGE = "justpy is sorry - that path doesn't exist"

def create_component_file_list():
file_list = []
Expand Down Expand Up @@ -198,8 +198,9 @@ async def get(self, request):
new_cookie = True
logging.debug(f"New session_id created: {request.session_id}")
func = JpRoute.get_func_for_request(request)
if func:
func_to_run = func
if not func:
return HTMLResponse(content=HTML_404_PAGE, status_code=404)
func_to_run = func
func_parameters = len(inspect.signature(func_to_run).parameters)
assert (
func_parameters < 2
Expand Down
55 changes: 41 additions & 14 deletions tests/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from justpy.routing import JpRoute
from tests.basetest import Basetest

@jp.SetRoute('/greet/{name}')
def greeting_function(request):
wp = jp.WebPage()
name=f"""{request.path_params["name"]}"""
wp.add(jp.P(text=f'Hello there, {name}!', classes='text-5xl m-2'))
return wp

@jp.SetRoute("/bye", name="bye")
def bye_function(_request):
Expand All @@ -32,35 +38,56 @@ class TestRouteAndUrlFor(Basetest):
"""
test the url_for functionality
"""

def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
self.app=jp.app

def testRoute(self):
"""
Test the routing
"""
self.assertEqual(2, len(JpRoute.routes_by_path))
self.assertEqual(3, len(JpRoute.routes_by_path))
debug = self.debug
# debug=True
for route in JpRoute.routes_by_path.values():
route_as_text = str(route)
if debug:
print(route_as_text)
for path in ["/bye", "/hello"]:
for path in ["/bye", "/hello","/greet/{name}"]:
self.assertTrue(path in JpRoute.routes_by_path)

def checkResponse(self,path,expected_code=200):
with TestClient(self.app) as client:
response = client.get(path)
self.assertEqual(expected_code, response.status_code)
return response

def testUrlFor(self):
"""
Test url for functionality
"""
app = jp.app
#@TODO - not implemented yet
pass

def testInvalidPath(self):
'''
test handling an invalid path
'''
response=self.checkResponse("/invalidpath",404)
debug=True
if debug:
print(response.text)
self.assertEqual(jp.HTML_404_PAGE,response.text)

def testHtmlContent(self):
# see https://www.starlette.io/testclient/
with TestClient(app) as client:
response = client.get("/hello")
self.assertEqual(200, response.status_code)
debug = self.debug
debug = True
lines = response.text.split("\n")
if debug:
print(response.text)
print(f"{len(lines)} lines")
self.assertTrue(response.text.startswith("<!DOCTYPE html>"))
self.assertTrue(len(lines) < 500)
response=self.checkResponse("/hello")
debug = self.debug
debug = True
lines = response.text.split("\n")
if debug:
print(response.text)
print(f"{len(lines)} lines")
self.assertTrue(response.text.startswith("<!DOCTYPE html>"))
self.assertTrue(len(lines) < 500)

0 comments on commit 25990dd

Please sign in to comment.