Skip to content

Commit bac245b

Browse files
pbdeuchlerhoisie
authored andcommitted
Added NotAuthorized function, which gives a 403 response, and the respective tests. Also added additional logging for route/response code testing
Refactored to include Unauthorized and Forbidden methods, instead of NotAuthorized Squash previous two commits, added Forbidden and Unauthorized methods
1 parent 0b25551 commit bac245b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

web.go

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ func (ctx *Context) NotFound(message string) {
6565
ctx.ResponseWriter.Write([]byte(message))
6666
}
6767

68+
//Unauthorized writes a 401 HTTP response
69+
func (ctx *Context) Unauthorized() {
70+
ctx.ResponseWriter.WriteHeader(401)
71+
}
72+
73+
//Forbidden writes a 403 HTTP response
74+
func (ctx *Context) Forbidden() {
75+
ctx.ResponseWriter.WriteHeader(403)
76+
}
77+
6878
// ContentType sets the Content-Type header for an HTTP response.
6979
// For example, ctx.ContentType("json") sets the content-type to "application/json"
7080
// If the supplied value contains a slash (/) it is set as the Content-Type

web_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ func init() {
145145

146146
Get("/error/notfound/(.*)", func(ctx *Context, message string) { ctx.NotFound(message) })
147147

148+
Get("/error/unauthorized", func(ctx *Context) { ctx.Unauthorized() })
149+
Post("/error/unauthorized", func(ctx *Context) { ctx.Unauthorized() })
150+
151+
Get("/error/forbidden", func(ctx *Context) { ctx.Forbidden() })
152+
Post("/error/forbidden", func(ctx *Context) { ctx.Forbidden() })
153+
148154
Post("/posterror/code/(.*)/(.*)", func(ctx *Context, code string, message string) string {
149155
n, _ := strconv.Atoi(code)
150156
ctx.Abort(n, message)
@@ -224,6 +230,10 @@ var tests = []Test{
224230
//long url
225231
{"GET", "/echo/" + strings.Repeat("0123456789", 100), nil, "", 200, strings.Repeat("0123456789", 100)},
226232
{"GET", "/writetest", nil, "", 200, "hello"},
233+
{"GET", "/error/unauthorized", nil, "", 401, ""},
234+
{"POST", "/error/unauthorized", nil, "", 401, ""},
235+
{"GET", "/error/forbidden", nil, "", 403, ""},
236+
{"POST", "/error/forbidden", nil, "", 403, ""},
227237
{"GET", "/error/notfound/notfound", nil, "", 404, "notfound"},
228238
{"GET", "/doesnotexist", nil, "", 404, "Page not found"},
229239
{"POST", "/doesnotexist", nil, "", 404, "Page not found"},
@@ -278,10 +288,10 @@ func TestRouting(t *testing.T) {
278288
resp := getTestResponse(test.method, test.path, test.body, test.headers, nil)
279289

280290
if resp.statusCode != test.expectedStatus {
281-
t.Fatalf("expected status %d got %d", test.expectedStatus, resp.statusCode)
291+
t.Fatalf("%v(%v) expected status %d got %d", test.method, test.path, test.expectedStatus, resp.statusCode)
282292
}
283293
if resp.body != test.expectedBody {
284-
t.Fatalf("expected %q got %q", test.expectedBody, resp.body)
294+
t.Fatalf("%v(%v) expected %q got %q", test.method, test.path, test.expectedBody, resp.body)
285295
}
286296
if cl, ok := resp.headers["Content-Length"]; ok {
287297
clExp, _ := strconv.Atoi(cl[0])

0 commit comments

Comments
 (0)