Skip to content

Commit

Permalink
Fix double-escaping of the curl and Python example code (#709)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
SpecLad and pre-commit-ci[bot] authored Jun 10, 2024
1 parent 1cb4623 commit 508ba34
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
23 changes: 23 additions & 0 deletions project/tests/test_code_gen_curl.py
Original file line number Diff line number Diff line change
@@ -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'
])
20 changes: 18 additions & 2 deletions project/tests/test_code_gen_django.py
Original file line number Diff line number Diff line change
@@ -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')
"""))
4 changes: 2 additions & 2 deletions silk/code_generation/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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', ' ')
4 changes: 2 additions & 2 deletions silk/code_generation/django_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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', '']),
)

0 comments on commit 508ba34

Please sign in to comment.