Skip to content

Commit 508ba34

Browse files
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>
1 parent 1cb4623 commit 508ba34

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

project/tests/test_code_gen_curl.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import shlex
2+
from unittest import TestCase
3+
4+
from silk.code_generation.curl import curl_cmd
5+
6+
7+
class TestCodeGenCurl(TestCase):
8+
def test_post_json(self):
9+
result = curl_cmd(
10+
url="https://example.org/alpha/beta",
11+
method="POST",
12+
body={"gamma": "delta"},
13+
content_type="application/json",
14+
)
15+
16+
result_words = shlex.split(result)
17+
18+
self.assertEqual(result_words, [
19+
'curl', '-X', 'POST',
20+
'-H', 'content-type: application/json',
21+
'-d', '{"gamma": "delta"}',
22+
'https://example.org/alpha/beta'
23+
])

project/tests/test_code_gen_django.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
from django.test import TestCase
1+
import textwrap
2+
from unittest import TestCase
3+
4+
from silk.code_generation.django_test_client import gen
25

36

47
class TestCodeGenDjango(TestCase):
8+
def test_post(self):
9+
result = gen(
10+
path="/alpha/beta",
11+
method="POST",
12+
data={"gamma": "delta", "epsilon": "zeta"},
13+
content_type="application/x-www-form-urlencoded",
14+
)
515

6-
pass
16+
self.assertEqual(result, textwrap.dedent("""\
17+
from django.test import Client
18+
c = Client()
19+
response = c.post(path='/alpha/beta',
20+
data={'gamma': 'delta', 'epsilon': 'zeta'},
21+
content_type='application/x-www-form-urlencoded')
22+
"""))

silk/code_generation/curl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from django.template import Context, Template
55

6-
curl_template = """
6+
curl_template = """\
77
curl {% if method %}-X {{ method }}{% endif %}
88
{% if content_type %}-H 'content-type: {{ content_type }}'{% endif %}
99
{% 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):
6666
'content_type': content_type,
6767
'extra': extra,
6868
}
69-
return t.render(Context(context)).replace('\n', ' ')
69+
return t.render(Context(context, autoescape=False)).replace('\n', ' ')

silk/code_generation/django_test_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from silk.profiling.dynamic import is_str_typ
77

8-
template = """
8+
template = """\
99
from django.test import Client
1010
c = Client()
1111
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):
4343
context['data'] = data
4444
context['query_params'] = query_params
4545
return autopep8.fix_code(
46-
t.render(Context(context)),
46+
t.render(Context(context, autoescape=False)),
4747
options=autopep8.parse_args(['--aggressive', '']),
4848
)

0 commit comments

Comments
 (0)