Exemple #1
0
        def _handler(client, request):
            url = request.url
            parts = urlparse(url)

            full_path = '%s?%s' % (parts.path, parts.query)
            path_info = paths.get(full_path)

            if path_info is None:
                path_info = paths.get(parts.path)

                if path_info is None:
                    path_info = paths.get(None)

                    if path_info is None:
                        self.fail('Unexpected path "%s"' % full_path)

            status_code = path_info.get('status_code') or 200
            payload = path_info.get('payload') or b''
            headers = path_info.get('headers') or {}

            if status_code >= 400:
                raise HTTPError(url, status_code, '', headers,
                                io.BytesIO(payload))
            else:
                return HostingServiceHTTPResponse(request=request,
                                                  url=url,
                                                  data=payload,
                                                  headers=headers,
                                                  status_code=status_code)
    def test_json_with_non_json_response(self):
        """Testing HostingServiceHTTPResponse.json with non-JSON response"""
        request = HostingServiceHTTPRequest('http://example.com')
        response = HostingServiceHTTPResponse(request=request,
                                              url='http://example.com',
                                              data=b'{[(',
                                              headers={},
                                              status_code=200)

        with self.assertRaises(ValueError):
            response.json
Exemple #3
0
 def test_json(self):
     """Testing HostingServiceHTTPResponse.json"""
     request = HostingServiceHTTPRequest('http://example.com')
     response = HostingServiceHTTPResponse(request=request,
                                           url='http://example.com',
                                           data=b'{"a": 1, "b": 2}',
                                           headers={},
                                           status_code=200)
     self.assertEqual(response.json, {
         'a': 1,
         'b': 2,
     })
Exemple #4
0
    def open(self):
        method = self.method

        if method in ('DELETE', 'HEAD'):
            data = None
        else:
            data = b'{"key": "test response"}'

        if method == 'DELETE':
            status_code = 204
        elif method == 'POST':
            status_code = 201
        else:
            status_code = 200

        return HostingServiceHTTPResponse(request=self,
                                          url=self.url,
                                          data=data,
                                          headers={
                                              str('Test-header'): str('Value'),
                                          },
                                          status_code=status_code)
    def test_get_header(self):
        """Testing HostingServiceHTTPRequest.get_header"""
        request = HostingServiceHTTPRequest('http://example.com')
        response = HostingServiceHTTPResponse(
            request=request,
            url=request.url,
            status_code=200,
            data=b'',
            headers={
                str('Authorization'): str('Basic abc123'),
                str('Content-Length'): str('123'),
            })

        self.assertEqual(response.get_header('Authorization'), 'Basic abc123')
        self.assertEqual(response.get_header('AUTHORIZATION'), 'Basic abc123')
        self.assertEqual(response.get_header('authorization'), 'Basic abc123')

        self.assertEqual(response.get_header('Content-Length'), '123')
        self.assertEqual(response.get_header('CONTENT-LENGTH'), '123')
        self.assertEqual(response.get_header('content-length'), '123')