Skip to content

Commit

Permalink
debug api for enforcerd
Browse files Browse the repository at this point in the history
debug api for enforcerd
  • Loading branch information
primalmotion authored Mar 20, 2020
1 parent 6c9a00e commit 9b32dc1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 19 deletions.
3 changes: 3 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func EncodingFromHeaders(header http.Header) (read EncodingType, write EncodingT
if !supported {
return "", "", NewError("Unsupported Media Type", fmt.Sprintf("Cannot find any acceptable Content-Type media type in provided header: %s", v), "elemental", http.StatusUnsupportedMediaType)
}

read = EncodingType(ct)
}
}

Expand Down Expand Up @@ -249,6 +251,7 @@ func EncodingFromHeaders(header http.Header) (read EncodingType, write EncodingT
for t := range externalSupportedAcceptType {
if at == t {
agreed = true
write = EncodingType(at)
break L
}
}
Expand Down
14 changes: 12 additions & 2 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,16 @@ func TestEncodingFromHeader(t *testing.T) {

Convey("When I call EncodingFromHeaders", func() {

_, _, err := EncodingFromHeaders(h)
r, w, err := EncodingFromHeaders(h)

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then r and w should be correct", func() {
So(string(r), ShouldEqual, "application/aaaa")
So(string(w), ShouldEqual, "application/msgpack")
})
})
})

Expand All @@ -427,11 +432,16 @@ func TestEncodingFromHeader(t *testing.T) {

Convey("When I call EncodingFromHeaders", func() {

_, _, err := EncodingFromHeaders(h)
r, w, err := EncodingFromHeaders(h)

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then r and w should be correct", func() {
So(string(r), ShouldEqual, "application/msgpack")
So(string(w), ShouldEqual, "application/aaaa")
})
})
})
}
41 changes: 24 additions & 17 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques
components = append(components[:0], components[2:]...)
}

contentType, acceptType, err := EncodingFromHeaders(req.Header)
if err != nil {
return nil, err
}

switch len(components) {
case 1:
identity = manager.IdentityFromCategory(components[0])
Expand Down Expand Up @@ -142,27 +147,34 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques

case http.MethodPatch:
operation = OperationPatch
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
fmt.Println("------->", contentType)
if _, ok := externalSupportedContentType[string(contentType)]; !ok {
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
}
defer req.Body.Close() // nolint: errcheck
}
defer req.Body.Close() // nolint: errcheck

case http.MethodPost:
operation = OperationCreate
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
if _, ok := externalSupportedContentType[string(contentType)]; !ok {
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
}
defer req.Body.Close() // nolint: errcheck
}
defer req.Body.Close() // nolint: errcheck

case http.MethodPut:
operation = OperationUpdate
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
if _, ok := externalSupportedContentType[string(contentType)]; !ok {
data, err = ioutil.ReadAll(req.Body)
if err != nil {
return nil, NewError("Bad Request", fmt.Sprintf("Unable to read body of request: %s", err), "elemental", http.StatusBadRequest)
}
defer req.Body.Close() // nolint: errcheck
}
defer req.Body.Close() // nolint: errcheck
}

var page, pageSize, limit int
Expand Down Expand Up @@ -271,11 +283,6 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques
clientIP = req.RemoteAddr
}

contentType, acceptType, err := EncodingFromHeaders(req.Header)
if err != nil {
return nil, err
}

return &Request{
RequestID: uuid.Must(uuid.NewV4()).String(),
Namespace: req.Header.Get("X-Namespace"),
Expand Down
79 changes: 79 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,32 @@ func TestRequest_NewRequestFromHTTPRequest(t *testing.T) {
})
})

Convey("Given I have a patch http request on /lists using multipart/form-data", t, func() {

RegisterSupportedContentType("multipart/form-data")
defer func() { externalSupportedAcceptType = map[string]struct{}{} }()

buffer := bytes.NewBuffer([]byte(`{"name": "toto"}`))
req, _ := http.NewRequest(http.MethodPatch, "http://server/lists?lup1=A&lup2=true", buffer)
req.Header.Add("X-Namespace", "ns")
req.Header.Add("Authorization", "user pass")
req.Header.Add("Content-Type", "multipart/form-data; boundary=x")

Convey("When I create a new elemental Request from it", func() {

r, err := NewRequestFromHTTPRequest(req, Manager())

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then r should be correct", func() {
So(r.Operation, ShouldEqual, OperationPatch)
So(string(r.Data), ShouldEqual, "") // should not be decoded
})
})
})

Convey("Given I have a post http request on /lists", t, func() {

buffer := bytes.NewBuffer([]byte(`{"name": "toto"}`))
Expand Down Expand Up @@ -245,6 +271,33 @@ func TestRequest_NewRequestFromHTTPRequest(t *testing.T) {
})
})

Convey("Given I have a post http request on /lists using multipart/form-data", t, func() {

RegisterSupportedContentType("multipart/form-data")
defer func() { externalSupportedAcceptType = map[string]struct{}{} }()

buffer := bytes.NewBuffer([]byte(`{"name": "toto"}`))
req, _ := http.NewRequest(http.MethodPost, "http://server/lists?order=name&order=toto&rlcp1=A&rlcp2=true", buffer)
req.Header.Add("X-Namespace", "ns")
req.Header.Add("Authorization", "user pass")
req.Header.Add("Content-Type", "multipart/form-data; boundary=x")

Convey("When I create a new elemental Request from it", func() {

r, err := NewRequestFromHTTPRequest(req, Manager())

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then r should be correct", func() {
So(r.Operation, ShouldEqual, OperationCreate)

So(string(r.Data), ShouldEqual, ``)
})
})
})

Convey("Given I have a get http request on /lists/xx", t, func() {

req, _ := http.NewRequest(http.MethodGet, "http://server/lists/xx?lgp1=A&lgp2=true", nil)
Expand Down Expand Up @@ -304,6 +357,32 @@ func TestRequest_NewRequestFromHTTPRequest(t *testing.T) {
})
})

Convey("Given I have a put http request on /lists/xx using multipart/form-data", t, func() {

RegisterSupportedContentType("multipart/form-data")
defer func() { externalSupportedAcceptType = map[string]struct{}{} }()

buffer := bytes.NewBuffer([]byte(`{"name": "toto"}`))
req, _ := http.NewRequest(http.MethodPut, "http://server/lists/xx?lup1=A&lup2=true", buffer)
req.Header.Add("X-Namespace", "ns")
req.Header.Add("Authorization", "user pass")
req.Header.Add("Content-Type", "multipart/form-data")

Convey("When I create a new elemental Request from it", func() {

r, err := NewRequestFromHTTPRequest(req, Manager())

Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Then r should be correct", func() {
So(r.Operation, ShouldEqual, OperationUpdate)
So(string(r.Data), ShouldEqual, ``)
})
})
})

Convey("Given I have a delete http request on /lists/xx", t, func() {

req, _ := http.NewRequest(http.MethodDelete, "http://server/lists/xx?ldp1=A&ldp2=true", nil)
Expand Down

0 comments on commit 9b32dc1

Please sign in to comment.