def test_jsonp(): # Check that response is wrapped in callback request = MyDummyRequest([b"/"]) request.addArg(b"id", b"1234") request.addArg(b"callback", b"myCallback") resource = EchoArgsPage() yield _render(resource, request) response_str = b"".join(request.written) assert response_str.startswith(b"myCallback(") assert response_str.endswith(b")") # Reject invalid callback request = MyDummyRequest(b"/") evil_callback = b"alert('hi');" request.addArg(b"id", b"1234") request.addArg(b"callback", evil_callback) resource = EchoArgsPage() yield _render(resource, request) response_str = b"".join(request.written) assert evil_callback not in response_str assert request.responseCode == 400
def test_special_character_in_parameter(): # This triggered a server-side exception in retwist <= 0.4.1 for Python 2 request = MyDummyRequest([b"/"]) special_chars = u"Spécial cháracters" request.addArg(b"id", special_chars.encode("utf-8")) resource = EchoArgsPage() yield _render(resource, request) response_str = b"".join(request.written) response = json.loads(response_str) assert response["id"] == special_chars
def test_json_resource(): """ Test regular synchronous JSON rendering. """ request = MyDummyRequest([b"/"]) request.addArg(b"id", b"1234") request.addArg(b"show_details", b"false") resource = EchoArgsPage() yield _render(resource, request) response_str = b"".join(request.written) response = json.loads(response_str.decode()) assert response == { "id": "1234", "show_details": False, }