def __call__(self, result): if result == None: raise HTTPException(404) return Result( content=json_encode(result), content_type='application/json; charset=UTF-8', http_status=200, )
def _fallback_app(self, env, start_resp): if env['REQUEST_METHOD'] == 'POST': # This used to fail since nudge had already read the FP body = env['wsgi.input'].read() else: body = json.json_encode({'success': True}) start_resp(200, [('Content-Type', 'application/json; charset=utf8')]) return [body + '\r\n']
def _fallback_app(self, env, start_resp): if env["REQUEST_METHOD"] == "POST": # This used to fail since nudge had already read the FP body = env["wsgi.input"].read() else: body = json.json_encode({"success": True}) start_resp(200, [("Content-Type", "application/json; charset=utf8")]) return [body + "\r\n"]
def test_fallback_app_used_post_body(self): endpoints = [Endpoint(name="", method="GET", uri="/test", function=self._nudge_func)] sp = ServicePublisher(endpoints=endpoints, fallbackapp=self._fallback_app) body = json.json_encode({"success": True}) + "\r\n" req = create_req("POST", "/not-test", body=body) resp = MockResponse(req, 200) result = sp(req, resp.start_response) self.assertEqual({"success": True}, json.json_decode(result[0]))
def __call__(self, result): if result == None: raise HTTPException(404) if isinstance(result, (types.ListType, types.TupleType)): raise SecurityException( 'Results that encode as json arrays are not '+\ 'allowed for security concerns' ) return Result( content=json_encode(result), content_type='application/json; charset=UTF-8', http_status=200, )
def test_fallback_app_used_post_body(self): endpoints = [ Endpoint( name='', method='GET', uri='/test', function=self._nudge_func, ) ] sp = ServicePublisher(endpoints=endpoints, fallbackapp=self._fallback_app) body = json.json_encode({'success': True}) + '\r\n' req = create_req('POST', '/not-test', body=body) resp = MockResponse(req, 200) result = sp(req, resp.start_response) self.assertEqual({'success': True}, json.json_decode(result[0]))
def test_encode_custom_class(self): class Bar(json.JsonSerializable): __dict__ = {"foo":"bar"} class Foo(json.JsonSerializable): __dict__ = {"blah":Bar()} self.assertEqual('{"blah": {"foo": "bar"}}', json.json_encode(Foo()))
def test_encode_normal(self): class Foo(json.JsonSerializable): __dict__ = {"test":1} data = {"test_that":Foo(), "testThis":2,"blah":"woot","things":[12,123,1234],"this":{"me":"my"}} data_str = '{"testThis": 2, "test_that": {"test": 1}, "this": {"me": "my"}, "things": [12, 123, 1234], "blah": "woot"}' self.assertEqual(data_str, json.json_encode(data))
def test_encode_date(self): data = datetime.datetime.now() print "data.ctime", data.ctime() print "json_encode data", json.json_encode(data) self.assertEqual('"'+data.ctime()+'"', json.json_encode(data))
def test_encode(self): data = '{"test_that":3, "testThis":2,"blah":"woot","things":[12,123,1234],"this":{"me":"my"}}' result = json.Dictomatic.wrap(data) data2 = json.json_encode(result) result2 = json.Dictomatic.wrap(data2) self.assertEqual(result, result2)
def test_encode_fail(self): class Foo(): def __repr__(self): return '{"test":1}' json.json_encode(Foo())