-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhttp.status_test.go
131 lines (120 loc) · 4.61 KB
/
http.status_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package quick
import "testing"
// TestQuick_HttpStatus verifies that the HTTP statuses and HTTP methods are registered correctly.
// This test only prints the values into the log for visual inspection.
// The will test func TestQuick_HttpStatus(t *testing.T)
//
// $ go test -v -run ^func TestQuick_HttpStatus(t *testing.T)
func TestQuick_HttpStatus(t *testing.T) {
// Log of informative status codes
t.Log(StatusContinue)
t.Log(StatusSwitchingProtocols)
t.Log(StatusProcessing)
t.Log(StatusEarlyHints)
// Log of success status codes
t.Log(StatusOK)
t.Log(StatusCreated)
t.Log(StatusAccepted)
t.Log(StatusNonAuthoritativeInfo)
t.Log(StatusNoContent)
t.Log(StatusResetContent)
t.Log(StatusPartialContent)
// Log of supported HTTP methods
t.Log(MethodGet)
t.Log(MethodHead)
t.Log(MethodPost)
t.Log(MethodPut)
t.Log(MethodPatch)
t.Log(MethodDelete)
t.Log(MethodConnect)
t.Log(MethodTrace)
}
// TestStatusText checks if the StatusText function returns the correct description
// for each HTTP status code.
// The will test func TestStatusText(t *testing.T)
//
// $ go test -v -run ^func TestStatusText(t *testing.T)
func TestStatusText(t *testing.T) {
// Set of tests with HTTP status codes and their expected descriptions.
tests := []struct {
code int // HTTP status code
expected string // Expected description
}{
// Tests for 1xx status codes (Informative)
{StatusContinue, "Continue"},
{StatusSwitchingProtocols, "Switching Protocols"},
{StatusProcessing, "Processing"},
{StatusEarlyHints, "Early Hints"},
// Tests for 2xx status codes (Success)
{StatusOK, "OK"},
{StatusCreated, "Created"},
{StatusAccepted, "Accepted"},
{StatusNonAuthoritativeInfo, "Non-Authoritative Information"},
{StatusNoContent, "No Content"},
{StatusResetContent, "Reset Content"},
{StatusPartialContent, "Partial Content"},
{StatusMultiStatus, "Multi-Status"},
{StatusAlreadyReported, "Already Reported"},
{StatusIMUsed, "IM Used"},
// Tests for 3xx status codes (Redirection)
{StatusMultipleChoices, "Multiple Choices"},
{StatusMovedPermanently, "Moved Permanently"},
{StatusFound, "Found"},
{StatusSeeOther, "See Other"},
{StatusNotModified, "Not Modified"},
{StatusUseProxy, "Use Proxy"},
{StatusTemporaryRedirect, "Temporary Redirect"},
{StatusPermanentRedirect, "Permanent Redirect"},
// Testing for 4xx status codes (Customer Error)
{StatusBadRequest, "Bad Request"},
{StatusUnauthorized, "Unauthorized"},
{StatusPaymentRequired, "Payment Required"},
{StatusForbidden, "Forbidden"},
{StatusNotFound, "Not Found"},
{StatusMethodNotAllowed, "Method Not Allowed"},
{StatusNotAcceptable, "Not Acceptable"},
{StatusProxyAuthRequired, "Proxy Authentication Required"},
{StatusRequestTimeout, "Request Timeout"},
{StatusConflict, "Conflict"},
{StatusGone, "Gone"},
{StatusLengthRequired, "Length Required"},
{StatusPreconditionFailed, "Precondition Failed"},
{StatusRequestEntityTooLarge, "Request Entity Too Large"},
{StatusRequestURITooLong, "Request URI Too Long"},
{StatusUnsupportedMediaType, "Unsupported Media Type"},
{StatusRequestedRangeNotSatisfiable, "Requested Range Not Satisfiable"},
{StatusExpectationFailed, "Expectation Failed"},
{StatusTeapot, "I'm a teapot"},
{StatusMisdirectedRequest, "Misdirected Request"},
{StatusUnprocessableEntity, "Unprocessable Entity"},
{StatusLocked, "Locked"},
{StatusFailedDependency, "Failed Dependency"},
{StatusTooEarly, "Too Early"},
{StatusUpgradeRequired, "Upgrade Required"},
{StatusPreconditionRequired, "Precondition Required"},
{StatusTooManyRequests, "Too Many Requests"},
{StatusRequestHeaderFieldsTooLarge, "Request Header Fields Too Large"},
// Tests for 5xx status codes (Server Error)
{StatusUnavailableForLegalReasons, "Unavailable For Legal Reasons"},
{StatusInternalServerError, "Internal Server Error"},
{StatusNotImplemented, "Not Implemented"},
{StatusBadGateway, "Bad Gateway"},
{StatusServiceUnavailable, "Service Unavailable"},
{StatusGatewayTimeout, "Gateway Timeout"},
{StatusHTTPVersionNotSupported, "HTTP Version Not Supported"},
{StatusVariantAlsoNegotiates, "Variant Also Negotiates"},
{StatusInsufficientStorage, "Insufficient Storage"},
{StatusLoopDetected, "Loop Detected"},
{StatusNotExtended, "Not Extended"},
{StatusNetworkAuthenticationRequired, "Network Authentication Required"},
{999, ""}, // Test for an unknown code
}
for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
result := StatusText(test.code)
if result != test.expected {
t.Errorf("For code %d, expected '%s', but got '%s'", test.code, test.expected, result)
}
})
}
}