|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | + |
| 5 | +""" Post Tests """ |
| 6 | +# pylint: disable=line-too-long |
| 7 | + |
| 8 | +from unittest import mock |
| 9 | + |
| 10 | +import mocket |
| 11 | +import pytest |
| 12 | + |
| 13 | +""" |
| 14 | +For building tests, you can use CPython requests with logging to see what should actuall get sent. |
| 15 | +
|
| 16 | +import logging |
| 17 | +import http.client |
| 18 | +import requests |
| 19 | +
|
| 20 | +def httpclient_logging_patch(level=logging.DEBUG): |
| 21 | + logging.basicConfig(level=level) |
| 22 | +
|
| 23 | + httpclient_logger = logging.getLogger("http.client") |
| 24 | +
|
| 25 | + def httpclient_log(*args): |
| 26 | + httpclient_logger.log(level, " ".join(args)) |
| 27 | +
|
| 28 | + http.client.print = httpclient_log |
| 29 | + http.client.HTTPConnection.debuglevel = 1 |
| 30 | +
|
| 31 | +httpclient_logging_patch() |
| 32 | +
|
| 33 | +URL = "https://httpbin.org/post" |
| 34 | +
|
| 35 | +with open("tests/files/red_green.png", "rb") as file_1: |
| 36 | + file_data = { |
| 37 | + "file_1": ( |
| 38 | + "red_green.png", |
| 39 | + file_1, |
| 40 | + "image/png", |
| 41 | + { |
| 42 | + "Key_1": "Value 1", |
| 43 | + "Key_2": "Value 2", |
| 44 | + "Key_3": "Value 3", |
| 45 | + }, |
| 46 | + ), |
| 47 | + } |
| 48 | +
|
| 49 | + print(requests.post(URL, files=file_data).json()) |
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +def test_post_files_text(sock, requests): |
| 54 | + file_data = { |
| 55 | + "key_4": (None, "Value 5"), |
| 56 | + } |
| 57 | + |
| 58 | + requests._build_boundary_string = mock.Mock( |
| 59 | + return_value="8cd45159712eeb914c049c717d3f4d75" |
| 60 | + ) |
| 61 | + requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data) |
| 62 | + |
| 63 | + sock.connect.assert_called_once_with((mocket.MOCK_POOL_IP, 80)) |
| 64 | + sock.send.assert_has_calls( |
| 65 | + [ |
| 66 | + mock.call(b"Content-Type"), |
| 67 | + mock.call(b": "), |
| 68 | + mock.call( |
| 69 | + b"multipart/form-data; boundary=8cd45159712eeb914c049c717d3f4d75" |
| 70 | + ), |
| 71 | + mock.call(b"\r\n"), |
| 72 | + ] |
| 73 | + ) |
| 74 | + sock.send.assert_has_calls( |
| 75 | + [ |
| 76 | + mock.call( |
| 77 | + b'--8cd45159712eeb914c049c717d3f4d75\r\nContent-Disposition: form-data; name="key_4"\r\n\r\n' |
| 78 | + ), |
| 79 | + mock.call(b"Value 5\r\n"), |
| 80 | + mock.call(b"--8cd45159712eeb914c049c717d3f4d75--\r\n"), |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_post_files_file(sock, requests): |
| 86 | + with open("tests/files/red_green.png", "rb") as file_1: |
| 87 | + file_data = { |
| 88 | + "file_1": ( |
| 89 | + "red_green.png", |
| 90 | + file_1, |
| 91 | + "image/png", |
| 92 | + { |
| 93 | + "Key_1": "Value 1", |
| 94 | + "Key_2": "Value 2", |
| 95 | + "Key_3": "Value 3", |
| 96 | + }, |
| 97 | + ), |
| 98 | + } |
| 99 | + |
| 100 | + requests._build_boundary_string = mock.Mock( |
| 101 | + return_value="e663061c5bfcc53139c8f68d016cbef3" |
| 102 | + ) |
| 103 | + requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data) |
| 104 | + |
| 105 | + sock.connect.assert_called_once_with((mocket.MOCK_POOL_IP, 80)) |
| 106 | + sock.send.assert_has_calls( |
| 107 | + [ |
| 108 | + mock.call(b"Content-Type"), |
| 109 | + mock.call(b": "), |
| 110 | + mock.call( |
| 111 | + b"multipart/form-data; boundary=e663061c5bfcc53139c8f68d016cbef3" |
| 112 | + ), |
| 113 | + mock.call(b"\r\n"), |
| 114 | + ] |
| 115 | + ) |
| 116 | + sock.send.assert_has_calls( |
| 117 | + [ |
| 118 | + mock.call( |
| 119 | + b'--e663061c5bfcc53139c8f68d016cbef3\r\nContent-Disposition: form-data; name="file_1"; filename="red_green.png"\r\nContent-Type: image/png\r\nKey_1: Value 1\r\nKey_2: Value 2\r\nKey_3: Value 3\r\n\r\n' |
| 120 | + ), |
| 121 | + mock.call( |
| 122 | + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9a" |
| 123 | + ), |
| 124 | + mock.call( |
| 125 | + b"s\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00" |
| 126 | + ), |
| 127 | + mock.call( |
| 128 | + b"\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x00\x10IDAT\x18Wc\xf8\xcf" |
| 129 | + ), |
| 130 | + mock.call( |
| 131 | + b"\xc0\x00\xc5\xff\x19\x18\x00\x1d\xf0\x03\xfd\x8fk\x13|\x00\x00\x00\x00IEND\xaeB`\x82" |
| 132 | + ), |
| 133 | + mock.call(b"\r\n"), |
| 134 | + mock.call(b"--e663061c5bfcc53139c8f68d016cbef3--\r\n"), |
| 135 | + ] |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +def test_post_files_complex(sock, requests): |
| 140 | + with open("tests/files/red_green.png", "rb") as file_1, open( |
| 141 | + "tests/files/green_red.png", "rb" |
| 142 | + ) as file_2: |
| 143 | + file_data = { |
| 144 | + "file_1": ( |
| 145 | + "red_green.png", |
| 146 | + file_1, |
| 147 | + "image/png", |
| 148 | + { |
| 149 | + "Key_1": "Value 1", |
| 150 | + "Key_2": "Value 2", |
| 151 | + "Key_3": "Value 3", |
| 152 | + }, |
| 153 | + ), |
| 154 | + "key_4": (None, "Value 5"), |
| 155 | + "file_2": ( |
| 156 | + "green_red.png", |
| 157 | + file_2, |
| 158 | + "image/png", |
| 159 | + ), |
| 160 | + "key_6": (None, "Value 6"), |
| 161 | + } |
| 162 | + |
| 163 | + requests._build_boundary_string = mock.Mock( |
| 164 | + return_value="e663061c5bfcc53139c8f68d016cbef3" |
| 165 | + ) |
| 166 | + requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data) |
| 167 | + |
| 168 | + sock.connect.assert_called_once_with((mocket.MOCK_POOL_IP, 80)) |
| 169 | + sock.send.assert_has_calls( |
| 170 | + [ |
| 171 | + mock.call(b"Content-Type"), |
| 172 | + mock.call(b": "), |
| 173 | + mock.call( |
| 174 | + b"multipart/form-data; boundary=e663061c5bfcc53139c8f68d016cbef3" |
| 175 | + ), |
| 176 | + mock.call(b"\r\n"), |
| 177 | + ] |
| 178 | + ) |
| 179 | + sock.send.assert_has_calls( |
| 180 | + [ |
| 181 | + mock.call( |
| 182 | + b'--e663061c5bfcc53139c8f68d016cbef3\r\nContent-Disposition: form-data; name="file_1"; filename="red_green.png"\r\nContent-Type: image/png\r\nKey_1: Value 1\r\nKey_2: Value 2\r\nKey_3: Value 3\r\n\r\n' |
| 183 | + ), |
| 184 | + mock.call( |
| 185 | + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9a" |
| 186 | + ), |
| 187 | + mock.call( |
| 188 | + b"s\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00" |
| 189 | + ), |
| 190 | + mock.call( |
| 191 | + b"\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x00\x10IDAT\x18Wc\xf8\xcf" |
| 192 | + ), |
| 193 | + mock.call( |
| 194 | + b"\xc0\x00\xc5\xff\x19\x18\x00\x1d\xf0\x03\xfd\x8fk\x13|\x00\x00\x00\x00IEND\xaeB`\x82" |
| 195 | + ), |
| 196 | + mock.call(b"\r\n"), |
| 197 | + mock.call( |
| 198 | + b'--e663061c5bfcc53139c8f68d016cbef3\r\nContent-Disposition: form-data; name="key_4"\r\n\r\n' |
| 199 | + ), |
| 200 | + mock.call(b"Value 5\r\n"), |
| 201 | + mock.call( |
| 202 | + b'--e663061c5bfcc53139c8f68d016cbef3\r\nContent-Disposition: form-data; name="file_2"; filename="green_red.png"\r\nContent-Type: image/png\r\n\r\n' |
| 203 | + ), |
| 204 | + mock.call( |
| 205 | + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9a" |
| 206 | + ), |
| 207 | + mock.call( |
| 208 | + b"s\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00" |
| 209 | + ), |
| 210 | + mock.call( |
| 211 | + b"\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x00\x12IDAT\x18Wc`\xf8" |
| 212 | + ), |
| 213 | + mock.call( |
| 214 | + b'\x0f\x84 \x92\x81\xe1?\x03\x00\x1d\xf0\x03\xfd\x88"uS\x00\x00\x00\x00IEND\xaeB`\x82' |
| 215 | + ), |
| 216 | + mock.call(b"\r\n"), |
| 217 | + mock.call( |
| 218 | + b'--e663061c5bfcc53139c8f68d016cbef3\r\nContent-Disposition: form-data; name="key_6"\r\n\r\n' |
| 219 | + ), |
| 220 | + mock.call(b"Value 6\r\n"), |
| 221 | + mock.call(b"--e663061c5bfcc53139c8f68d016cbef3--\r\n"), |
| 222 | + ] |
| 223 | + ) |
| 224 | + |
| 225 | + |
| 226 | +def test_post_files_not_binary(requests): |
| 227 | + with open("tests/files/red_green.png", "r") as file_1: |
| 228 | + file_data = { |
| 229 | + "file_1": ( |
| 230 | + "red_green.png", |
| 231 | + file_1, |
| 232 | + "image/png", |
| 233 | + ), |
| 234 | + } |
| 235 | + |
| 236 | + with pytest.raises(AttributeError) as context: |
| 237 | + requests.post("http://" + mocket.MOCK_HOST_1 + "/post", files=file_data) |
| 238 | + assert "Files must be opened in binary mode" in str(context) |
0 commit comments