From 4822b484f07a40bce784459d09de0aecb4b42b84 Mon Sep 17 00:00:00 2001
From: Roman Donchenko <rdonchen@outlook.com>
Date: Mon, 11 Mar 2024 23:20:43 +0200
Subject: [PATCH 1/2] 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.
---
 project/tests/test_code_gen_curl.py        | 23 ++++++++++++++++++++++
 project/tests/test_code_gen_django.py      | 19 ++++++++++++++++--
 silk/code_generation/curl.py               |  4 ++--
 silk/code_generation/django_test_client.py |  4 ++--
 4 files changed, 44 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..c9a54128
--- /dev/null
+++ b/project/tests/test_code_gen_curl.py
@@ -0,0 +1,23 @@
+import shlex
+import textwrap
+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..314254ea 100644
--- a/project/tests/test_code_gen_django.py
+++ b/project/tests/test_code_gen_django.py
@@ -1,6 +1,21 @@
-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', '']),
     )

From 8f966585a38c2575096c33c07fa833b6d2192e50 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Mon, 11 Mar 2024 21:28:43 +0000
Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 project/tests/test_code_gen_curl.py   | 2 +-
 project/tests/test_code_gen_django.py | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/project/tests/test_code_gen_curl.py b/project/tests/test_code_gen_curl.py
index c9a54128..2288c2f3 100644
--- a/project/tests/test_code_gen_curl.py
+++ b/project/tests/test_code_gen_curl.py
@@ -1,9 +1,9 @@
 import shlex
-import textwrap
 from unittest import TestCase
 
 from silk.code_generation.curl import curl_cmd
 
+
 class TestCodeGenCurl(TestCase):
     def test_post_json(self):
         result = curl_cmd(
diff --git a/project/tests/test_code_gen_django.py b/project/tests/test_code_gen_django.py
index 314254ea..a5b3872a 100644
--- a/project/tests/test_code_gen_django.py
+++ b/project/tests/test_code_gen_django.py
@@ -3,6 +3,7 @@
 
 from silk.code_generation.django_test_client import gen
 
+
 class TestCodeGenDjango(TestCase):
     def test_post(self):
         result = gen(