diff options
| author | Caleb Maclennan <caleb@alerque.com> | 2026-08-31 11:03:24 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-31 11:03:24 +0300 |
| commit | 5afe174eeffc7f3e3b9ef80b5871237620c68415 (patch) | |
| tree | 85c8bb14a445823dc7a683a53394901523f5f16b /test/headerstest.lua | |
| parent | b836466de5a78e9b4f29ca6c85a2bdecd7c69da9 (diff) | |
| parent | 3dd5fb0468977c6626fe4a0ea7b95222f14da09f (diff) | |
| download | luasocket-5afe174eeffc7f3e3b9ef80b5871237620c68415.tar.gz luasocket-5afe174eeffc7f3e3b9ef80b5871237620c68415.tar.bz2 luasocket-5afe174eeffc7f3e3b9ef80b5871237620c68415.zip | |
Merge pull request #470 from lunarmodules/header-canon
feat(headers): dynamically create cononicalized headers
Diffstat (limited to 'test/headerstest.lua')
| -rw-r--r-- | test/headerstest.lua | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/test/headerstest.lua b/test/headerstest.lua new file mode 100644 index 0000000..cbf638f --- /dev/null +++ b/test/headerstest.lua | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | local socket = require("socket") | ||
| 2 | local headers = require("socket.headers") | ||
| 3 | |||
| 4 | -- the original hardcoded table this file used to contain, kept here so the | ||
| 5 | -- refactor to dynamic generation + a small anomaly table can be checked for | ||
| 6 | -- regressions. | ||
| 7 | local canonic = { | ||
| 8 | ["accept"] = "Accept", | ||
| 9 | ["accept-charset"] = "Accept-Charset", | ||
| 10 | ["accept-encoding"] = "Accept-Encoding", | ||
| 11 | ["accept-language"] = "Accept-Language", | ||
| 12 | ["accept-ranges"] = "Accept-Ranges", | ||
| 13 | ["action"] = "Action", | ||
| 14 | ["alternate-recipient"] = "Alternate-Recipient", | ||
| 15 | ["age"] = "Age", | ||
| 16 | ["allow"] = "Allow", | ||
| 17 | ["arrival-date"] = "Arrival-Date", | ||
| 18 | ["authorization"] = "Authorization", | ||
| 19 | ["bcc"] = "Bcc", | ||
| 20 | ["cache-control"] = "Cache-Control", | ||
| 21 | ["cc"] = "Cc", | ||
| 22 | ["comments"] = "Comments", | ||
| 23 | ["connection"] = "Connection", | ||
| 24 | ["content-description"] = "Content-Description", | ||
| 25 | ["content-disposition"] = "Content-Disposition", | ||
| 26 | ["content-encoding"] = "Content-Encoding", | ||
| 27 | ["content-id"] = "Content-ID", | ||
| 28 | ["content-language"] = "Content-Language", | ||
| 29 | ["content-length"] = "Content-Length", | ||
| 30 | ["content-location"] = "Content-Location", | ||
| 31 | ["content-md5"] = "Content-MD5", | ||
| 32 | ["content-range"] = "Content-Range", | ||
| 33 | ["content-transfer-encoding"] = "Content-Transfer-Encoding", | ||
| 34 | ["content-type"] = "Content-Type", | ||
| 35 | ["cookie"] = "Cookie", | ||
| 36 | ["date"] = "Date", | ||
| 37 | ["diagnostic-code"] = "Diagnostic-Code", | ||
| 38 | ["dsn-gateway"] = "DSN-Gateway", | ||
| 39 | ["etag"] = "ETag", | ||
| 40 | ["expect"] = "Expect", | ||
| 41 | ["expires"] = "Expires", | ||
| 42 | ["final-log-id"] = "Final-Log-ID", | ||
| 43 | ["final-recipient"] = "Final-Recipient", | ||
| 44 | ["from"] = "From", | ||
| 45 | ["host"] = "Host", | ||
| 46 | ["if-match"] = "If-Match", | ||
| 47 | ["if-modified-since"] = "If-Modified-Since", | ||
| 48 | ["if-none-match"] = "If-None-Match", | ||
| 49 | ["if-range"] = "If-Range", | ||
| 50 | ["if-unmodified-since"] = "If-Unmodified-Since", | ||
| 51 | ["in-reply-to"] = "In-Reply-To", | ||
| 52 | ["keywords"] = "Keywords", | ||
| 53 | ["last-attempt-date"] = "Last-Attempt-Date", | ||
| 54 | ["last-modified"] = "Last-Modified", | ||
| 55 | ["location"] = "Location", | ||
| 56 | ["max-forwards"] = "Max-Forwards", | ||
| 57 | ["message-id"] = "Message-ID", | ||
| 58 | ["mime-version"] = "MIME-Version", | ||
| 59 | ["original-envelope-id"] = "Original-Envelope-ID", | ||
| 60 | ["original-recipient"] = "Original-Recipient", | ||
| 61 | ["pragma"] = "Pragma", | ||
| 62 | ["proxy-authenticate"] = "Proxy-Authenticate", | ||
| 63 | ["proxy-authorization"] = "Proxy-Authorization", | ||
| 64 | ["range"] = "Range", | ||
| 65 | ["received"] = "Received", | ||
| 66 | ["received-from-mta"] = "Received-From-MTA", | ||
| 67 | ["references"] = "References", | ||
| 68 | ["referer"] = "Referer", | ||
| 69 | ["remote-mta"] = "Remote-MTA", | ||
| 70 | ["reply-to"] = "Reply-To", | ||
| 71 | ["reporting-mta"] = "Reporting-MTA", | ||
| 72 | ["resent-bcc"] = "Resent-Bcc", | ||
| 73 | ["resent-cc"] = "Resent-Cc", | ||
| 74 | ["resent-date"] = "Resent-Date", | ||
| 75 | ["resent-from"] = "Resent-From", | ||
| 76 | ["resent-message-id"] = "Resent-Message-ID", | ||
| 77 | ["resent-reply-to"] = "Resent-Reply-To", | ||
| 78 | ["resent-sender"] = "Resent-Sender", | ||
| 79 | ["resent-to"] = "Resent-To", | ||
| 80 | ["retry-after"] = "Retry-After", | ||
| 81 | ["return-path"] = "Return-Path", | ||
| 82 | ["sender"] = "Sender", | ||
| 83 | ["server"] = "Server", | ||
| 84 | ["smtp-remote-recipient"] = "SMTP-Remote-Recipient", | ||
| 85 | ["status"] = "Status", | ||
| 86 | ["subject"] = "Subject", | ||
| 87 | ["te"] = "TE", | ||
| 88 | ["to"] = "To", | ||
| 89 | ["trailer"] = "Trailer", | ||
| 90 | ["transfer-encoding"] = "Transfer-Encoding", | ||
| 91 | ["upgrade"] = "Upgrade", | ||
| 92 | ["user-agent"] = "User-Agent", | ||
| 93 | ["vary"] = "Vary", | ||
| 94 | ["via"] = "Via", | ||
| 95 | ["warning"] = "Warning", | ||
| 96 | ["will-retry-until"] = "Will-Retry-Until", | ||
| 97 | ["www-authenticate"] = "WWW-Authenticate", | ||
| 98 | ["x-mailer"] = "X-Mailer", | ||
| 99 | } | ||
| 100 | |||
| 101 | local check_canonic = function(lower, expected) | ||
| 102 | local got = headers.canonic[lower] | ||
| 103 | if got ~= expected then | ||
| 104 | print("canonic[" .. lower .. "] = " .. tostring(got) .. | ||
| 105 | ", expected " .. expected) | ||
| 106 | os.exit(1) | ||
| 107 | end | ||
| 108 | end | ||
| 109 | |||
| 110 | print("testing all known header fields resolve to their canonical form") | ||
| 111 | for lower, expected in pairs(canonic) do | ||
| 112 | check_canonic(lower, expected) | ||
| 113 | end | ||
| 114 | |||
| 115 | print("testing mixed/upper-case input normalizes correctly") | ||
| 116 | check_canonic("Content-Type", "Content-Type") | ||
| 117 | check_canonic("CONTENT-TYPE", "Content-Type") | ||
| 118 | check_canonic("ETAG", "ETag") | ||
| 119 | check_canonic("Www-Authenticate", "WWW-Authenticate") | ||
| 120 | |||
| 121 | print("testing novel unregistered headers are auto-titlecased") | ||
| 122 | check_canonic("x-request-id", "X-Request-Id") | ||
| 123 | check_canonic("x-custom-header-name", "X-Custom-Header-Name") | ||
| 124 | check_canonic("single", "Single") | ||
| 125 | |||
| 126 | print("testing setcanonic() registers a custom canonical capitalization") | ||
| 127 | headers.setcanonic("X-Request-ID") | ||
| 128 | check_canonic("x-request-id", "X-Request-ID") | ||
| 129 | check_canonic("X-REQUEST-ID", "X-Request-ID") | ||
| 130 | |||
| 131 | print("the library passed all tests") | ||
