mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #36778 from odg0318/master
Using authorization plugin, I changed Content-Type check routine.
This commit is contained in:
commit
ff6a103067
2 changed files with 67 additions and 1 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -153,7 +154,12 @@ func sendBody(url string, header http.Header) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// body is sent only for text or json messages
|
// body is sent only for text or json messages
|
||||||
return header.Get("Content-Type") == "application/json"
|
contentType, _, err := mime.ParseMediaType(header.Get("Content-Type"))
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentType == "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
// headers returns flatten version of the http headers excluding authorization
|
// headers returns flatten version of the http headers excluding authorization
|
||||||
|
|
|
@ -172,6 +172,66 @@ func TestDrainBody(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendBody(t *testing.T) {
|
||||||
|
var (
|
||||||
|
url = "nothing.com"
|
||||||
|
testcases = []struct {
|
||||||
|
contentType string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
contentType: "application/json",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "Application/json",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "application/JSON",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "APPLICATION/JSON",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "application/json;charset=utf-8",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "application/json; charset=UTF8",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "application/json;charset=UTF8",
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "text/html",
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contentType: "",
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, testcase := range testcases {
|
||||||
|
header := http.Header{}
|
||||||
|
header.Set("Content-Type", testcase.contentType)
|
||||||
|
|
||||||
|
if b := sendBody(url, header); b != testcase.expected {
|
||||||
|
t.Fatalf("Unexpected Content-Type; Expected: %t, Actual: %t", testcase.expected, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResponseModifierOverride(t *testing.T) {
|
func TestResponseModifierOverride(t *testing.T) {
|
||||||
r := httptest.NewRecorder()
|
r := httptest.NewRecorder()
|
||||||
m := NewResponseModifier(r)
|
m := NewResponseModifier(r)
|
||||||
|
|
Loading…
Add table
Reference in a new issue