Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double-escaping of the curl and Python example code #709

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', ' ')
Archmonger marked this conversation as resolved.
Show resolved Hide resolved
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', '']),
)
Loading