Skip to content

Commit

Permalink
Merge 4a8eb19 into a378b70
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed Jan 28, 2020
2 parents a378b70 + 4a8eb19 commit 1d09df5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 12 additions & 2 deletions http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ func writeJSON(jsonableResponse interface{}, w http.ResponseWriter, statusCode i

// BasicDecodeRequest does not expect parameters
func BasicDecodeRequest(ctx context.Context, req *http.Request) (interface{}, error) {
return DecodeRequest(ctx, req, map[string]string{}, map[string]string{})
return DecodeRequestWithHeaders(ctx, req, map[string]string{}, map[string]string{}, nil)
}

// DecodeRequest gets the HTTP parameters and body content
func DecodeRequest(_ context.Context, req *http.Request, pathParams map[string]string, queryParams map[string]string) (interface{}, error) {
func DecodeRequest(ctx context.Context, req *http.Request, pathParams map[string]string, queryParams map[string]string) (interface{}, error) {
return DecodeRequestWithHeaders(ctx, req, pathParams, queryParams, nil)
}

// DecodeRequestWithHeaders gets the HTTP parameters, headers and body content
func DecodeRequestWithHeaders(_ context.Context, req *http.Request, pathParams map[string]string, queryParams map[string]string, headers []string) (interface{}, error) {
var request = map[string]string{}

// Fetch and validate path parameter such as realm, userID, ...
Expand Down Expand Up @@ -108,6 +113,11 @@ func DecodeRequest(_ context.Context, req *http.Request, pathParams map[string]s
}
}

if headers != nil {
for _, headerKey := range headers {
request[headerKey] = req.Header.Get(headerKey)
}
}
return request, nil
}

Expand Down
29 changes: 27 additions & 2 deletions http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func checkInvalidPathParameter(t *testing.T, url string) {
}

func genericTestDecodeRequest(ctx context.Context, tls *tls.ConnectionState, xFwdProto *string, rawQuery string) (map[string]string, error) {
return genericTestDecodeRequestWithHeader(ctx, tls, xFwdProto, rawQuery, nil)
}

func genericTestDecodeRequestWithHeader(ctx context.Context, tls *tls.ConnectionState, xFwdProto *string, rawQuery string, headers map[string]string) (map[string]string, error) {
input := "the body"
var req http.Request
var url url.URL
Expand All @@ -201,7 +205,17 @@ func genericTestDecodeRequest(ctx context.Context, tls *tls.ConnectionState, xFw
"queryParam2": "^[a-zA-Z0-9-]+$",
}

r, err := DecodeRequest(ctx, &req, pathParams, queryParams)
var r interface{}
var err error

if headers != nil {
for key, value := range headers {
req.Header.Add(key, value)
}
r, err = DecodeRequestWithHeaders(ctx, &req, pathParams, queryParams, []string{"Authorization"})
} else {
r, err = DecodeRequest(ctx, &req, pathParams, queryParams)
}

if err != nil {
return nil, err
Expand Down Expand Up @@ -253,13 +267,24 @@ func TestDecodeRequestQueryParams(t *testing.T) {
assert.Equal(t, value, request["queryParam2"])
}

func TestDecodeEventsRequestInvalidQueryParams(t *testing.T) {
func TestDecodeRequestInvalidQueryParams(t *testing.T) {
value := "valueX!"
_, err := genericTestDecodeRequest(context.Background(), nil, nil, "queryParam2="+value)

assert.NotNil(t, err)
}

func TestDecodeRequestWithHeaders(t *testing.T) {
var auth = "Basic ABC="
var headers = map[string]string{"Content-Type": "text/plain", "Authorization": auth}
request, err := genericTestDecodeRequestWithHeader(context.Background(), nil, nil, "", headers)

assert.Nil(t, err)
assert.NotContains(t, request, "Content-Type")
assert.Contains(t, request, "Authorization")
assert.Equal(t, auth, request["Authorization"])
}

func TestEncodeReplyNilResponse(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down

0 comments on commit 1d09df5

Please sign in to comment.