Skip to content

Commit

Permalink
♻️ let go's http.DetectContentType set text and html content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
aisk committed Dec 4, 2018
1 parent b08b3f9 commit 66a2807
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 30 deletions.
13 changes: 1 addition & 12 deletions response.go
Expand Up @@ -82,20 +82,9 @@ func (response *Response) setImplicitContentType() {
return
}

switch v := response.Body.(type) {
switch response.Body.(type) {
case []byte:
if bodyMatcher.Match(v) {
response.Header.Set("Content-Type", mime.TypeByExtension(".html"))
} else {
response.Header.Set("Content-Type", mime.TypeByExtension(".text"))
}
case string:
if bodyMatcher.MatchString(v) {
response.Header.Set("Content-Type", mime.TypeByExtension(".html"))
} else {
response.Header.Set("Content-Type", mime.TypeByExtension(".text"))
}
// case map[string]interface{}, map[string]string:
default:
response.Header.Set("Content-Type", mime.TypeByExtension(".json"))
}
Expand Down
18 changes: 0 additions & 18 deletions response_test.go
Expand Up @@ -14,24 +14,6 @@ func TestNewResponse(t *testing.T) {
t.Fail()
}

w = httptest.NewRecorder()
response = createResponse(w)
response.Body = "plaintext"
response.setImplicit()
if response.Header.Get("Content-Type") != "text/plain; charset=utf-8" {
t.Fail()
}

w = httptest.NewRecorder()
response = createResponse(w)
response.Body = `
<!doctype html
`
response.setImplicit()
if response.Header.Get("Content-Type") != "text/html; charset=utf-8" {
t.Fail()
}

w = httptest.NewRecorder()
response = createResponse(w)
response.Body = map[string]string{"foo": "bar"}
Expand Down

0 comments on commit 66a2807

Please sign in to comment.