def test_givenMissingOrigin_whenHandlingPreflightRequest_thenReturn400BadRequest(
            self):
        request = RequestBuilder().headers({}).build()
        self.cors_handler = CorsPreflightRequestHandler(
            Headers({'Access-Control-Allow-Origin': 'http://api.jivago.io'}))

        response = self.cors_handler.invoke(request)

        self.assertEqual(400, response.status)
Exemple #2
0
class CorsRule(RouterConfigRule):
    def __init__(self, path: str, cors_headers: dict):
        self.path = path
        self.cors_headers = Headers(cors_headers)

    def matches(self, path: str) -> bool:
        return path.startswith(self.path)

    def takes_precedence_over(self, other: "CorsRule") -> bool:
        return len(self.path) >= len(other.path)

    def inject_headers(self, headers: Headers) -> None:
        for key, value in self.cors_headers.items():
            headers[key] = value
    def build_request(self, env: dict) -> Request:
        """Builds the Jivago request object from the WSGI environment dictionary."""
        raw_headers = Stream(env.items()).filter(
            lambda key, value: key.startswith("HTTP_")).map(
                lambda key, value: (re.sub("^HTTP_", "", key).title().replace(
                    "_", "-"), value)).toDict()
        request_size = int(
            env.get('CONTENT_LENGTH')) if 'CONTENT_LENGTH' in env else 0

        if 'CONTENT_TYPE' in env:
            raw_headers['Content-Type'] = env['CONTENT_TYPE']

        return Request(env['REQUEST_METHOD'], env['PATH_INFO'],
                       Headers(raw_headers), env['QUERY_STRING'],
                       env['wsgi.input'].read(request_size))
Exemple #4
0
    def test_whenGettingHeaderValue_thenMatchRegardlessOfCase(self):
        headers = Headers({"Foo-Bar": "baz"})

        self.assertEqual("baz", headers['FOO_BAR'])
        self.assertEqual("baz", headers['Foo-Bar'])
        self.assertEqual("baz", headers['FOo-baR'])
Exemple #5
0
 def __init__(self, path: str, cors_headers: dict):
     self.path = path
     self.cors_headers = Headers(cors_headers)
 def preflight(self):
     headers = Headers()
     headers['Access-Control-Allow-Methods'] = "POST, GET, OPTIONS, DELETE"
     headers['Access-Control-Allow-Headers'] = '*'
     return Response(200, headers, "")
 def get_with_headers(self, headers: Headers) -> list:
     return headers.items()
 def setUp(self):
     self.cors_headers = Headers({"Access-Control-Allow-Origin": "*"})
     self.cors_handler = CorsPreflightRequestHandler(self.cors_headers)
Exemple #9
0
 def __init__(self):
     self._method = "GET"
     self._path = "/"
     self._headers = Headers()
     self._queryString = ""
     self._body = ""
Exemple #10
0
 def headers(self, headers: dict) -> "RequestBuilder":
     self._headers = Headers(headers)
     return self
Exemple #11
0
 def get_stream(self) -> Response:
     # A Response object can also be manually created to provide further control over transport parameters.
     return Response(202, Headers(), StreamingResponseBody(self.generate_bytes()))
Exemple #12
0
 def __init__(self, status: int, headers: Union[dict, Headers], body):
     self.status = status
     self.headers = Headers(headers) if isinstance(headers, dict) else headers
     self.body = body