| 1 |
import pytest |
| 2 |
import responses |
| 3 |
|
| 4 |
|
| 5 |
def test_cannot_encode_dict_as_text(client): |
| 6 |
with pytest.raises(TypeError): |
| 7 |
client.post("/bar", data={"baz": 1}, encoding="text") |
| 8 |
|
| 9 |
|
| 10 |
def test_cannot_encode_with_unknown_encoding(client): |
| 11 |
with pytest.raises(ValueError): |
| 12 |
client.post("/bar", data={"baz": 1}, encoding="some-random-string") |
| 13 |
|
| 14 |
|
| 15 |
def test_cannot_form_encode_bad_types(client): |
| 16 |
with pytest.raises(TypeError): |
| 17 |
client.post("/bar", data=["baz", "buzz"], encoding="form") |
| 18 |
with pytest.raises(TypeError): |
| 19 |
client.post("/bar", data=1, encoding="form") |
| 20 |
|
| 21 |
|
| 22 |
def test_form_encoding_works(client): |
| 23 |
responses.add(responses.POST, "https://foo.api.globus.org/bar", body="hi") |
| 24 |
client.post("/bar", data={"baz": 1}, encoding="form") |
| 25 |
|
| 26 |
last_req = responses.calls[-1].request |
| 27 |
assert last_req.body == "baz=1" |
| 28 |
|
| 29 |
|
| 30 |
def test_text_encoding_sends_ascii_string(client): |
| 31 |
responses.add(responses.POST, "https://foo.api.globus.org/bar", body="hi") |
| 32 |
client.post("/bar", data="baz", encoding="text") |
| 33 |
|
| 34 |
last_req = responses.calls[-1].request |
| 35 |
assert last_req.body == "baz" |
| 36 |
|
| 37 |
|
| 38 |
def test_text_encoding_can_send_non_ascii_utf8_bytes(client): |
| 39 |
# this test is a reproducer for an issue in which attempting to send these bytes |
| 40 |
# in the form of a (decoded) string would fail, as urllib3 tried to encode them as |
| 41 |
# latin-1 bytes incorrectly |
| 42 |
# passing the bytes already UTF-8 encoded should work |
| 43 |
responses.add(responses.POST, "https://foo.api.globus.org/bar", body="hi") |
| 44 |
client.post("/bar", data='{"field“: "value“}'.encode(), encoding="text") |
| 45 |
|
| 46 |
last_req = responses.calls[-1].request |
| 47 |
assert last_req.body == '{"field“: "value“}'.encode() |