def test_post_file(self):
        view = ProxyView()

        factory = APIRequestFactory()
        request = factory.post('some/url')
        request.content_type = 'multipart/form-data; boundary='\
                               '------------------------f8317b014f42e05a'
        request.query_params = ''

        upload_bstr = b'test binary data'
        upload_file = BytesIO()
        upload_file.write(upload_bstr)
        upload_file.seek(0)
        upload_data = InMemoryUploadedFile(upload_file,
                                           'file',
                                           'test_file.dat',
                                           'application/octet-stream',
                                           len(upload_bstr),
                                           None,
                                           content_type_extra={})

        request.data = QueryDict(mutable=True)
        request.data['file'] = upload_data
        view.get_request_files = lambda r: {'file': upload_data}

        with patch.object(requests.sessions.Session, 'request') as patched_request:
            with patch.object(view, 'create_response'):
                view.proxy(request)
                args, kwargs = patched_request.call_args
                request_data = kwargs['data']
                self.assertEqual(request_data.files, {'file': upload_data})
                self.assertEqual(request_data.data['file'], upload_data)
Esempio n. 2
0
    def test_postitional_and_keyword_arguments_passed_through_to_proxy_method(
            self):
        proxied_http_methods = ['get', 'put', 'post', 'patch', 'delete']
        request = Mock()
        view = ProxyView()

        for http_method in proxied_http_methods:
            with patch.object(ProxyView, 'proxy') as patched_proxy_method:
                handler = getattr(view, http_method)
                handler(request, 42, foo='bar')

            patched_proxy_method.assert_called_once_with(request,
                                                         42,
                                                         foo='bar')
Esempio n. 3
0
    def test_400_error_default_with_proxied_text(self):
        view = ProxyView()
        view = self.get_view({'RETURN_RAW_ERROR': True})

        responses.add(responses.GET,
                      'http://sample',
                      body="error response text",
                      status=400)

        resp = requests.get('http://sample')
        proxy_resp = view.create_response(resp)

        self.assertEqual(proxy_resp.__class__.__name__, 'HttpResponse')
        self.assertEqual(proxy_resp.content, b'error response text')
        self.assertEqual(proxy_resp.status_code, 400)
        self.assertEqual(proxy_resp._headers['content-type'][1], 'text/plain')
    def test_passes_cookies_through_to_request(self):
        request = Mock()
        view = ProxyView()
        view.get_cookies = lambda r: {'test_cookie': 'value'}

        factory = APIRequestFactory()
        request = factory.post('some/url', data={}, cookies={'original_request_cookie': 'I will not get passed'})
        request.content_type = 'application/json'
        request.query_params = ''
        request.data = {}

        with patch('rest_framework_proxy.views.requests.request') as patched_requests:
            with patch.object(view, 'create_response'):
                view.proxy(request)
                args, kwargs = patched_requests.call_args
                request_cookies = kwargs['cookies']
                self.assertEqual(request_cookies, {'test_cookie': 'value'})
Esempio n. 5
0
    def test_400_error_default_with_proxied_json(self):
        view = ProxyView()
        view = self.get_view({'RETURN_RAW_ERROR': True})

        responses.add(responses.GET,
                      'http://sample',
                      json={'error': 'error response'},
                      status=400)

        resp = requests.get('http://sample')
        proxy_resp = view.create_response(resp)

        self.assertEqual(proxy_resp.__class__.__name__, 'HttpResponse')
        self.assertEqual(proxy_resp.content, b'{"error": "error response"}')
        self.assertEqual(proxy_resp.status_code, 400)
        self.assertEqual(proxy_resp._headers['content-type'][1],
                         'application/json')
    def test_return_raw_audio_file(self):
        view = ProxyView()
        view.return_raw = True
        test_content = b'ID3\x04\x00\x00\x00\x00\x00#TSSE\x00\x00\x00\x0f\x00\x00\x03Lavf57.71.100\x00\xff\xf3'

        factory = APIRequestFactory()
        request = factory.get('http://someurl')
        request.content_type = 'audio/mpeg'
        request.query_params = ''
        request.data = QueryDict(mutable=True)

        with patch.object(requests.sessions.Session, 'request') as patched_request:
            def side_effect(*args, **kwargs):
                response = requests.models.Response()
                response._content = test_content
                response._content_consumed = True
                response.status_code = 200
                return response
            patched_request.side_effect = side_effect
            result = view.proxy(request)
            assert result.content == test_content
Esempio n. 7
0
 def get_view(self, custom_settings=None):
     view = ProxyView()
     view.proxy_settings = settings.APISettings(custom_settings,
                                                settings.DEFAULTS)
     return view