From 508ba34ecc6f24db8b8bb8e1321e6fd07a51773b Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Mon, 10 Jun 2024 04:29:39 +0300 Subject: [PATCH] Fix double-escaping of the curl and Python example code (#709) * Fix double-escaping of the curl and Python example code These code fragments are escaped when they are substituted into the `silk/request.html` template, so they should not be escaped an extra time when they are constructed. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- project/tests/test_code_gen_curl.py | 23 ++++++++++++++++++++++ project/tests/test_code_gen_django.py | 20 +++++++++++++++++-- silk/code_generation/curl.py | 4 ++-- silk/code_generation/django_test_client.py | 4 ++-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 project/tests/test_code_gen_curl.py diff --git a/project/tests/test_code_gen_curl.py b/project/tests/test_code_gen_curl.py new file mode 100644 index 00000000..2288c2f3 --- /dev/null +++ b/project/tests/test_code_gen_curl.py @@ -0,0 +1,23 @@ +import shlex +from unittest import TestCase + +from silk.code_generation.curl import curl_cmd + + +class TestCodeGenCurl(TestCase): + def test_post_json(self): + result = curl_cmd( + url="https://example.org/alpha/beta", + method="POST", + body={"gamma": "delta"}, + content_type="application/json", + ) + + result_words = shlex.split(result) + + self.assertEqual(result_words, [ + 'curl', '-X', 'POST', + '-H', 'content-type: application/json', + '-d', '{"gamma": "delta"}', + 'https://example.org/alpha/beta' + ]) diff --git a/project/tests/test_code_gen_django.py b/project/tests/test_code_gen_django.py index 2cfca135..a5b3872a 100644 --- a/project/tests/test_code_gen_django.py +++ b/project/tests/test_code_gen_django.py @@ -1,6 +1,22 @@ -from django.test import TestCase +import textwrap +from unittest import TestCase + +from silk.code_generation.django_test_client import gen class TestCodeGenDjango(TestCase): + def test_post(self): + result = gen( + path="/alpha/beta", + method="POST", + data={"gamma": "delta", "epsilon": "zeta"}, + content_type="application/x-www-form-urlencoded", + ) - pass + self.assertEqual(result, textwrap.dedent("""\ + from django.test import Client + c = Client() + response = c.post(path='/alpha/beta', + data={'gamma': 'delta', 'epsilon': 'zeta'}, + content_type='application/x-www-form-urlencoded') + """)) diff --git a/silk/code_generation/curl.py b/silk/code_generation/curl.py index 1181a92d..db135911 100644 --- a/silk/code_generation/curl.py +++ b/silk/code_generation/curl.py @@ -3,7 +3,7 @@ from django.template import Context, Template -curl_template = """ +curl_template = """\ curl {% if method %}-X {{ method }}{% endif %} {% if content_type %}-H 'content-type: {{ content_type }}'{% endif %} {% if modifier %}{{ modifier }} {% endif %}{% if body %}'{{ body }}'{% endif %} @@ -66,4 +66,4 @@ def curl_cmd(url, method=None, query_params=None, body=None, content_type=None): 'content_type': content_type, 'extra': extra, } - return t.render(Context(context)).replace('\n', ' ') + return t.render(Context(context, autoescape=False)).replace('\n', ' ') diff --git a/silk/code_generation/django_test_client.py b/silk/code_generation/django_test_client.py index b097c201..cd34cd5f 100644 --- a/silk/code_generation/django_test_client.py +++ b/silk/code_generation/django_test_client.py @@ -5,7 +5,7 @@ from silk.profiling.dynamic import is_str_typ -template = """ +template = """\ from django.test import Client c = Client() response = c.{{ lower_case_method }}(path='{{ path }}'{% if data or content_type %},{% else %}){% endif %}{% if data %} @@ -43,6 +43,6 @@ def gen(path, method=None, query_params=None, data=None, content_type=None): context['data'] = data context['query_params'] = query_params return autopep8.fix_code( - t.render(Context(context)), + t.render(Context(context, autoescape=False)), options=autopep8.parse_args(['--aggressive', '']), )