more robust command response content-type checking (#7412)

Этот коммит содержится в:
Chris
2017-09-11 10:09:24 -05:00
коммит произвёл GitHub
родитель 402491b7e5
Коммит 674a606bd0
2 изменённых файлов: 3 добавлений и 1 удалений

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"encoding/json"
"io"
"io/ioutil"
"strings"
)
const (
@@ -33,7 +34,7 @@ func (o *CommandResponse) ToJson() string {
}
func CommandResponseFromHTTPBody(contentType string, body io.Reader) *CommandResponse {
if contentType == "application/json" {
if strings.TrimSpace(strings.Split(contentType, ";")[0]) == "application/json" {
return CommandResponseFromJson(body)
}
if b, err := ioutil.ReadAll(body); err == nil {

Просмотреть файл

@@ -27,6 +27,7 @@ func TestCommandResponseFromHTTPBody(t *testing.T) {
{"", "foo", "foo"},
{"text/plain", "foo", "foo"},
{"application/json", `{"text": "foo"}`, "foo"},
{"application/json; charset=utf-8", `{"text": "foo"}`, "foo"},
} {
response := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body))
if response.Text != test.ExpectedText {