|
| 1 | + |
| 2 | +from http_code import HttpCode |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.parametrize( |
| 7 | + "code, expected_name, expected_label", |
| 8 | + [ |
| 9 | + (HttpCode.CONTINUE, "CONTINUE", "Continue"), |
| 10 | + (HttpCode.SWITCH_PROTOCOL, "SWITCH_PROTOCOL", "Switch Protocol"), |
| 11 | + (HttpCode.PROCESSING, "PROCESSING", "Processing"), |
| 12 | + (HttpCode.EARLY_HINTS, "EARLY_HINTS", "Early Hints"), |
| 13 | + ], |
| 14 | + ids=[ |
| 15 | + "100 Continue", |
| 16 | + "101 Switch Protocol", |
| 17 | + "102 Processing", |
| 18 | + "103 Early Hints", |
| 19 | + ] |
| 20 | +) |
| 21 | +def test_informational_codes(code, expected_name, expected_label): |
| 22 | + """Test informational HTTP status codes""" |
| 23 | + assert code == getattr(HttpCode, expected_name) |
| 24 | + assert code.label() == expected_label |
| 25 | + assert str(code) == f"{code.value} {expected_label}" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "code, expected_name, expected_label", |
| 30 | + [ |
| 31 | + (HttpCode.OK, "OK", "Ok"), |
| 32 | + (HttpCode.CREATED, "CREATED", "Created"), |
| 33 | + (HttpCode.ACCEPTED, "ACCEPTED", "Accepted"), |
| 34 | + (HttpCode.NON_AUTHORITATIVE_INFORMATION, "NON_AUTHORITATIVE_INFORMATION", "Non Authoritative Information"), |
| 35 | + (HttpCode.NO_CONTENT, "NO_CONTENT", "No Content"), |
| 36 | + (HttpCode.RESET_CONTENT, "RESET_CONTENT", "Reset Content"), |
| 37 | + (HttpCode.PARTIAL_CONTENT, "PARTIAL_CONTENT", "Partial Content"), |
| 38 | + (HttpCode.MULTI_STATUS, "MULTI_STATUS", "Multi Status"), |
| 39 | + (HttpCode.ALREADY_REPORTED, "ALREADY_REPORTED", "Already Reported"), |
| 40 | + (HttpCode.IM_USED, "IM_USED", "Im Used"), |
| 41 | + ], |
| 42 | + ids=[ |
| 43 | + "200 OK", |
| 44 | + "201 Created", |
| 45 | + "202 Accepted", |
| 46 | + "203 Non-Authoritative Information", |
| 47 | + "204 No Content", |
| 48 | + "205 Reset Content", |
| 49 | + "206 Partial Content", |
| 50 | + "207 Multi-Status", |
| 51 | + "208 Already Reported", |
| 52 | + "226 IM Used", |
| 53 | + ] |
| 54 | +) |
| 55 | +def test_success_codes(code, expected_name, expected_label): |
| 56 | + """Test informational HTTP status codes""" |
| 57 | + assert code == getattr(HttpCode, expected_name) |
| 58 | + assert code.label() == expected_label |
| 59 | + assert str(code) == f"{code.value} {expected_label}" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "code, expected_name, expected_label", |
| 64 | + [ |
| 65 | + (HttpCode.MULTIPLE_CHOICES, "MULTIPLE_CHOICES", "Multiple Choices"), |
| 66 | + (HttpCode.MOVED_PERMANENTLY, "MOVED_PERMANENTLY", "Moved Permanently"), |
| 67 | + (HttpCode.FOUND, "FOUND", "Found"), |
| 68 | + (HttpCode.SEE_OTHER, "SEE_OTHER", "See Other"), |
| 69 | + (HttpCode.NOT_MODIFIED, "NOT_MODIFIED", "Not Modified"), |
| 70 | + (HttpCode.TEMPORARY_REDIRECT, "TEMPORARY_REDIRECT", "Temporary Redirect"), |
| 71 | + (HttpCode.PERMANENT_REDIRECT, "PERMANENT_REDIRECT", "Permanent Redirect"), |
| 72 | + ], |
| 73 | + ids=[ |
| 74 | + "300 Multiple Choices", |
| 75 | + "301 Moved Permanently", |
| 76 | + "302 Found", |
| 77 | + "303 See Other", |
| 78 | + "304 Not Modified", |
| 79 | + "307 Temporary Redirect", |
| 80 | + "308 Permanent Redirect", |
| 81 | + ] |
| 82 | +) |
| 83 | +def test_redirection_codes(code, expected_name, expected_label): |
| 84 | + """Test redirection HTTP status codes""" |
| 85 | + assert code == getattr(HttpCode, expected_name) |
| 86 | + assert code.label() == expected_label |
| 87 | + assert str(code) == f"{code.value} {expected_label}" |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize( |
| 91 | + "code, expected_name, expected_label", |
| 92 | + [ |
| 93 | + (HttpCode.BAD_REQUEST, "BAD_REQUEST", "Bad Request"), |
| 94 | + (HttpCode.UNAUTHORIZED, "UNAUTHORIZED", "Unauthorized"), |
| 95 | + (HttpCode.PAYMENT_REQUIRED, "PAYMENT_REQUIRED", "Payment Required"), |
| 96 | + (HttpCode.FORBIDDEN, "FORBIDDEN", "Forbidden"), |
| 97 | + (HttpCode.NOT_FOUND, "NOT_FOUND", "Not Found"), |
| 98 | + (HttpCode.METHOD_NOT_ALLOWED, "METHOD_NOT_ALLOWED", "Method Not Allowed"), |
| 99 | + (HttpCode.NOT_ACCEPTABLE, "NOT_ACCEPTABLE", "Not Acceptable"), |
| 100 | + (HttpCode.PROXY_AUTHENTICATION_REQUIRED, "PROXY_AUTHENTICATION_REQUIRED", "Proxy Authentication Required"), |
| 101 | + (HttpCode.REQUEST_TIMEOUT, "REQUEST_TIMEOUT", "Request Timeout"), |
| 102 | + (HttpCode.CONFLICT, "CONFLICT", "Conflict"), |
| 103 | + (HttpCode.GONE, "GONE", "Gone"), |
| 104 | + (HttpCode.LENGTH_REQUIRED, "LENGTH_REQUIRED", "Length Required"), |
| 105 | + (HttpCode.PRECONDITION_FAILED, "PRECONDITION_FAILED", "Precondition Failed"), |
| 106 | + (HttpCode.PAYLOAD_TOO_LARGE, "PAYLOAD_TOO_LARGE", "Payload Too Large"), |
| 107 | + (HttpCode.URI_TOO_LONG, "URI_TOO_LONG", "Uri Too Long"), |
| 108 | + (HttpCode.UNSUPPORTED_MEDIA_TYPE, "UNSUPPORTED_MEDIA_TYPE", "Unsupported Media Type"), |
| 109 | + (HttpCode.RANGE_NOT_SATISFIABLE, "RANGE_NOT_SATISFIABLE", "Range Not Satisfiable"), |
| 110 | + (HttpCode.EXPECTATION_FAILED, "EXPECTATION_FAILED", "Expectation Failed"), |
| 111 | + (HttpCode.IM_A_TEAPOT, 'IM_A_TEAPOT', 'Im A Teapot'), # RFC 7168 |
| 112 | + (HttpCode.MISDIRECTED_REQUEST, 'MISDIRECTED_REQUEST', 'Misdirected Request'), |
| 113 | + (HttpCode.UNPROCESSABLE_CONTENT, 'UNPROCESSABLE_CONTENT', 'Unprocessable Content'), |
| 114 | + (HttpCode.LOCKED, 'LOCKED', 'Locked'), |
| 115 | + (HttpCode.FAILED_DEPENDENCY, 'FAILED_DEPENDENCY', 'Failed Dependency'), |
| 116 | + ], |
| 117 | + ids=[ |
| 118 | + "400 Bad Request", |
| 119 | + "401 Unauthorized", |
| 120 | + "402 Payment Required", |
| 121 | + "403 Forbidden", |
| 122 | + "404 Not Found", |
| 123 | + "405 Method", |
| 124 | + "406 Not Acceptable", |
| 125 | + "407 Proxy Authentication Required", |
| 126 | + "408 Request Timeout", |
| 127 | + "409 Conflict", |
| 128 | + "410 Gone", |
| 129 | + "411 Length Required", |
| 130 | + "412 Precondition Failed", |
| 131 | + "413 Payload Too Large", |
| 132 | + "414 URI Too Long", |
| 133 | + "415 Unsupported Media Type", |
| 134 | + "416 Range Not Satisfiable", |
| 135 | + "417 Expectation failed", |
| 136 | + "418 Im A Teapot", |
| 137 | + "421 Misdirected Request", |
| 138 | + "422 Unprocessable Content", |
| 139 | + "423 Locked", |
| 140 | + "424 Failed Dependency" |
| 141 | + ] |
| 142 | +) |
| 143 | +def test_client_error_codes(code, expected_name, expected_label): |
| 144 | + """Test client error HTTP status codes""" |
| 145 | + assert code == getattr(HttpCode, expected_name) |
| 146 | + assert code.label() == expected_label |
| 147 | + assert str(code) == f"{code.value} {expected_label}" |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.parametrize( |
| 151 | + "code, expected_name, expected_label", |
| 152 | + [ |
| 153 | + (HttpCode.INTERNAL_SERVER_ERROR, "INTERNAL_SERVER_ERROR", "Internal Server Error"), |
| 154 | + (HttpCode.NOT_IMPLEMENTED, "NOT_IMPLEMENTED", "Not Implemented"), |
| 155 | + (HttpCode.BAD_GATEWAY, "BAD_GATEWAY", "Bad Gateway"), |
| 156 | + (HttpCode.SERVICE_UNAVAILABLE, "SERVICE_UNAVAILABLE", "Service Unavailable"), |
| 157 | + (HttpCode.GATEWAY_TIMEOUT, "GATEWAY_TIMEOUT", "Gateway Timeout"), |
| 158 | + (HttpCode.HTTP_VERSION_NOT_SUPPORTED, "HTTP_VERSION_NOT_SUPPORTED", "Http Version Not Supported"), |
| 159 | + (HttpCode.VARIANT_ALSO_NEGOTIATES, "VARIANT_ALSO_NEGOTIATES", "Variant Also Negotiates"), |
| 160 | + (HttpCode.INSUFFICIENT_STORAGE, "INSUFFICIENT_STORAGE", "Insufficient Storage"), |
| 161 | + (HttpCode.LOOP_DETECTED, "LOOP_DETECTED", "Loop Detected"), |
| 162 | + (HttpCode.NOT_EXTENDED, "NOT_EXTENDED", "Not Extended"), |
| 163 | + (HttpCode.NETWORK_AUTHENTICATION_REQUIRED, 'NETWORK_AUTHENTICATION_REQUIRED', 'Network Authentication Required'), |
| 164 | + ], |
| 165 | + ids=[ |
| 166 | + "500 Internal Server Error", |
| 167 | + "501 Not Implemented", |
| 168 | + "502 Bad Gateway", |
| 169 | + "503 Service Unavailable", |
| 170 | + "504 Gateway Timeout", |
| 171 | + "505 Http Version Not Supported", |
| 172 | + "506 Variant Also Negotiates", |
| 173 | + "507 Insufficient Storage", |
| 174 | + "508 Loop Detected", |
| 175 | + "510 Not Extended", |
| 176 | + '511 Network Authentication Required' |
| 177 | + ] |
| 178 | +) |
| 179 | +def test_server_error_codes(code, expected_name, expected_label): |
| 180 | + """Test server error HTTP status codes""" |
| 181 | + assert code == getattr(HttpCode, expected_name) |
| 182 | + assert code.label() == expected_label |
| 183 | + assert str(code) == f"{code.value} {expected_label}" |
0 commit comments