def test_disable_default_redirect_cache(self, mock_request):
        """
        Test disable default redirect cache (by setting default redirect cache to None)
        """
        response0 = Response()
        response0.url = 'http://www.test.com/neverseemeagain'
        response0.status_code = 301
        response0.headers = {
            'Location': 'http://www.test.com/redirect_here',
        }

        response1 = Response()
        response1.url = 'http://www.test.com/redirect_here'
        response1.status_code = 200
        response1._content = 'Mocked response content'
        response1.headers = {
            'Vary': 'Accept',
        }
        response1.history = [response0]

        mock_request.return_value = response1

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)

        set_default_redirect_cache(None)

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
Beispiel #2
0
    def test_get_301_circular_redirect(self, mock_request):
        response0 = Response()
        response0.url = 'http://www.test.com/path0'
        response0.status_code = 301
        response0.headers = {'Location': 'http://www.test.com/path1'}

        response1 = Response()
        response1.url = 'http://www.test.com/path1'
        response1.status_code = 301
        response1.headers = {'Location': 'http://www.test.com/path0'}

        response2 = Response()
        response2.url = 'http://www.test.com/path2'
        response2.status_code = 200
        response2._content = 'Mocked response content'
        response2.history = [response0, response1]

        mock_request.return_value = response2


        r = get('http://www.test.com/path0')
        self.assertEqual(mock_request.call_count, 1)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.content, 'Mocked response content')

        with self.assertRaises(TooManyRedirects):
            get('http://www.test.com/path0')
Beispiel #3
0
    def test_get_301_only_once(self, mock_request):
        response0 = Response()
        response0.url = 'http://www.test.com/neverseemeagain'
        response0.status_code = 301
        response0.headers = {
            'Location': 'http://www.test.com/redirect_here',
        }

        response1 = Response()
        response1.url = 'http://www.test.com/redirect_here'
        response1.status_code = 200
        response1._content = 'Mocked response content'
        response1.headers = {
            'Vary': 'Accept',
        }
        response1.history = [response0]

        mock_request.return_value = response1


        r = get('http://www.test.com/neverseemeagain')
        self.assertEqual(mock_request.call_count, 1)
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        self.assertEqual(r.status_code, 200)

        #assert we not make request to 301 again
        r = get('http://www.test.com/neverseemeagain')
        self.assertEqual(mock_request.call_count, 2)
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
        self.assertEqual(r.status_code, 200)
Beispiel #4
0
 def get(url, **kwargs):
     resp_basic_auth = Response()
     resp_basic_auth.status_code = 200
     if "authenticate" in url:
         if kwargs["auth"].password != "PASSWORD!":
             raise Exception("Bad password")
         resp_basic_auth._content = b"TOKEN"
         resp_basic_auth.headers = {"Content-Type": "text/plain"}
     elif "ping" in url:
         token = getattr(kwargs["auth"], "token", None)
         password = getattr(kwargs["auth"], "password", None)
         if token and token != "TOKEN":
             raise Exception("Bad JWT Token")
         if not token and not password:
             raise AuthenticationException(
                 "I'm an Artifactory without anonymous access that "
                 "requires authentication for the ping endpoint and "
                 "I don't return the capabilities")
     elif "search" in url:
         if kwargs["auth"].token != "TOKEN":
             raise Exception("Bad JWT Token")
         resp_basic_auth._content = b'{"results": []}'
         resp_basic_auth.headers = {
             "Content-Type": "application/json"
         }
     else:
         raise Exception("Shouldn't be more remote calls")
     return resp_basic_auth
    def test_get_301_thrice(self, mock_get):
        response0 = Response()
        response0.url = 'http://www.test.com/neverseemeagain'
        response0.status_code = 301
        response0.headers = {
            'Location': 'http://www.test.com/redirect_1',
            }

        response1 = Response()
        response1.url = 'http://www.test.com/redirect_1'
        response1.status_code = 301
        response1.headers = {
            'Location': 'http://www.test.com/redirect_2',
            }

        response2 = Response()
        response2.url = 'http://www.test.com/redirect_2'
        response2.status_code = 301
        response2.headers = {
            'Location': 'http://www.test.com/redirect_3',
            }

        response3 = Response()
        response3.url = 'http://www.test.com/redirect_3'
        response3.status_code = 200
        response3._content = 'Mocked response content'
        response3.headers = {
            'Vary': 'Accept',
            }
        response3.history = [response0, response1, response2]

        mock_get.return_value = response3


        r = get('http://www.test.com/neverseemeagain')
        self.assertEqual(mock_get.call_count, 1)
        mock_get.assert_called_with('http://www.test.com/neverseemeagain')
        self.assertEqual(r.status_code, 200)

        #assert we not make request to 301 again
        r = get('http://www.test.com/neverseemeagain')
        self.assertEqual(mock_get.call_count, 2)
        mock_get.assert_called_with('http://www.test.com/redirect_3')
        self.assertEqual(r.status_code, 200)

        r = get('http://www.test.com/redirect_1')
        self.assertEqual(mock_get.call_count, 3)
        mock_get.assert_called_with('http://www.test.com/redirect_3')
        self.assertEqual(r.status_code, 200)

        r = get('http://www.test.com/redirect_2')
        self.assertEqual(mock_get.call_count, 4)
        mock_get.assert_called_with('http://www.test.com/redirect_3')
        self.assertEqual(r.status_code, 200)

        r = get('http://www.test.com/redirect_3')
        self.assertEqual(mock_get.call_count, 5)
        mock_get.assert_called_with('http://www.test.com/redirect_3')
        self.assertEqual(r.status_code, 200)
Beispiel #6
0
    def test_domain_cookie(self, mock_request):
        """
        Test domain cookies without 'Path'
        """
        response0 = Response()
        response0.status_code = 200
        response0._content = 'Mocked response content'
        response0.headers = {
            'Set-Cookie': 'a=apple; Domain=fruits.com;, ' +
                          'b=banana; Domain=fruits.com;, ' +
                          'c=citrus; Domain=mediterranean.fruits.com;, ' +
                          'm=mango; Domain=tropical.fruits.com;'
        }
        response0.url = 'http://mediterranean.fruits.com/path0'
        mock_request.return_value = response0

        get('http://mediterranean.fruits.com/path0')    # Initial request. No cookies.
        mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path0', allow_redirects=True)

        get('http://mediterranean.fruits.com/path1')    # 'a', 'b', and 'c' cookies should be present.
        mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path1', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana', 'c': 'citrus'})

        get('http://tropical.fruits.com/path2')         # 'a', 'b', and 'm' cookies should be present.
        mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path2', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})

        get('http://www.fruits.com/path3')              # 'a' and 'b' cookies should be present.
        mock_request.assert_called_with('GET', 'http://www.fruits.com/path3', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana'})

        get('http://fruits.com/path4')                  # 'a' and 'b' cookies should be present.
        mock_request.assert_called_with('GET', 'http://fruits.com/path4', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana'})

        get('http://animals.com/path5')                 # Different domain. No cookies should be present.
        mock_request.assert_called_with('GET', 'http://animals.com/path5', allow_redirects=True)

        response1 = Response()
        response1.status_code = 200
        response1._content = 'Mocked response content'
        response1.headers = {
            'Set-Cookie': 'a=apricot; Domain=fruits.com;, ' +
                          'b=; Domain=fruits.com;, ' +
                          'm=melon; Domain=tropical.fruits.com;'
        }
        response1.url = 'http://tropical.fruits.com/path0'
        mock_request.return_value = response1

        get('http://tropical.fruits.com/path0')         # Still called with previous cookies
        mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path0', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})

        get('http://tropical.fruits.com/path1')         # called with new cookies
        mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path1', allow_redirects=True,
            cookies={'a': 'apricot', 'b': '', 'm': 'melon'})
Beispiel #7
0
 def setUp(self):
     self.cassette = cassette.Cassette(
         TestCassette.cassette_name,
         'json',
         'w+'
     )
     r = Response()
     r.status_code = 200
     r.encoding = 'utf-8'
     r.headers = CaseInsensitiveDict({'Content-Type': decode('foo')})
     r.url = 'http://example.com'
     cassette.add_urllib3_response({
         'body': {
             'string': decode('foo'),
             'encoding': 'utf-8'
         }
     }, r)
     self.response = r
     r = Request()
     r.method = 'GET'
     r.url = 'http://example.com'
     r.headers = {}
     r.data = {'key': 'value'}
     self.response.request = r.prepare()
     self.response.request.headers.update(
         {'User-Agent': 'betamax/test header'}
     )
     self.json = {
         'request': {
             'body': 'key=value',
             'headers': {
                 'User-Agent': 'betamax/test header',
                 'Content-Length': '9',
                 'Content-Type': 'application/x-www-form-urlencoded',
             },
             'method': 'GET',
             'uri': 'http://example.com/',
         },
         'response': {
             'body': {
                 'string': decode('foo'),
                 'encoding': 'utf-8',
             },
             'headers': {'Content-Type': decode('foo')},
             'status_code': 200,
             'url': 'http://example.com',
         },
         'recorded_at': '2013-08-31T00:00:00',
     }
     self.date = datetime(2013, 8, 31)
     self.cassette.save_interaction(self.response, self.response.request)
     self.interaction = self.cassette.interactions[0]
     self.interaction.recorded_at = self.date
Beispiel #8
0
    def setUp(self):
        # Make a new serializer to test with
        self.test_serializer = Serializer()
        serializers.serializer_registry["test"] = self.test_serializer

        # Instantiate the cassette to test with
        self.cassette = cassette.Cassette(TestCassette.cassette_name, "test", record_mode="once")

        # Create a new object to serialize
        r = Response()
        r.status_code = 200
        r.reason = "OK"
        r.encoding = "utf-8"
        r.headers = CaseInsensitiveDict({"Content-Type": decode("foo")})
        r.url = "http://example.com"
        util.add_urllib3_response({"body": {"string": decode("foo"), "encoding": "utf-8"}}, r)
        self.response = r

        # Create an associated request
        r = Request()
        r.method = "GET"
        r.url = "http://example.com"
        r.headers = {}
        r.data = {"key": "value"}
        self.response.request = r.prepare()
        self.response.request.headers.update({"User-Agent": "betamax/test header"})

        # Expected serialized cassette data.
        self.json = {
            "request": {
                "body": {"encoding": "utf-8", "string": "key=value"},
                "headers": {
                    "User-Agent": ["betamax/test header"],
                    "Content-Length": ["9"],
                    "Content-Type": ["application/x-www-form-urlencoded"],
                },
                "method": "GET",
                "uri": "http://example.com/",
            },
            "response": {
                "body": {"string": decode("foo"), "encoding": "utf-8"},
                "headers": {"Content-Type": [decode("foo")]},
                "status": {"code": 200, "message": "OK"},
                "url": "http://example.com",
            },
            "recorded_at": "2013-08-31T00:00:00",
        }
        self.date = datetime(2013, 8, 31)
        self.cassette.save_interaction(self.response, self.response.request)
        self.interaction = self.cassette.interactions[0]
        self.interaction.recorded_at = self.date
    def test_redirect(self, mock_request):
        """
        Test that each session has its own redirect "sandbox".
        """
        response0 = Response()
        response0.url = 'http://www.test.com/neverseemeagain'
        response0.status_code = 301
        response0.headers = {'Location': 'http://www.test.com/redirect_1'}

        response1 = Response()
        response1.url = 'http://www.test.com/redirect_1'
        response1.status_code = 301
        response1.headers = {'Location': 'http://www.test.com/redirect_2'}

        response2 = Response()
        response2.url = 'http://www.test.com/redirect_2'
        response2.status_code = 301
        response2.headers = {'Location': 'http://www.test.com/redirect_3'}

        response3 = Response()
        response3.url = 'http://www.test.com/redirect_3'
        response3.status_code = 200
        response3._content = 'Mocked response content'
        response3.history = [response0, response1, response2]

        mock_request.return_value = response3

        s0 = Session()
        s1 = Session()

        # s0 make a request
        r = s0.get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        self.assertEqual(r.status_code, 200)

        # s0 make a request again. Assert we not make request to 301 again.
        r = s0.get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
        self.assertEqual(r.status_code, 200)

        # s1 make a request
        r = s1.get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        self.assertEqual(r.status_code, 200)

        # s1 make a request again. Assert we not make request to 301 again.
        r = s1.get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
        self.assertEqual(r.status_code, 200)
Beispiel #10
0
    def requests_call(self,
                      method,
                      url,
                      headers={},
                      params=None,
                      data=None,
                      stream=False):
        if (self.MyServerURL == ''):
            print('no target URL!')
            return
        print('request call: ' + method + ' ' + url)
        headers.update(self.additional_headers)
        mydata = {}
        mydata['method'] = method
        mydata['url'] = url
        mydata['headers'] = headers
        mydata['data'] = data
        mydata['stream'] = stream
        mydata['params'] = params

        t = requests.post(self.MyServerURL, None, json.dumps(mydata))
        t = json.loads(t.content)
        final = Response()
        final.status_code = t['statusCode']
        final._content = bytes(t['body'], 'utf-8')
        final.headers = t['headers']
        final.encoding = 'utf-8'
        tt = mydata
        return (final)
Beispiel #11
0
def test_arn_partition_rewriting_in_response_without_region_and_with_default_region(
        encoding, switch_region):
    with switch_region("us-gov-east-1"):
        listener = ArnPartitionRewriteListener()
        response = Response()
        response._content = encoding(
            json.dumps(
                {"some-data-with-arn": "arn:aws:iam::123456789012:ArnInData"}))
        response._status_code = 200
        response.headers = {
            "some-header-with-arn": "arn:aws:iam::123456789012:ArnInHeader"
        }

        result = listener.return_response(method="POST",
                                          path="/",
                                          data="ignored",
                                          headers={},
                                          response=response)

        assert result.status_code == response.status_code
        assert result.headers == {
            "some-header-with-arn":
            "arn:aws-us-gov:iam::123456789012:ArnInHeader"
        }
        assert result.content == encoding(
            json.dumps({
                "some-data-with-arn":
                "arn:aws-us-gov:iam::123456789012:ArnInData"
            }))
Beispiel #12
0
def setup_cache():
    response = Response()
    response.headers = {"ETag": "etag"}
    response.status_code = 200
    _cache["test"] = response
    yield
    del _cache._cache["test"]
Beispiel #13
0
    def create_mock_response(cls, status_code, data, filter=None, order_by=None, page=None, error=None, headers=None):
        """ Build a fake response

            Args:
                status_code: the status code
                data: the NURESTObject
                filter: a string representing a filter
                order_by: a string representing an order by
                page: a page number

        """

        content = None
        if type(data) == list:
            content = list()
            for obj in data:
                content.append(obj.to_dict())
        elif data:
            content = data.to_dict()

        response = Response()
        response.status_code = status_code
        response._content = json.dumps(content)

        if headers:
            response.headers = headers

        return MagicMock(return_value=response)
Beispiel #14
0
    def send(self,
             request: PreparedRequest,
             stream=False,
             timeout=None,
             verify=True,
             cert=None,
             proxies=None):
        request.url = request.url.decode("utf-8") if isinstance(
            request.url, bytes) else request.url
        # Check URL, ask the fake Icinga to handle it
        self.check_path(request.url)
        resp = self.icinga.handle(request)

        # Create response, emulate equests.adapters.HTTPAdapter behavior a bit
        response = Response()
        response.status_code = resp.get("status_code", None)
        response.headers = CaseInsensitiveDict(resp.get("headers", {}))
        response.encoding = get_encoding_from_headers(response.headers)
        response.reason = resp.get("reason", None)
        response.url = request.url  # Already decoded above
        response.request = request
        response.raw = StreamableBytesIO(resp.get("body", "").encode("utf-8"))
        # Cookie jar is not mocked, as Icinga doesn't use cookies
        # response.connection is not mocked, because it's not a response attribute by default

        # Call settings hook with the settings used for this request (to make testing with these easier)
        self.settings_hook(stream=stream,
                           timeout=timeout,
                           verify=verify,
                           cert=cert,
                           proxies=proxies)

        return response
    def test_set_default_cookie_cache(self, mock_request):
        response = Response()
        response.headers = {
            'Set-Cookie': 'name=value',
        }
        response.url = 'http://www.test.com/path'
        mock_request.return_value = response

        C0 = self.cookie_cache
        C1 = Cache()
        C2 = Cache()

        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)

        set_default_cookie_cache(C1)

        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)

        set_default_cookie_cache(C2)

        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)

        set_default_cookie_cache(C0)

        get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)
    def test_disable_default_cache(self, mock_request):
        """
        Test disable default cache (by setting default cache to None)
        """
        response = Response()
        response.status_code = 200
        response._content = 'Mocked response content'
        response.headers = {
            'Cache-Control': 'max-age=100',
            }
        mock_request.return_value = response

        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)
        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)

        set_default_cache(None)

        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 4)
Beispiel #17
0
    def build_response(self, request, resp):
        """
        Builds a Requests' response object.  This emulates most of the logic of
        the standard fuction but deals with the lack of the ``.headers``
        property on the HTTP20Response object.
        """
        response = Response()

        response.status_code = resp.status
        response.headers = CaseInsensitiveDict(resp.getheaders())
        response.raw = resp
        response.reason = resp.reason
        response.encoding = get_encoding_from_headers(response.headers)

        extract_cookies_to_jar(response.cookies, request, response)
        response.url = request.url

        response.request = request
        response.connection = self

        # One last horrible patch: Requests expects its raw responses to have a
        # release_conn method, which I don't. We should monkeypatch a no-op on.
        resp.release_conn = lambda: None

        return response
Beispiel #18
0
def test_no_arn_partition_rewriting_in_internal_response():
    """Partitions should not be rewritten for _responses_ of _internal_ requests."""
    listener = ArnPartitionRewriteListener()
    response = Response()
    body_content = json.dumps({
        "some-data-with-arn":
        "arn:aws:apigateway:us-gov-west-1::/restapis/arn-in-body/*"
    })
    response._content = body_content
    response._status_code = 200
    response_header_content = {
        "some-header-with-arn":
        "arn:aws:apigateway:us-gov-west-1::/restapis/arn-in-header/*"
    }
    response.headers = response_header_content

    # mimic an internal request
    request_headers = mock_aws_request_headers(
        region_name="us-gov-west-1", access_key=INTERNAL_AWS_ACCESS_KEY_ID)

    result = listener.return_response(method="POST",
                                      path="/",
                                      data="ignored",
                                      headers=request_headers,
                                      response=response)

    assert result is None
Beispiel #19
0
    def send(self, request, stream=None, timeout=None, verify=None, cert=None,
             proxies=None):
        parsed_url = urlparse.urlparse(request.url)

        # We only work for requests with a host of localhost
        if parsed_url.netloc.lower() != "localhost":
            raise InvalidURL("Invalid URL %r: Only localhost is allowed" %
                request.url)

        real_url = urlparse.urlunparse(parsed_url[:1] + ("",) + parsed_url[2:])
        pathname = url_to_path(real_url)

        resp = Response()
        resp.status_code = 200
        resp.url = real_url

        stats = os.stat(pathname)
        modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
        resp.headers = CaseInsensitiveDict({
            "Content-Type": mimetypes.guess_type(pathname)[0] or "text/plain",
            "Content-Length": stats.st_size,
            "Last-Modified": modified,
        })

        resp.raw = LocalFSResponse(open(pathname, "rb"))
        resp.close = resp.raw.close

        return resp
Beispiel #20
0
def requests_response(content, status_code=200, headers={}):
    resp = RequestsResponse()
    content = json.dumps(content) if isinstance(content, dict) else content
    resp._content = content
    resp.status_code = status_code
    resp.headers = headers
    return resp
Beispiel #21
0
    def send(self, request, stream=None, timeout=None, verify=None, cert=None, proxies=None):
        pathname = url_to_path(request.url)

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            stats = lstat(pathname)
        except (IOError, OSError) as exc:
            resp.status_code = 404
            message = {
                "error": "file does not exist",
                "path": pathname,
                "exception": repr(exc),
            }
            fh = SpooledTemporaryFile()
            fh.write(ensure_binary(json.dumps(message)))
            fh.seek(0)
            resp.raw = fh
            resp.close = resp.raw.close
        else:
            modified = formatdate(stats.st_mtime, usegmt=True)
            content_type = guess_type(pathname)[0] or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": stats.st_size,
                "Last-Modified": modified,
            })

            resp.raw = open(pathname, "rb")
            resp.close = resp.raw.close
        return resp
Beispiel #22
0
    def build_response(self, req, resp):
        """Builds a :class:`Response <requests.Response>` object from a urllib3
        response. This should not be called from user code, and is only exposed
        for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`

        :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
        :param resp: The urllib3 response object.
        """
        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(resp, 'status', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = resp
        response.reason = response.raw.reason

        if isinstance(req.url, bytes):
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Add new cookies from the server.
        extract_cookies_to_jar(response.cookies, req, resp)

        # Give the Response some context.
        response.request = req
        response.connection = self

        return response
Beispiel #23
0
    def send(self, request, stream=None, timeout=None, verify=None, cert=None,
             proxies=None):
        parsed_url = urlparse.urlparse(request.url)

        # We only work for requests with a host of localhost
        if parsed_url.netloc.lower() != "localhost":
            raise InvalidURL("Invalid URL %r: Only localhost is allowed" %
                request.url)

        real_url = urlparse.urlunparse(parsed_url[:1] + ("",) + parsed_url[2:])
        pathname = url_to_path(real_url)

        resp = Response()
        resp.status_code = 200
        resp.url = real_url

        stats = os.stat(pathname)
        modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
        resp.headers = CaseInsensitiveDict({
            "Content-Type": mimetypes.guess_type(pathname)[0] or "text/plain",
            "Content-Length": stats.st_size,
            "Last-Modified": modified,
        })

        resp.raw = LocalFSResponse(open(pathname, "rb"))
        resp.close = resp.raw.close

        return resp
    def test_cookie(self, mock_request):
        """
        Test that each session has its own cookie "sandbox".
        """
        response = Response()
        response.status_code = 200
        response._content = 'Mocked response content'
        response.headers = {'Set-Cookie': 'name=value'}
        response.url = 'http://www.test.com/path'

        mock_request.return_value = response

        s0 = Session()
        s1 = Session()

        # s0 make requests
        s0.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
        s0.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})

        # s1 make requests
        s1.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
        s1.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})

        # s0 make requests again
        s0.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})
        s0.get('http://www.test.com/path')
        mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})
    def http_response_to_response(self, http_response, prepared_request):
        """
        transform a WSGIResponse into a requests's Response model
        :param django.http.response.HttpResponse http_response: the http response send by django view
        :return: the requests's Response model corresponding to the http_response
        :rtype: Response
        """
        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(http_response, 'status_code', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(http_response._headers, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = http_response
        response.reason = response.raw.reason_phrase
        response._content = http_response.content
        req = prepared_request

        if isinstance(req.url, bytes):  # pragma: no cover
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Add new cookies from the server.
        extract_cookies_to_jar(response.cookies, req, response)

        # Give the Response some context.
        response.request = req
        response.connection = self

        return response
 def request(method, url, **kwargs):
     if 'data' in kwargs:
         kwargs['params'] = kwargs.pop('data')
     elif 'params' in kwargs and kwargs['params'] is None:
         kwargs.pop('params')
     auth = None
     if 'auth' in kwargs:
         auth = kwargs.pop('auth')
     for i in ['auth', 'allow_redirects', 'stream']:
         if i in kwargs:
             kwargs.pop(i)
     if app.app.registry.api_url in url:
         if auth:
             authorization = api.authorization
             api.authorization = ('Basic', auth)
         resp = api._gen_request(method.upper(), url, expect_errors=True, **kwargs)
         if auth:
             api.authorization = authorization
     else:
         resp = app._gen_request(method.upper(), url, expect_errors=True, **kwargs)
     response = Response()
     response.status_code = resp.status_int
     response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
     response.encoding = get_encoding_from_headers(response.headers)
     response.raw = resp
     response._content = resp.body
     response.reason = resp.status
     if isinstance(url, bytes):
         response.url = url.decode('utf-8')
     else:
         response.url = url
     response.request = resp.request
     return response
Beispiel #27
0
async def wrap_async(response: ClientResponse) -> Response:
    """Build a ``requests`` response from a ``aiohttp`` response.

    A ``requests.Response`` instance is built to provide synchronous
    access to the original response's data. Note that the returned
    response does not have proper data for :attr:``elapsed`` or
    :attr:``request``.

    The response will be consumed if it has not already.
    """

    # Ensure the response data is read so that the wrapped response
    # does not require any async methods.
    await response.read()

    wrapped = Response()
    wrapped._content = response._body  # type: ignore
    wrapped._content_consumed = True  # type: ignore
    wrapped.status_code = response.status
    wrapped.headers = CaseInsensitiveDict(response.headers)
    wrapped.url = str(response.url)  # `aiohttp` uses a `URL` object.
    wrapped.encoding = response.get_encoding()
    wrapped.history = [await wrap_async(rsp) for rsp in response.history]
    wrapped.reason = response.reason or ""
    wrapped.cookies = cookiejar_from_dict(response.cookies)
    return wrapped
def test_arn_partition_rewriting_in_response(encoding):
    listener = ArnPartitionRewriteListener()
    response = Response()
    response._content = encoding(
        json.dumps(
            {"some-data-with-arn": "arn:aws:apigateway:us-gov-west-1::/restapis/arn-in-body/*"}
        )
    )
    response._status_code = 200
    response.headers = {
        "some-header-with-arn": "arn:aws:apigateway:us-gov-west-1::/restapis/arn-in-header/*"
    }

    result = listener.return_response(
        method="POST", path="/", data="ignored", headers={}, response=response
    )

    assert result.status_code == response.status_code
    assert result.headers == {
        "some-header-with-arn": "arn:aws-us-gov:apigateway:us-gov-west-1::/restapis/arn-in-header/*"
    }
    assert result.content == encoding(
        json.dumps(
            {
                "some-data-with-arn": "arn:aws-us-gov:apigateway:us-gov-west-1::/restapis/arn-in-body/*"
            }
        )
    )
Beispiel #29
0
    def create_mock_response(cls,
                             status_code,
                             data,
                             filter=None,
                             order_by=None,
                             page=None,
                             error=None,
                             headers=None):
        """ Build a fake response

            Args:
                status_code: the status code
                data: the NURESTObject
                filter: a string representing a filter
                order_by: a string representing an order by
                page: a page number

        """

        content = None
        if type(data) == list:
            content = list()
            for obj in data:
                content.append(obj.to_dict())
        elif data:
            content = data.to_dict()

        response = Response()
        response.status_code = status_code
        response._content = json.dumps(content).encode('utf-8')

        if headers:
            response.headers = headers

        return MagicMock(return_value=response)
    def return_response(self, method, path, data, headers, response):

        if method == 'POST' and path == '/':
            req_data = urlparse.parse_qs(data)
            action = req_data.get('Action', [None])[0]
            event_type = None
            if action == 'CreateQueue':
                event_type = event_publisher.EVENT_SQS_CREATE_QUEUE
                response_data = xmltodict.parse(response.content)
                queue_url = response_data['CreateQueueResponse'][
                    'CreateQueueResult']['QueueUrl']
            elif action == 'DeleteQueue':
                event_type = event_publisher.EVENT_SQS_DELETE_QUEUE
                queue_url = req_data.get('QueueUrl', [None])[0]

            if event_type:
                event_publisher.fire_event(
                    event_type,
                    payload={'u': event_publisher.get_hash(queue_url)})

            # patch the response and return https://... if we're supposed to use SSL
            if config.USE_SSL and action in ('CreateQueue', 'GetQueueUrl'):
                content_str = to_str(response.content)
                if '<QueueUrl>http://' in content_str:
                    new_response = Response()
                    new_response.status_code = response.status_code
                    new_response.headers = response.headers
                    new_response._content = re.sub(r'<QueueUrl>\s*http://',
                                                   '<QueueUrl>https://',
                                                   content_str)
                    new_response.headers['content-length'] = len(
                        new_response._content)
                    return new_response
Beispiel #31
0
    def build_response(self, request, resp):
        """
        Builds a Requests' response object.  This emulates most of the logic of
        the standard fuction but deals with the lack of the ``.headers``
        property on the HTTP20Response object.
        """
        response = Response()

        response.status_code = resp.status
        response.headers = CaseInsensitiveDict(resp.getheaders())
        response.raw = resp
        response.reason = resp.reason
        response.encoding = get_encoding_from_headers(response.headers)

        extract_cookies_to_jar(response.cookies, request, response)

        if isinstance(request.url, bytes):
            response.url = request.url.decode('utf-8')
        else:
            response.url = request.url

        response.request = request
        response.connection = self

        # One last horrible patch: Requests expects its raw responses to have a
        # release_conn method, which I don't. We should monkeypatch a no-op on.
        resp.release_conn = lambda: None

        return response
Beispiel #32
0
    def send(self, request, **kwargs):
        url = urlparse(request.url)
        if url.scheme != 'https':
            raise Exception('Only HTTPS is supported!')

        ctx = self._make_context()

        conn = httpslib.HTTPSConnection(
                url.hostname, url.port or 443, ssl_context=ctx)
        conn.request(request.method, url.path, request.body, request.headers)

        resp = conn.getresponse()
        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(resp, 'status', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = resp
        response.reason = response.raw.reason

        if isinstance(request.url, bytes):
            response.url = request.url.decode('utf-8')
        else:
            response.url = request.url

        # Give the Response some context.
        response.request = request
        response.connection = self

        return response
Beispiel #33
0
    def send(self, request, stream=None, timeout=None, verify=None, cert=None, proxies=None):
        pathname = url_to_path(request.url)

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            stats = lstat(pathname)
        except (IOError, OSError) as exc:
            resp.status_code = 404
            message = {
                "error": "file does not exist",
                "path": pathname,
                "exception": repr(exc),
            }
            fh = SpooledTemporaryFile()
            fh.write(ensure_binary(json.dumps(message)))
            fh.seek(0)
            resp.raw = fh
            resp.close = resp.raw.close
        else:
            modified = formatdate(stats.st_mtime, usegmt=True)
            content_type = guess_type(pathname)[0] or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": stats.st_size,
                "Last-Modified": modified,
            })

            resp.raw = open(pathname, "rb")
            resp.close = resp.raw.close
        return resp
Beispiel #34
0
    def test_expired_cookie(self, mock_request):
        response = Response()
        response.status_code = 200
        response._content = 'Mocked response content'
        response.headers = {
            'Set-Cookie': 'a=apple; expires=%s;, b=banana; max-age=6' % _getdate(future=3)
        }
        response.url = 'http://www.fruits.com'
        mock_request.return_value = response

        get('http://www.fruits.com/path')

        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=1)
        get('http://www.fruits.com/path')
        mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True,
            cookies={'a': 'apple', 'b': 'banana'})

        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=4)
        get('http://www.fruits.com/path')
        mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True,
            cookies={'b': 'banana'})

        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=11)
        get('http://www.fruits.com/path')
        mock_request.assert_called_with('GET', 'http://www.fruits.com/path', allow_redirects=True)
Beispiel #35
0
 def from_serializable(self, data: Any) -> Response:
     response = Response()
     for key in self.__response_keys:
         setattr(response, key, data[key])
     for key in self.__response_keys_special:
         if key == "raw":
             response.raw = BytesIO(data[key])
         if key == "headers":
             response.headers = CaseInsensitiveDict(data[key])
         if key == "elapsed":
             response.elapsed = datetime.timedelta(seconds=data[key])
         if key == "_content":
             encoding = response.encoding or self.__implicit_encoding
             indicator = data[self.__store_indicator]
             if indicator == 0:
                 what_store = data[key]
             elif indicator == 1:
                 what_store = data[key].encode(encoding)
             elif indicator == 2:
                 what_store = json.dumps(data[key])
                 what_store = what_store.encode(encoding)
             response._content = what_store  # type: ignore
         if key == "_next":
             setattr(response, "_next", data[key])
     return response
Beispiel #36
0
    def _receive_response(self, task, response):
        """
        Called by the delegate when a response has been received.

        This call is expected only on background threads, and thus may not do
        anything that is not Python-thread-safe. This means that, for example,
        it is safe to grab things from the _tasks dictionary, but it is not
        safe to make other method calls on this object unless they explicitly
        state that they are safe in background threads.
        """
        queue, request = self._tasks[task]

        resp = Response()
        resp.status_code = getKey(response, 'statusCode')
        resp.reason = ''

        # TODO: Why do I have to do this?
        raw_headers = getKey(response, 'allHeaderFields')
        resp.headers = CaseInsensitiveDict(raw_headers)
        resp.encoding = get_encoding_from_headers(resp.headers)

        # TODO: This needs to point to an object that we can use to provide
        # the various raw things that requests needs.
        resp.raw = None

        if isinstance(request.url, bytes):
            resp.url = request.url.decode('utf-8')
        else:
            resp.url = request.url

        resp.request = request
        resp.connection = self

        # Put this response on the queue.
        queue.put_nowait(resp)
Beispiel #37
0
    def return_response(self, method, path, data, headers, response,
                        request_handler):
        if method == 'OPTIONS' and path == '/':
            # Allow CORS preflight requests to succeed.
            return 200

        if method != 'POST':
            return

        region_name = aws_stack.get_region()
        req_data = urlparse.parse_qs(to_str(data))
        action = req_data.get('Action', [None])[0]
        content_str = content_str_original = to_str(response.content)

        if response.status_code >= 400:
            return response

        _fire_event(req_data, response)

        # patch the response and add missing attributes
        if action == 'GetQueueAttributes':
            content_str = _add_queue_attributes(path, req_data, content_str,
                                                headers)

        # instruct listeners to fetch new SQS message
        if action == 'SendMessage':
            self._process_sent_message(path, data, req_data, headers)

        # patch the response and return the correct endpoint URLs / ARNs
        if action in ('CreateQueue', 'GetQueueUrl', 'ListQueues',
                      'GetQueueAttributes'):
            if config.USE_SSL and '<QueueUrl>http://' in content_str:
                # return https://... if we're supposed to use SSL
                content_str = re.sub(r'<QueueUrl>\s*http://',
                                     r'<QueueUrl>https://', content_str)
            # expose external hostname:port
            external_port = SQS_PORT_EXTERNAL or get_external_port(
                headers, request_handler)
            content_str = re.sub(
                r'<QueueUrl>\s*([a-z]+)://[^<]*:([0-9]+)/([^<]*)\s*</QueueUrl>',
                r'<QueueUrl>\1://%s:%s/\3</QueueUrl>' %
                (HOSTNAME_EXTERNAL, external_port), content_str)
            # fix queue ARN
            content_str = re.sub(
                r'<([a-zA-Z0-9]+)>\s*arn:aws:sqs:elasticmq:([^<]+)</([a-zA-Z0-9]+)>',
                r'<\1>arn:aws:sqs:%s:\2</\3>' % (region_name), content_str)

            if action == 'CreateQueue':
                queue_url = re.match(r'.*<QueueUrl>(.*)</QueueUrl>',
                                     content_str, re.DOTALL).group(1)
                _set_queue_attributes(queue_url, req_data)

        if content_str_original != content_str:
            # if changes have been made, return patched response
            new_response = Response()
            new_response.status_code = response.status_code
            new_response.headers = response.headers
            new_response._content = content_str
            new_response.headers['content-length'] = len(new_response._content)
            return new_response
Beispiel #38
0
    def build_response(self, req, resp):
        """Builds a :class:`Response <requests.Response>` object from a urllib3
        response. This should not be called from user code, and is only exposed
        for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`

        :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
        :param resp: The urllib3 response object.
        """
        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(resp, 'status', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = resp
        response.reason = response.raw.reason

        if isinstance(req.url, bytes):
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Add new cookies from the server.
        extract_cookies_to_jar(response.cookies, req, resp)

        # Give the Response some context.
        response.request = req
        response.connection = self

        return response
Beispiel #39
0
    def return_response(self, method, path, data, headers, response,
                        request_handler):

        if method == 'POST' and path == '/':
            req_data = urlparse.parse_qs(to_str(data))
            action = req_data.get('Action', [None])[0]
            event_type = None
            queue_url = None
            if action == 'CreateQueue':
                event_type = event_publisher.EVENT_SQS_CREATE_QUEUE
                response_data = xmltodict.parse(response.content)
                if 'CreateQueueResponse' in response_data:
                    queue_url = response_data['CreateQueueResponse'][
                        'CreateQueueResult']['QueueUrl']
            elif action == 'DeleteQueue':
                event_type = event_publisher.EVENT_SQS_DELETE_QUEUE
                queue_url = req_data.get('QueueUrl', [None])[0]

            if event_type and queue_url:
                event_publisher.fire_event(
                    event_type,
                    payload={'u': event_publisher.get_hash(queue_url)})

            # patch the response and return the correct endpoint URLs
            if action in ('CreateQueue', 'GetQueueUrl', 'ListQueues'):
                content_str = content_str_original = to_str(response.content)
                new_response = Response()
                new_response.status_code = response.status_code
                new_response.headers = response.headers
                if config.USE_SSL and '<QueueUrl>http://' in content_str:
                    # return https://... if we're supposed to use SSL
                    content_str = re.sub(r'<QueueUrl>\s*http://',
                                         r'<QueueUrl>https://', content_str)
                # expose external hostname:port
                external_port = get_external_port(headers, request_handler)
                content_str = re.sub(
                    r'<QueueUrl>\s*([a-z]+)://[^<]*:([0-9]+)/([^<]*)\s*</QueueUrl>',
                    r'<QueueUrl>\1://%s:%s/\3</QueueUrl>' %
                    (HOSTNAME_EXTERNAL, external_port), content_str)
                new_response._content = content_str
                if content_str_original != new_response._content:
                    # if changes have been made, return patched response
                    new_response.headers['content-length'] = len(
                        new_response._content)
                    return new_response

            # Since this API call is not implemented in ElasticMQ, we're mocking it
            # and letting it return an empty response
            if action == 'ListQueueTags':
                new_response = Response()
                new_response.status_code = 200
                new_response._content = (
                    '<?xml version="1.0"?>'
                    '<ListQueueTagsResponse xmlns="{}">'
                    '<ListQueueTagsResult/>'  # noqa: W291
                    '<ResponseMetadata>'  # noqa: W291
                    '<RequestId>{}</RequestId>'  # noqa: W291
                    '</ResponseMetadata>'  # noqa: W291
                    '</ListQueueTagsResponse>').format(XMLNS_SQS, uuid.uuid4())
                return new_response
Beispiel #40
0
 def test_getting_content_for_empty_2xx_json_response(self, m_get):
     response = Response()
     response.status_code = 200
     response.headers = {"Content-Type": "application/json"}
     response._content = str.encode("")
     m_get.return_value = response
     response = self.client.get("some_url")
     assert response == ""
    def test_set_default_redirect_cache(self, mock_request):
        response0 = Response()
        response0.url = 'http://www.test.com/neverseemeagain'
        response0.status_code = 301
        response0.headers = {
            'Location': 'http://www.test.com/redirect_here',
        }

        response1 = Response()
        response1.url = 'http://www.test.com/redirect_here'
        response1.status_code = 200
        response1._content = 'Mocked response content'
        response1.headers = {
            'Vary': 'Accept',
        }
        response1.history = [response0]

        mock_request.return_value = response1

        C0 = self.redirect_cache
        C1 = Cache()
        C2 = Cache()

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)

        set_default_redirect_cache(C1)

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)

        set_default_redirect_cache(C2)

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)

        set_default_redirect_cache(C0)

        get('http://www.test.com/neverseemeagain')
        mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
Beispiel #42
0
 def test_add_urllib3_response(self):
     r = Response()
     r.status_code = 200
     r.headers = {}
     util.add_urllib3_response({"body": {"string": decode("foo"), "encoding": "utf-8"}}, r)
     assert isinstance(r.raw, urllib3.response.HTTPResponse)
     assert r.content == b"foo"
     assert isinstance(r.raw._original_response, cassette.MockHTTPResponse)
Beispiel #43
0
def deserialize_response(serialized):
    r = Response()
    r.encoding = serialized['body']['encoding']
    r.headers = CaseInsensitiveDict(serialized['headers'])
    r.url = serialized.get('url', '')
    r.status_code = serialized['status_code']
    add_urllib3_response(serialized, r)
    return r
Beispiel #44
0
    def test_get_etag_missing_header(self):
        cache = _ResponseCache()
        response = Response()
        response.headers = {}
        cache[self.path] = response

        out = cache.get_etag(self.path)
        assert out is None
Beispiel #45
0
def send_request(client,
                 endpoint: str,
                 method: str,
                 request_data,
                 request_token: Optional[str] = None):
    """
    Send an api request and return the response.

    :param client: The requesting client.
    :param endpoint: The end-point to contact e.g. "/dashboard".
    :param method: The request method.
    :param request_data: The data payload.
    :param request_token: An optional auth token to use instead of the default test token.
    :return: The status response.
    """
    try:
        request_data_string = json.dumps(request_data)
    except ValueError:
        request_data_string = ""

    if request_token is None:
        header = {
            "X-Auth-Token": testing_api_key
        }  # The token used for testing by default.
    else:
        header = {"X-Auth-Token": request_token}

    if method == "post":
        response = client.open(endpoint,
                               method="POST",
                               data=request_data_string,
                               content_type="application/json",
                               headers=header)
    elif method == "get":
        response = client.open(endpoint,
                               method="GET",
                               data=request_data_string,
                               content_type="application/json",
                               headers=header)
    elif method == "put":
        response = client.open(endpoint,
                               method="PUT",
                               data=request_data_string,
                               content_type="application/json",
                               headers=header)
    elif method == "delete":
        response = client.open(endpoint,
                               method="DELETE",
                               data=request_data_string,
                               content_type="application/json",
                               headers=header)
    else:
        response = Response()
        response.status_code = 405
        response.headers = {"Content-Type": "application/json"}

    return response
Beispiel #46
0
 def request(self, *args, **kwargs):
     res = Response()
     res.status_code = requests.codes.ok
     page = self.pages.pop()
     res.headers = {"link": '<https:///>; rel="next"'} if self.pages else {}
     res.headers['content-type'] = 'application/json'
     res.headers['X-OpenAPI-Paginated-Content-Key'] = 'results'
     res.raw = io.BytesIO(json.dumps(page).encode())
     return res
Beispiel #47
0
def fake_response(url, html, headers):

    r = Response()

    r._content = https_html.encode()
    r.headers = headers
    r.url = url

    return r
Beispiel #48
0
 def get(self, *args, **kwargs):
     self.count += 1
     resp = Response()
     resp._content = b'{"results": []}'
     resp.headers = {"Content-Type": "application/json"}
     resp.status_code = 200
     if (self.fail_first and self.count == 1) or self.fail_all:
         resp.status_code = 408
     return resp
Beispiel #49
0
 def create_response(data,
                     status_code=200,
                     content_type='application/json'):
     resp = Response()
     resp.headers = CaseInsensitiveDict({
         'Content-Type': content_type,
     })
     resp.status_code = status_code
     resp.raw = data
     return resp
 def send(self, request, **kwargs):
     response = Response()
     response.status_code = 200
     response.reason = 'OK'
     response.headers = {
         'Content-Type': 'text/html; charset=UTF-8',
     }
     response.encoding = get_encoding_from_headers(response.headers)
     response.raw = BytesIO(b'<!doctype html><html>Hello</html>')
     return response
Beispiel #51
0
def build_response(req, delegate, cookiestore):
    # type: (PreparedRequest, NSURLSessionAdapterDelegate, NSHTTPCookieStorage) -> Response
    """Build a requests `Response` object from the response data collected by the NSURLSessionDelegate, and the
    default cookie store."""

    response = Response()

    # Fallback to None if there's no status_code, for whatever reason.
    response.status_code = getattr(delegate, 'status', None)

    # Make headers case-insensitive.
    response.headers = CaseInsensitiveDict(getattr(delegate, 'headers', {}))

    # Set encoding.
    response.encoding = get_encoding_from_headers(response.headers)
    # response.raw = resp
    # response.reason = response.raw.reason

    if isinstance(req.url, bytes):
        response.url = req.url.decode('utf-8')
    else:
        response.url = req.url

    # Add new cookies from the server. For NSURLSession these have already been parsed.
    # jar = RequestsCookieJar()
    # for cookie in cookiestore.cookies():
    #     print cookie
    #     c = Cookie(
    #         version=cookie.version(),
    #         name=cookie.name(),
    #         value=cookie.value(),
    #         port=8444,
    #         # port=cookie.portList()[-1],
    #         port_specified=0,
    #         domain=cookie.domain(),
    #         domain_specified=cookie.domain(),
    #         domain_initial_dot=cookie.domain(),
    #         path=cookie.path(),
    #         path_specified=cookie.path(),
    #         secure=!cookie.HTTPOnly(),
    #         expires=cookie.expiresDate(),
    #         comment=cookie.comment(),
    #         comment_url=cookie.commentURL(),
    #     )
    #     jar.set_cookie(c)
    #
    # response.cookies = jar

    response.raw = io.BytesIO(buffer(delegate.output))

    # Give the Response some context.
    response.request = req
    # response.connection = self

    return response
    def test_cache(self, mock_request):
        """
        Test that each session has its own cache "sandbox".
        """
        response = Response()
        response.status_code = 200
        response._content = 'Mocked response content'
        response.headers = {'Cache-Control': 'max-age=10'}

        mock_request.return_value = response

        s0 = Session()
        s1 = Session()

        # T=0: s0 makes multiple requests. Only the 1st request should not come from cache.
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)

        # T=5: s1 makes multiple requests. Only the 1st request should not come from cache.
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=5)
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)

        # s0 makes multiple requests. All requests should come from cache.
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)

        # T=15: s0 makes multiple requests. Only the 1st request should not come from cache.
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=15)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        s0.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)

        # s1 makes multiple requests. Only the 1st request should not come from cache.
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 4)
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 4)
        s1.get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 4)
Beispiel #53
0
    def test_get_304(self, mock_request):
        response0 = Response()
        response0.status_code = 200
        response0._content = 'Mocked response content'
        response0.headers = {
            'Cache-Control': 'max-age=1',
            'ETag': '"fdcd6016cf6059cbbf418d66a51a6b0a"',
            }

        response1 = Response()
        response1.status_code = 304
        response1._content = ''
        response1.headers = {
            'Cache-Control': 'max-age=2',
            }
        mock_request.side_effect = [response0, response1, response1, response1]

        get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)

        # Move time forward 1 second
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=1)

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        self.assertEqual(r.status_code, 304)
        self.assertEqual(r.content, 'Mocked response content')
        self.assertEqual(r.headers['Cache-Control'], 'max-age=2')

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 2)
        self.assertEqual(r.status_code, 304)
        self.assertEqual(r.content, 'Mocked response content')

        # Move time forward 3 seconds (1 + 2)
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3)

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        self.assertEqual(r.status_code, 304)
        self.assertEqual(r.content, 'Mocked response content')
        self.assertEqual(r.headers['Cache-Control'], 'max-age=2')
Beispiel #54
0
 def test_decoding_content_for_2xx_json_response(self, m_get):
     response = Response()
     response.status_code = 200
     response.headers = {"Content-Type": "application/json"}
     response._content = str.encode(
         json.dumps(
             {"data": "this_is_some_content_string_that_needs_to_be_long"}))
     m_get.return_value = response
     response = self.client.get("some_url")
     assert response[
         "data"] == "this_is_some_content_string_that_needs_to_be_long"
 def test_get_response_headers_response(self):
     """Test we get expected headers with a Response argument."""
     proxy_response = Response()
     proxy_response.headers = {'lol': 'wat', 'etag': '1'}
     with webcompat.app.app_context():
         new_headers = get_response_headers(proxy_response)
         assert new_headers.get('content-type') == 'application/json'
         assert new_headers.get('etag') == '1'
         assert new_headers.get('lol') is None
         new_headers2 = get_response_headers(proxy_response,
                                             mime_type='text/html')
         assert new_headers2.get('content-type') == 'text/html'
    def return_response(self, method, path, data, headers, response):
        req_data = None
        if method == 'POST' and path == '/':
            req_data = urlparse.parse_qs(to_str(data))
            action = req_data.get('Action')[0]

        if req_data:
            if action == 'DescribeStackResources':
                if response.status_code < 300:
                    response_dict = xmltodict.parse(response.content)['DescribeStackResourcesResponse']
                    resources = response_dict['DescribeStackResourcesResult']['StackResources']
                    if not resources:
                        # Check if stack exists
                        stack_name = req_data.get('StackName')[0]
                        cloudformation_client = aws_stack.connect_to_service('cloudformation')
                        try:
                            cloudformation_client.describe_stacks(StackName=stack_name)
                        except Exception:
                            return error_response('Stack with id %s does not exist' % stack_name, code=404)
            if action == 'DescribeStackResource':
                if response.status_code >= 500:
                    # fix an error in moto where it fails with 500 if the stack does not exist
                    return error_response('Stack resource does not exist', code=404)
            if action == 'ListStackResources':
                response_dict = xmltodict.parse(response.content, force_list=('member'))['ListStackResourcesResponse']
                resources = response_dict['ListStackResourcesResult']['StackResourceSummaries']
                if resources:
                    sqs_client = aws_stack.connect_to_service('sqs')
                    content_str = content_str_original = to_str(response.content)
                    new_response = Response()
                    new_response.status_code = response.status_code
                    new_response.headers = response.headers
                    for resource in resources['member']:
                        if resource['ResourceType'] == 'AWS::SQS::Queue':
                            try:
                                queue_name = resource['PhysicalResourceId']
                                queue_url = sqs_client.get_queue_url(QueueName=queue_name)['QueueUrl']
                            except Exception:
                                stack_name = req_data.get('StackName')[0]
                                return error_response('Stack with id %s does not exist' % stack_name, code=404)
                            content_str = re.sub(resource['PhysicalResourceId'], queue_url, content_str)
                    new_response._content = content_str
                    if content_str_original != new_response._content:
                        # if changes have been made, return patched response
                        new_response.headers['content-length'] = len(new_response._content)
                        return new_response
            elif action in ('CreateStack', 'UpdateStack'):
                if response.status_code >= 400:
                    return response
                # run the actual deployment
                template = template_deployer.template_to_json(req_data.get('TemplateBody')[0])
                template_deployer.deploy_template(template, req_data.get('StackName')[0])
Beispiel #57
0
 def test_add_urllib3_response(self):
     r = Response()
     r.status_code = 200
     r.headers = {}
     cassette.add_urllib3_response({
         'body': {
             'string': decode('foo'),
             'encoding': 'utf-8'
         }
     }, r)
     assert isinstance(r.raw, urllib3.response.HTTPResponse)
     assert r.content == b'foo'
     assert isinstance(r.raw._original_response, cassette.MockHTTPResponse)
Beispiel #58
0
    def test_get_304_cache_not_exist(self, mock_request):
        response0 = Response()
        response0.status_code = 200
        response0._content = 'Mocked response content X'
        response0.headers = {
            'Cache-Control': 'max-age=10',
            'ETag': '"fdcd6016cf6059cbbf418d66a51a6b0a"',
            }

        response1 = Response()
        response1.status_code = 304
        response1._content = ''
        response1.headers = {
            'Cache-Control': 'max-age=10',
            }

        response2 = Response()
        response2.status_code = 200
        response2._content = 'Mocked response content Y'
        response2.headers = {
            'Cache-Control': 'max-age=10',
            'ETag': '"a0b6a15a66d814fbbc9506fc6106dcdf"',
            }

        mock_request.side_effect = [response0, response1, response2]

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 1)
        self.assertEqual(r.content, 'Mocked response content X')

        self.cache.clear()

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        self.assertEqual(r.content, 'Mocked response content Y')

        r = get('http://www.test.com/path')
        self.assertEqual(mock_request.call_count, 3)
        self.assertEqual(r.content, 'Mocked response content Y')
 def side_effect(url, *args, **kwargs):
     if '/1' in url:
         response = Response()
         response.status_code = 200
         response._content = 'Mocked response content X'
         response.headers = {
             'Cache-Control': 'max-age=1',
             'ETag': '"fdcd6016cf6059cbbf418d66a51a6b0a"',
         }
     elif '/2' in url:
         response = Response()
         response.status_code = 204
         response._content = 'Mocked response content Y'
         response.headers = {
             'Cache-Control': 'max-age=1',
             'ETag': '"ffffffffffffffffffffffffffffffff"',
         }
     else:
         response = Response()
         response.status_code = 403
         response._content = 'Mocked response content Z'
     return response
    def test_origin_cookies_with_max_age(self):
        """
        Test setting and getting origin cookies with 'Max-Age' and/or 'Expires' attribute
        """

        # Cookie cache keys for convenience
        lookup_key = self.cookie_manager.get_origin_cookie_lookup_key("sweet.test.com")
        chipsahoy_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "chipsahoy")
        coke_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "coke")
        squeeze_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "squeeze")
        kitkat_key = self.cookie_manager.get_origin_cookie_key("sweet.test.com", "", "kitkat")

        # Prepare test responses
        response0 = Response()
        response0.headers = {
            "Set-Cookie": "chipsahoy=cookie; Max-Age=3;, "
            + "coke=soda; Max-Age=6;, "
            + "squeeze=juice; Expires=%s;, " % _getdate(future=3)
            + "kitkat=chocolate;"
        }
        response0.url = "http://sweet.test.com/path"

        ##### Process response cookie ####
        self.cookie_manager.process_response(None, response0)  # Note that 'request' is not used (thus None param)

        ##### Process request #####
        request = Request("http://sweet.test.com/help")
        self.cookie_manager.process_request(request)
        self.assertEqual(
            request.cookies, {"chipsahoy": "cookie", "coke": "soda", "squeeze": "juice", "kitkat": "chocolate"}
        )
        cookie_keys_set = self.cookie_cache.get(lookup_key)
        self.assertEqual(cookie_keys_set, set([chipsahoy_key, coke_key, squeeze_key, kitkat_key]))

        # 3 seconds pass by
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=3)

        request = Request("http://sweet.test.com/help")
        self.cookie_manager.process_request(request)
        self.assertEqual(request.cookies, {"coke": "soda", "kitkat": "chocolate"})
        cookie_keys_set = self.cookie_cache.get(lookup_key)
        self.assertEqual(cookie_keys_set, set([coke_key, kitkat_key]))

        # 3 more seconds pass by
        dummycache_cache.datetime.now = lambda: datetime.now() + timedelta(seconds=6)

        request = Request("http://sweet.test.com/help")
        self.cookie_manager.process_request(request)
        self.assertEqual(request.cookies, {"kitkat": "chocolate"})
        cookie_keys_set = self.cookie_cache.get(lookup_key)
        self.assertEqual(cookie_keys_set, set([kitkat_key]))