-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_django_test_curl.py
82 lines (72 loc) · 2.46 KB
/
test_django_test_curl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import unittest
from unittest.mock import patch
from . import CurlClient
class CurlTests(unittest.TestCase):
def setUp(self):
self.client = CurlClient()
@patch.object(CurlClient, "get")
def test_curl_extracts_url(self, mock_req):
self.client.curl(
"""
curl http://localhost:8000/api/v1/poop/
"""
)
mock_req.assert_called_once_with("/api/v1/poop/")
@patch.object(CurlClient, "get")
def test_curl_extracts_quoted_url(self, mock_req):
self.client.curl(
"""
curl "http://localhost:8000/api/v1/poop/"
"""
)
mock_req.assert_called_once_with("/api/v1/poop/")
@patch.object(CurlClient, "get")
def test_curl_extracts_get_params(self, mock_req):
self.client.curl(
"""
curl 'http://localhost:8000/api/v1/poop/?foo=a&foo=b'
"""
)
mock_req.assert_called_once_with("/api/v1/poop/?foo=a&foo=b")
@patch.object(CurlClient, "head")
def test_curl_sees_head_implies_head(self, mock_req):
self.client.curl(
"""
curl -I http://localhost:8000/neck-topper
"""
)
mock_req.assert_called_once_with("/neck-topper")
@patch.object(CurlClient, "post")
def test_curl_sees_data_implies_post(self, mock_req):
self.client.curl(
"""
curl -v http://localhost:8000/api/v1/awesome/ \
-H 'content-type: application/json' \
-d '{"foo": "bar"}'
"""
)
mock_req.assert_called_once()
@patch.object(CurlClient, "post")
def test_curl_converts_content_type(self, mock_req):
self.client.curl(
"""
curl -v http://localhost:8000/api/v1/awesome/ \
-H 'content-type: application/json' \
-d '{"foo": "bar"}'
"""
)
mock_req.assert_called_once()
args, kwargs = mock_req.call_args
self.assertEqual(args, ("/api/v1/awesome/",))
self.assertEqual(kwargs["content_type"], "application/json")
self.assertTrue(kwargs["data"])
@patch.object(CurlClient, "get")
def test_curl_converts_auth(self, mock_req):
self.client.curl(
"""
curl http://test:pass@localhost:8000/secret/files/
"""
)
mock_req.assert_called_once_with(
"/secret/files/", HTTP_AUTHORIZATION="Basic dGVzdDpwYXNz"
)