def test_handler_missing_callback_required(self, app): with req(app, '/?param=1'): # It must fail since callback name is not provided # in the 'callback' query param. with pytest.raises(BadRequest): _json_p_handler('x', callbacks=['callback', 'foo'], optional=False)
def test_handler_text_default(self): with self.req("/?callback=foo"): r = _json_p_handler(str("hello")) assert_equals(r.get_data(as_text=True), 'foo("hello");') assert_equals(r.status_code, 200) assert_equals(r.headers["Content-Type"], "application/javascript") # For py 2.x we also test unicode string. if sys.version_info[0] == 2: r = _json_p_handler(unicode("hello"), callbacks=["callback"], optional=False) assert_equals(r.get_data(as_text=True), 'foo("hello");')
def test_handler_text_default(self, app): with req(app, '/?callback=foo'): r = _json_p_handler(str('hello')) assert r.get_data(as_text=True) == 'foo("hello");' assert r.status_code == 200 assert r.headers['Content-Type'] == 'application/javascript' # For py 2.x we also test unicode string. if sys.version_info[0] == 2: r = _json_p_handler(unicode('hello'), callbacks=['callback'], optional=False) assert r.get_data(as_text=True) == 'foo("hello");'
def test_handler_text_default(self): with self.req('/?callback=foo'): r = _json_p_handler(str('hello')) assert_equals(r.get_data(as_text=True), 'foo("hello");') assert_equals(r.status_code, 200) assert_equals(r.headers['Content-Type'], 'application/javascript') # For py 2.x we also test unicode string. if sys.version_info[0] == 2: r = _json_p_handler(unicode('hello'), callbacks=['callback'], optional=False) assert_equals(r.get_data(as_text=True), 'foo("hello");')
def test_handler_missing_callback_optional(self): with self.req('/?param=1'): rv = _json_p_handler({'x': 1}, callbacks=['callback', 'foo'], optional=True) assert_is_instance(rv, Response) assert_equals(rv.json, {'x': 1, 'status': 200})
def test_handler_missing_callback_optional(self, app): with req(app, '/?param=1'): rv = _json_p_handler({'x': 1}, callbacks=['callback', 'foo'], optional=True) assert isinstance(rv, Response) assert rv.json == {'x': 1, 'status': 200}
def test_handler_json(self): with self.req("/?callback=foo"): r = json_response(val=100, add_status_=False) val = _json_p_handler(r, callbacks=["callback"], optional=False) assert_equals(val.get_data(as_text=True), 'foo({"val": 100});') assert_equals(val.status_code, 200) assert_equals(val.headers["Content-Type"], "application/javascript")
def test_handler_json(self): with self.req('/?callback=foo'): r = json_response(val=100, add_status_=False) val = _json_p_handler(r, callbacks=['callback'], optional=False) assert_equals(val.get_data(as_text=True), 'foo({"val": 100});') assert_equals(val.status_code, 200) assert_equals(val.headers['Content-Type'], 'application/javascript')
def test_handler_json(self, app): with req(app, '/?callback=foo'): r = json_response(val=100, add_status_=False) val = _json_p_handler(r, callbacks=['callback'], optional=False) res = val.get_data(as_text=True).replace(' ', '').replace('\n', '') assert res == 'foo({"val":100});' assert val.status_code == 200 assert val.headers['Content-Type'] == 'application/javascript'
def test_handler_missing_callback_required(self): with self.req("/?param=1"): _json_p_handler("x", callbacks=["callback", "foo"], optional=False)
def test_handler_text_quotes2(self, app): with req(app, '/?callback=foo'): r = _json_p_handler(str('hello "Sam"'), add_quotes=True) assert r.get_data(as_text=True) == r'foo("hello \"Sam\"");' assert r.status_code == 200 assert r.headers['Content-Type'] == 'application/javascript'
def test_handler_text_quotes2(self): with self.req("/?callback=foo"): r = _json_p_handler(str('hello "Sam"'), add_quotes=True) assert_equals(r.get_data(as_text=True), r'foo("hello \"Sam\"");') assert_equals(r.status_code, 200) assert_equals(r.headers["Content-Type"], "application/javascript")
def test_handler_text_quotes2(self): with self.req('/?callback=foo'): r = _json_p_handler(str('hello "Sam"'), add_quotes=True) assert_equals(r.get_data(as_text=True), r'foo("hello \"Sam\"");') assert_equals(r.status_code, 200) assert_equals(r.headers['Content-Type'], 'application/javascript')
def test_handler_missing_callback_required(self): with self.req('/?param=1'): _json_p_handler('x', callbacks=['callback', 'foo'], optional=False)
def test_handler_missing_callback_optional(self): with self.req("/?param=1"): rv = _json_p_handler({"x": 1}, callbacks=["callback", "foo"], optional=True) assert_is_instance(rv, Response) assert_equals(rv.json, {"x": 1, "status": 200})