Skip to content

Commit 6f78904

Browse files
authored
Merge pull request hoisie#201 from hoisie/bad-request
Add a helper method for returning HTTP 400
2 parents 037f438 + c1d5d89 commit 6f78904

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

web.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ func (ctx *Context) Redirect(status int, url_ string) {
5555
ctx.ResponseWriter.Write([]byte("Redirecting to: " + url_))
5656
}
5757

58+
//BadRequest writes a 400 HTTP response
59+
func (ctx *Context) BadRequest() {
60+
ctx.ResponseWriter.WriteHeader(400)
61+
}
62+
5863
// Notmodified writes a 304 HTTP response
5964
func (ctx *Context) NotModified() {
6065
ctx.ResponseWriter.WriteHeader(304)
6166
}
6267

63-
// NotFound writes a 404 HTTP response
64-
func (ctx *Context) NotFound(message string) {
65-
ctx.ResponseWriter.WriteHeader(404)
66-
ctx.ResponseWriter.Write([]byte(message))
67-
}
68-
6968
//Unauthorized writes a 401 HTTP response
7069
func (ctx *Context) Unauthorized() {
7170
ctx.ResponseWriter.WriteHeader(401)
@@ -76,6 +75,12 @@ func (ctx *Context) Forbidden() {
7675
ctx.ResponseWriter.WriteHeader(403)
7776
}
7877

78+
// NotFound writes a 404 HTTP response
79+
func (ctx *Context) NotFound(message string) {
80+
ctx.ResponseWriter.WriteHeader(404)
81+
ctx.ResponseWriter.Write([]byte(message))
82+
}
83+
7984
// ContentType sets the Content-Type header for an HTTP response.
8085
// For example, ctx.ContentType("json") sets the content-type to "application/json"
8186
// If the supplied value contains a slash (/) it is set as the Content-Type

web_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ func init() {
145145

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

148+
Get("/error/badrequest", func(ctx *Context) { ctx.BadRequest() })
149+
Post("/error/badrequest", func(ctx *Context) { ctx.BadRequest() })
150+
148151
Get("/error/unauthorized", func(ctx *Context) { ctx.Unauthorized() })
149152
Post("/error/unauthorized", func(ctx *Context) { ctx.Unauthorized() })
150153

@@ -230,6 +233,8 @@ var tests = []Test{
230233
//long url
231234
{"GET", "/echo/" + strings.Repeat("0123456789", 100), nil, "", 200, strings.Repeat("0123456789", 100)},
232235
{"GET", "/writetest", nil, "", 200, "hello"},
236+
{"GET", "/error/badrequest", nil, "", 400, ""},
237+
{"POST", "/error/badrequest", nil, "", 400, ""},
233238
{"GET", "/error/unauthorized", nil, "", 401, ""},
234239
{"POST", "/error/unauthorized", nil, "", 401, ""},
235240
{"GET", "/error/forbidden", nil, "", 403, ""},

0 commit comments

Comments
 (0)