|
// ContentType returns the Content-Type of the request without the charset. |
|
func (c *Context) ContentType() (ct string) { |
|
ct = c.req.Header.Get(HeaderContentType) |
|
if index := strings.IndexAny(ct, ";"); index > 0 { |
|
ct = strings.TrimSpace(ct[:index]) |
|
} |
|
return |
|
} |
|
// Bind implements the interface Binder, which looks up the registered binder |
|
// by the request header "Content-Type" and calls it to bind the value dst |
|
// to req. |
|
func (mb *MuxBinder) Bind(dst interface{}, req *http.Request) error { |
|
ct := req.Header.Get("Content-Type") |
|
if index := strings.IndexAny(ct, ";"); index > 0 { |
|
ct = strings.TrimSpace(ct[:index]) |
|
} |
|
|
|
if ct == "" { |
|
return ErrMissingContentType |
|
} |
上面两处代码在获取 Content-Type 时用的是 index > 0,如果测试用例是 ; charset=utf-8,那么切割出的 Content-Type 是 ; charset=utf-8。此时我们期望结果的应该是 空字符串。
故:此处使用 index >= 0 更为妥当
ship/context.go
Lines 410 to 417 in 405a71d
ship/binder.go
Lines 72 to 83 in 405a71d
上面两处代码在获取
Content-Type时用的是index > 0,如果测试用例是; charset=utf-8,那么切割出的Content-Type是; charset=utf-8。此时我们期望结果的应该是空字符串。故:此处使用
index >= 0更为妥当