From 9c4614b368d188463a66c8cfcca3f018098f9b0f Mon Sep 17 00:00:00 2001 From: ziyacivan Date: Tue, 6 Feb 2024 14:43:44 +0300 Subject: [PATCH 1/7] Add support for multipart/form-data requests*** --- silk/model_factory.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/silk/model_factory.py b/silk/model_factory.py index a802beb4..8b8965e5 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -226,6 +226,15 @@ def construct_request_model(self): query_params = self.query_params() path = self.request.path view_name = self.view_name() + content_type = self.request.content_type + + if content_type.startswith('muştipart/form-data'): + form_data = self.request.POST.dict() + files = {f: self.request.FILES[f].name for f in self.request.FILES} + body = {'form_data': form_data, 'files': files} + raw_body = None # Raw body is not available for multipart/form-data + else: + body, raw_body = self.body() request_model = models.Request.objects.create( path=path, @@ -234,12 +243,15 @@ def construct_request_model(self): query_params=query_params, view_name=view_name, body=body) + # Text fields are encoded as UTF-8 in Django and hence will try to coerce # anything to we pass to UTF-8. Some stuff like binary will fail. - try: - request_model.raw_body = raw_body - except UnicodeDecodeError: - Logger.debug('NYI: Binary request bodies') # TODO + if raw_body is not None: + try: + request_model.raw_body = raw_body + except UnicodeDecodeError: + Logger.debug('NYI: Binary request bodies') # TODO + Logger.debug('Created new request model with pk %s' % request_model.pk) return request_model From 8cc1d29e0224d5347e37fb7c78a48414e13cfa07 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Feb 2024 11:48:20 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- silk/model_factory.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/silk/model_factory.py b/silk/model_factory.py index 8b8965e5..ae05d76b 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -227,12 +227,12 @@ def construct_request_model(self): path = self.request.path view_name = self.view_name() content_type = self.request.content_type - + if content_type.startswith('muştipart/form-data'): form_data = self.request.POST.dict() files = {f: self.request.FILES[f].name for f in self.request.FILES} body = {'form_data': form_data, 'files': files} - raw_body = None # Raw body is not available for multipart/form-data + raw_body = None # Raw body is not available for multipart/form-data else: body, raw_body = self.body() @@ -243,7 +243,7 @@ def construct_request_model(self): query_params=query_params, view_name=view_name, body=body) - + # Text fields are encoded as UTF-8 in Django and hence will try to coerce # anything to we pass to UTF-8. Some stuff like binary will fail. if raw_body is not None: @@ -251,7 +251,7 @@ def construct_request_model(self): request_model.raw_body = raw_body except UnicodeDecodeError: Logger.debug('NYI: Binary request bodies') # TODO - + Logger.debug('Created new request model with pk %s' % request_model.pk) return request_model From 6e61db6e4ae24088b257504f30a769a5cefb318f Mon Sep 17 00:00:00 2001 From: ziyacivan Date: Tue, 6 Feb 2024 15:46:49 +0300 Subject: [PATCH 3/7] Refactor handling of multipart/form-data in RequestModelFactory --- silk/model_factory.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/silk/model_factory.py b/silk/model_factory.py index 8b8965e5..bb9d4a10 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -229,9 +229,10 @@ def construct_request_model(self): content_type = self.request.content_type if content_type.startswith('muştipart/form-data'): - form_data = self.request.POST.dict() - files = {f: self.request.FILES[f].name for f in self.request.FILES} - body = {'form_data': form_data, 'files': files} + body = { + 'form_data': self.request.POST.dict(), + 'files': {f: self.request.FILES[f].name for f in self.request.FILES} + } raw_body = None # Raw body is not available for multipart/form-data else: body, raw_body = self.body() From 4da9b620efd10a59a1e149ca6718681403d60fb7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:47:41 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- silk/model_factory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/silk/model_factory.py b/silk/model_factory.py index 38102fed..c6bde940 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -233,7 +233,7 @@ def construct_request_model(self): 'form_data': self.request.POST.dict(), 'files': {f: self.request.FILES[f].name for f in self.request.FILES} } - raw_body = None # Raw body is not available for multipart/form-data + raw_body = None # Raw body is not available for multipart/form-data else: body, raw_body = self.body() From 3f0c0720f2da967d8813d9dde5c26ff25fb6ee74 Mon Sep 17 00:00:00 2001 From: ziyacivan Date: Tue, 6 Feb 2024 23:41:23 +0300 Subject: [PATCH 5/7] Fix content type typo in model_factory.py --- silk/model_factory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/silk/model_factory.py b/silk/model_factory.py index 38102fed..d68024f9 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -228,7 +228,7 @@ def construct_request_model(self): view_name = self.view_name() content_type = self.request.content_type - if content_type.startswith('muştipart/form-data'): + if content_type.startswith('multipart/form-data'): body = { 'form_data': self.request.POST.dict(), 'files': {f: self.request.FILES[f].name for f in self.request.FILES} From eb057a62519546830420a8ab2bb5e3bba03929fb Mon Sep 17 00:00:00 2001 From: 50-Course Date: Mon, 25 Mar 2024 18:44:30 +0100 Subject: [PATCH 6/7] test(multipart_form_data): test form request creation works correctly This test-patch addresses the newly updated patch changes when dealing with `multipart/form_data` request --- project/tests/test_multipart_forms.py | 38 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/project/tests/test_multipart_forms.py b/project/tests/test_multipart_forms.py index f4e9a8b6..76a46890 100644 --- a/project/tests/test_multipart_forms.py +++ b/project/tests/test_multipart_forms.py @@ -8,14 +8,44 @@ class TestMultipartForms(TestCase): + def setUp(self): + self.mock_request = Mock() + self.mock_request.method = "POST" + self.mock_request.GET = {} + self.mock_request.path = reverse("silk:requests") + self.mock_request.headers = {"content-type": multipart_form} + self.mock_request.FILES = {"file": "file.txt", "file2": "file2.pdf", "file3": "file3.jpg"} + def test_no_max_request(self): mock_request = Mock() - mock_request.headers = {'content-type': multipart_form} + mock_request.headers = {"content-type": multipart_form} mock_request.GET = {} - mock_request.path = reverse('silk:requests') - mock_request.method = 'post' + mock_request.path = reverse("silk:requests") + mock_request.method = "post" mock_request.body = Mock() request_model = RequestModelFactory(mock_request).construct_request_model() self.assertFalse(request_model.body) - self.assertEqual(b"Raw body not available for multipart_form data, Silk is not showing file uploads.", request_model.raw_body) + self.assertEqual( + b"Raw body not available for multipart_form data, Silk is not showing file uploads.", + request_model.raw_body, + ) mock_request.body.assert_not_called() + + def test_multipart_form_request_creation_raises_no_exception(self): + """ + Test that a request with multipart form data is created correctly without raising excetions + """ + request_model = RequestModelFactory(self.mock_request).construct_request_model() + self.assertTrue(request_model.body) + self.assertEqual( + { + 'form_data': {}, + 'files': { + 'file': 'file.txt', + 'file2': 'file2.pdf', + 'file3': 'file3.jpg' + } + }, + request_model.body + ) + self.assertIsNone(request_model.raw_body) From 212b987162081ac28cd8fcecc7df1a72d8c6b71d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:48:39 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project/tests/test_multipart_forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/tests/test_multipart_forms.py b/project/tests/test_multipart_forms.py index 76a46890..9f217b53 100644 --- a/project/tests/test_multipart_forms.py +++ b/project/tests/test_multipart_forms.py @@ -32,7 +32,7 @@ def test_no_max_request(self): mock_request.body.assert_not_called() def test_multipart_form_request_creation_raises_no_exception(self): - """ + """ Test that a request with multipart form data is created correctly without raising excetions """ request_model = RequestModelFactory(self.mock_request).construct_request_model()