Skip to content

Commit

Permalink
[CLOUDTRUST-2108] Retrieve values from HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 authored Feb 13, 2020
1 parent 80a2f3a commit 790d30f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 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
2 changes: 1 addition & 1 deletion http/rights.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cloudtrust/common-service/security"
)

// MakeRigtsHandler makes a HTTP handler that returns information about the rights of the user.
// MakeRightsHandler makes a HTTP handler that returns information about the rights of the user.
func MakeRightsHandler(authorizationManager security.AuthorizationManager) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
EncodeReply(r.Context(), w, authorizationManager.GetRightsOfCurrentUser(r.Context()))
Expand Down

0 comments on commit 790d30f

Please sign in to comment.