def test_status(self): r = Response() for code, status in BaseHTTPRequestHandler.responses.items(): r.code = code self.assertEqual(status[0], r.status) r.code = None r.status = None r = Response() r.code = 1000 self.assertEqual("UNKNOWN", r.status)
def test_code(self): r = Response() self.assertEqual(204, r.code) r.body = "this is the body" self.assertEqual(200, r.code) r.code = 404 self.assertEqual(404, r.code) r.body = "this is the body 2" self.assertEqual(404, r.code) r.body = None self.assertEqual(404, r.code) # now let's test defaults del (r._code) self.assertEqual(204, r.code) r.body = '' self.assertEqual(200, r.code) r.body = {} self.assertEqual(200, r.code)
def test_code(self): r = Response() self.assertEqual(204, r.code) r.body = "this is the body" self.assertEqual(200, r.code) r.code = 404 self.assertEqual(404, r.code) r.body = "this is the body 2" self.assertEqual(404, r.code) r.body = None self.assertEqual(404, r.code) # now let's test defaults del(r._code) self.assertEqual(204, r.code) r.body = '' self.assertEqual(200, r.code) r.body = {} self.assertEqual(200, r.code)
def test_body(self): b = {'foo': 'bar'} r = Response() r.headers['Content-Type'] = 'plain/text' self.assertEqual('', r.body) r.body = b self.assertEqual(str(b), r.body) r = Response() r.headers['Content-Type'] = 'application/json' r.body = b self.assertEqual(json.dumps(b), r.body) r = Response() r.headers['Content-Type'] = 'plain/text' self.assertEqual('', r.body) self.assertEqual('', r.body) # Make sure it doesn't change r.body = b self.assertEqual(str(b), r.body) r = Response() r.headers['Content-Type'] = 'application/json' r.body = {} self.assertEqual(r.body, "{}") r = Response() r.headers['Content-Type'] = 'application/json' r.body = ValueError("this is the message") r.code = 500 #self.assertEqual(r.body, '{"errno": 500, "errmsg": "this is the message"}') self.assertEqual(r.body, '{"errmsg": "this is the message"}') r.headers['Content-Type'] = '' self.assertEqual(r.body, "this is the message") r = Response() r.headers['Content-Type'] = 'application/json' r.body = None self.assertEqual( '', r.body) # was getting "null" when content-type was set to json
def test_body(self): b = {'foo': 'bar'} r = Response() r.headers['Content-Type'] = 'plain/text' self.assertEqual('', r.body) r.body = b self.assertEqual(String(b), r.body) r = Response() r.headers['Content-Type'] = 'application/json' r.body = b self.assertEqual(json.dumps(b), r.body) r = Response() r.headers['Content-Type'] = 'plain/text' self.assertEqual('', r.body) self.assertEqual('', r.body) # Make sure it doesn't change r.body = b self.assertEqual(String(b), r.body) r = Response() r.headers['Content-Type'] = 'application/json' r.body = {} self.assertEqual(r.body, "{}") r = Response() r.headers['Content-Type'] = 'application/json' r.body = ValueError("this is the message") r.code = 500 #self.assertEqual(r.body, '{"errno": 500, "errmsg": "this is the message"}') self.assertEqual(r.body, '{"errmsg": "this is the message"}') r.headers['Content-Type'] = '' self.assertEqual("this is the message", r.body) r = Response() r.headers['Content-Type'] = 'application/json' r.body = None self.assertEqual('', r.body) # was getting "null" when content-type was set to json