diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-09-23 21:02:26 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-09-23 21:02:26 -0700 |
commit | 9d08139f4363d3503398002bc82cb3746e3438cf (patch) | |
tree | b09c1efb14445624e4a2771bf8cbcf3a9828ecf9 /http/request/context.go | |
parent | 844680e57328034c20a2d2b84bd315b55fee9e23 (diff) |
Improve request package and add more unit tests
Diffstat (limited to 'http/request/context.go')
-rw-r--r-- | http/request/context.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/http/request/context.go b/http/request/context.go index b77365d..51ee46a 100644 --- a/http/request/context.go +++ b/http/request/context.go @@ -111,7 +111,12 @@ func ClientIP(r *http.Request) string { func getContextStringValue(r *http.Request, key ContextKey) string { if v := r.Context().Value(key); v != nil { - return v.(string) + value, valid := v.(string) + if !valid { + return "" + } + + return value } return "" @@ -119,7 +124,12 @@ func getContextStringValue(r *http.Request, key ContextKey) string { func getContextBoolValue(r *http.Request, key ContextKey) bool { if v := r.Context().Value(key); v != nil { - return v.(bool) + value, valid := v.(bool) + if !valid { + return false + } + + return value } return false @@ -127,7 +137,12 @@ func getContextBoolValue(r *http.Request, key ContextKey) bool { func getContextInt64Value(r *http.Request, key ContextKey) int64 { if v := r.Context().Value(key); v != nil { - return v.(int64) + value, valid := v.(int64) + if !valid { + return 0 + } + + return value } return 0 |