Skip to content

Commit

Permalink
If Content-Type is application/json;charset=UTF-8, RequestBody
Browse files Browse the repository at this point in the history
…is empty.

Signed-off-by: odg0318 <odg0318@gmail.com>
  • Loading branch information
odg0318 committed Apr 5, 2018
1 parent 094aa1a commit 6ac73d3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/authorization/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"mime"
"net/http"
"strings"

Expand Down Expand Up @@ -153,7 +154,12 @@ func sendBody(url string, header http.Header) bool {
}

// 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
Expand Down
60 changes: 60 additions & 0 deletions pkg/authorization/authz_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
r := httptest.NewRecorder()
m := NewResponseModifier(r)
Expand Down

0 comments on commit 6ac73d3

Please sign in to comment.