コード例 #1
0
 def test_create_with_status_and_reason(self):
     res = Response('not found', 404, reason='NOT FOUND')
     self.assertEqual(res.status_code, 404)
     self.assertEqual(res.headers, {})
     self.assertEqual(res.reason, 'NOT FOUND')
     self.assertEqual(res.body, b'not found')
     fd = FakeStreamAsync()
     _run(res.write(fd))
     self.assertIn(b'HTTP/1.0 404 NOT FOUND\r\n', fd.response)
コード例 #2
0
 def test_create_with_reason(self):
     res = Response('foo', reason='ALL GOOD!')
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers, {})
     self.assertEqual(res.reason, 'ALL GOOD!')
     self.assertEqual(res.body, b'foo')
     fd = FakeStreamAsync()
     _run(res.write(fd))
     self.assertIn(b'HTTP/1.0 200 ALL GOOD!\r\n', fd.response)
コード例 #3
0
 def test_send_file(self):
     res = Response.send_file('tests/files/test.txt',
                              content_type='text/html')
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers['Content-Type'], 'text/html')
     fd = FakeStreamAsync()
     _run(res.write(fd))
     self.assertEqual(
         fd.response,
         b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nfoo\n')
コード例 #4
0
 def test_create_from_bytes(self):
     res = Response(b'foo')
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers, {})
     self.assertEqual(res.body, b'foo')
     fd = FakeStreamAsync()
     _run(res.write(fd))
     self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
     self.assertIn(b'Content-Length: 3\r\n', fd.response)
     self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
     self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo'))
コード例 #5
0
 def test_create_empty(self):
     res = Response(headers={'X-Foo': 'Bar'})
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers, {'X-Foo': 'Bar'})
     self.assertEqual(res.body, b'')
     fd = FakeStreamAsync()
     _run(res.write(fd))
     self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
     self.assertIn(b'X-Foo: Bar\r\n', fd.response)
     self.assertIn(b'Content-Length: 0\r\n', fd.response)
     self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
     self.assertTrue(fd.response.endswith(b'\r\n\r\n'))
コード例 #6
0
    def test_create_json(self):
        res = Response({'foo': 'bar'})
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.headers, {'Content-Type': 'application/json'})
        self.assertEqual(res.body, b'{"foo": "bar"}')
        fd = FakeStreamAsync()
        _run(res.write(fd))
        self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
        self.assertIn(b'Content-Length: 14\r\n', fd.response)
        self.assertIn(b'Content-Type: application/json\r\n', fd.response)
        self.assertTrue(fd.response.endswith(b'\r\n\r\n{"foo": "bar"}'))

        res = Response([1, '2'])
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.headers, {'Content-Type': 'application/json'})
        self.assertEqual(res.body, b'[1, "2"]')
        fd = FakeStreamAsync()
        _run(res.write(fd))
        self.assertIn(b'HTTP/1.0 200 OK\r\n', fd.response)
        self.assertIn(b'Content-Length: 8\r\n', fd.response)
        self.assertIn(b'Content-Type: application/json\r\n', fd.response)
        self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))