From 65509fd36479fb180cbe76fd4516778a51253c72 Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Wed, 20 May 2020 11:12:03 +0200 Subject: [PATCH] Add StatusMessage --- utils.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils_test.go | 57 +++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 27 deletions(-) diff --git a/utils.go b/utils.go index 42b9dd4..82b81e9 100644 --- a/utils.go +++ b/utils.go @@ -223,6 +223,77 @@ func GetArgument(arg string) bool { return false } +// StatusMessage ... +func StatusMessage(status int) string { + return statusMessage[status] +} + +// HTTP status codes were copied from net/http. +var statusMessage = map[int]string{ + 100: "Continue", + 101: "Switching Protocols", + 102: "Processing", + 103: "Early Hints", + 200: "OK", + 201: "Created", + 202: "Accepted", + 203: "Non-Authoritative Information", + 204: "No Content", + 205: "Reset Content", + 206: "Partial Content", + 207: "Multi-Status", + 208: "Already Reported", + 226: "IM Used", + 300: "Multiple Choices", + 301: "Moved Permanently", + 302: "Found", + 303: "See Other", + 304: "Not Modified", + 305: "Use Proxy", + 306: "Switch Proxy", + 307: "Temporary Redirect", + 308: "Permanent Redirect", + 400: "Bad Request", + 401: "Unauthorized", + 402: "Payment Required", + 403: "Forbidden", + 404: "Not Found", + 405: "Method Not Allowed", + 406: "Not Acceptable", + 407: "Proxy Authentication Required", + 408: "Request Timeout", + 409: "Conflict", + 410: "Gone", + 411: "Length Required", + 412: "Precondition Failed", + 413: "Request Entity Too Large", + 414: "Request URI Too Long", + 415: "Unsupported Media Type", + 416: "Requested Range Not Satisfiable", + 417: "Expectation Failed", + 418: "I'm a teapot", + 421: "Misdirected Request", + 422: "Unprocessable Entity", + 423: "Locked", + 424: "Failed Dependency", + 426: "Upgrade Required", + 428: "Precondition Required", + 429: "Too Many Requests", + 431: "Request Header Fields Too Large", + 451: "Unavailable For Legal Reasons", + 500: "Internal Server Error", + 501: "Not Implemented", + 502: "Bad Gateway", + 503: "Service Unavailable", + 504: "Gateway Timeout", + 505: "HTTP Version Not Supported", + 506: "Variant Also Negotiates", + 507: "Insufficient Storage", + 508: "Loop Detected", + 510: "Not Extended", + 511: "Network Authentication Required", +} + // MIME types were copied from https://github.com/nginx/nginx/blob/master/conf/mime.types var mimeExtensions = map[string]string{ "html": "text/html", diff --git a/utils_test.go b/utils_test.go index 119d3dd..69fa7b8 100644 --- a/utils_test.go +++ b/utils_test.go @@ -88,24 +88,6 @@ func Test_Utils_Trim(t *testing.T) { AssertEqual(t, "test", res) } -func Test_Utils_GetMIME(t *testing.T) { - t.Parallel() - res := GetMIME(".json") - AssertEqual(t, "application/json", res) - - res = GetMIME(".xml") - AssertEqual(t, "application/xml", res) - - res = GetMIME("xml") - AssertEqual(t, "application/xml", res) - - res = GetMIME("unknown") - AssertEqual(t, MIMEOctetStream, res) - // empty case - res = GetMIME("") - AssertEqual(t, "", res) -} - // func Test_Utils_getArgument(t *testing.T) { // // TODO // } @@ -152,17 +134,38 @@ func Test_Utils_GetBytes(t *testing.T) { AssertEqual(t, []byte("Hello, World!"), res) } -func Test_Utils_extensionMIME(t *testing.T) { +func Test_Utils_GetMIME(t *testing.T) { + t.Parallel() + res := GetMIME(".json") + AssertEqual(t, "application/json", res) + + res = GetMIME(".xml") + AssertEqual(t, "application/xml", res) + + res = GetMIME("xml") + AssertEqual(t, "application/xml", res) + + res = GetMIME("unknown") + AssertEqual(t, MIMEOctetStream, res) + // empty case + res = GetMIME("") + AssertEqual(t, "", res) +} + +func Test_Utils_StatusMessage(t *testing.T) { t.Parallel() - res := GetMIME(".html") - AssertEqual(t, "text/html", res) + res := StatusMessage(204) + AssertEqual(t, "No Content", res) - res = GetMIME("html") - AssertEqual(t, "text/html", res) + res = StatusMessage(404) + AssertEqual(t, "Not Found", res) - res = GetMIME(".msp") - AssertEqual(t, "application/octet-stream", res) + res = StatusMessage(426) + AssertEqual(t, "Upgrade Required", res) - res = GetMIME("msp") - AssertEqual(t, "application/octet-stream", res) + res = StatusMessage(511) + AssertEqual(t, "Network Authentication Required", res) + + res = StatusMessage(1337) + AssertEqual(t, "", res) }