Skip to content

Commit

Permalink
Merge pull request #3 from Fenny/master
Browse files Browse the repository at this point in the history
Add StatusMessage
  • Loading branch information
Fenny authored May 20, 2020
2 parents 1cba44d + 65509fd commit 889adf2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 27 deletions.
71 changes: 71 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 30 additions & 27 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
Expand Down Expand Up @@ -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)
}

0 comments on commit 889adf2

Please sign in to comment.