Exemplo n.º 1
0
 def test_req_body_empty(self):
     input = uio.BytesIO(('POST /some-resource?qs=value HTTP/1.1\n'
                          'Host: domain.com\n'
                          'Content-Length: 0\n'
                          '\n'))
     req = uhttp.Request(input)
     body = req.body().read(1024)
     self.assertEqual(b'', body)
Exemplo n.º 2
0
 def test_req_body(self):
     body = 'body line 1\nbody line 2\n'
     input = uio.BytesIO(('POST /some-resource?qs=value HTTP/1.1\n'
                          'Host: domain.com\n' +
                          'Content-Length: %d\n\n' % len(body) + body))
     req = uhttp.Request(input)
     got_body = req.body().read(1024)
     self.assertEqual(body, got_body.decode())
Exemplo n.º 3
0
 def test_req_body_json(self):
     json_payload = ('{\n' '"key":"value"' '}\n')
     input = uio.BytesIO(
         ('POST /some-resource?qs=value HTTP/1.1\n'
          'Host: domain.com\n' +
          'Content-Length: %d\n\n' % len(json_payload) + json_payload))
     req = uhttp.Request(input)
     body = ujson.load(req.body())
     self.assertEqual({'key': 'value'}, body)
Exemplo n.º 4
0
 def test_bad_method(self):
     input = uio.BytesIO(('SOMETHING /some-resource?qs=value HTTP/1.1\n'
                          'Host: domain.com\n'
                          '\n'))
     thrown = False
     err = None
     try:
         uhttp.Request(input)
     except uhttp.HTTPException as e:
         thrown = True
         err = e
     self.assertTrue(thrown, 'No error thrown')
     self.assertEqual(uhttp.HTTP_STATUS_NOT_IMPLEMENTED, err.status)
     self.assertEqual('Unrecognized method', err.body)
Exemplo n.º 5
0
 def test_init(self):
     input = uio.BytesIO(('GET /some-resource?qs=value HTTP/1.1\n'
                          'Host: domain.com\n'
                          'X-Header-1: value1\n'
                          'X-Header-2: value2\n'
                          '\n'))
     req = uhttp.Request(input)
     self.assertEqual("GET", req.method)
     self.assertEqual("/some-resource?qs=value", req.uri)
     self.assertEqual(req.headers, {
         'host': 'domain.com',
         'x-header-1': 'value1',
         'x-header-2': 'value2'
     })
Exemplo n.º 6
0
 def test_req_body_no_content_length(self):
     input = uio.BytesIO(('POST /some-resource?qs=value HTTP/1.1\n'
                          'Host: domain.com\n'
                          '\n'
                          'body line 1\nbody line 2\n'))
     req = uhttp.Request(input)
     err = None
     try:
         req.body()
     except uhttp.HTTPException as e:
         err = e
     self.assertIsNotNone(err, 'err has not been raised')
     self.assertEqual(uhttp.HTTP_STATUS_LENGTH_REQUIRED, err.status)
     self.assertEqual(
         uhttp.HTTP_REASON_PHRASE[uhttp.HTTP_STATUS_LENGTH_REQUIRED],
         err.body)