Skip to content

Commit

Permalink
Add the request header validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Oct 1, 2018
1 parent d44c366 commit f11b293
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (t *Analyzer) Analyze(req *http.Request) error {
switch param.In {
case "path":
err = t.validatePathParameter(pathParams, &param)
case "header":
err = t.validateHeaderParameter(req, &param)
case "body":
err = t.validateBodyParameter(req, &param)
case "query":
Expand Down Expand Up @@ -126,6 +128,15 @@ func (t *Analyzer) validateBodyParameter(req *http.Request, param *spec.Paramete
return nil
}

func (t *Analyzer) validateHeaderParameter(req *http.Request, param *spec.Parameter) error {
errs := validate.NewParamValidator(param, strfmt.Default).Validate(req.Header.Get(param.Name))
if errs != nil {
return errs.AsError()
}

return nil
}

func (t *Analyzer) validateQueryParameter(req *http.Request, param *spec.Parameter) error {
query := req.URL.Query()

Expand Down
32 changes: 32 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func Test_Analyzer_Analyze_with_path_parameters(t *testing.T) {
analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/42", nil)
req.Header.Set("userID", "some-id")
require.NoError(t, err)

err = analyzer.Analyze(req)
Expand All @@ -162,6 +163,7 @@ func Test_Analyzer_Analyze_with_invalid_path_parameters(t *testing.T) {
analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/not-a-number", nil)
req.Header.Set("userID", "42")
require.NoError(t, err)

err = analyzer.Analyze(req)
Expand Down Expand Up @@ -247,3 +249,33 @@ func Test_Analyzer_Analyze_with_missing_formData_field(t *testing.T) {
assert.EqualError(t, err, "validation failure list:\n"+
"additionalMetadata in formData is required")
}

func Test_Analyzer_Analyze_with_header(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/32", nil)
require.NoError(t, err)
req.Header.Set("userID", "42")

err = analyzer.Analyze(req)

assert.NoError(t, err)
}

func Test_Analyzer_Analyze_with_missing_header(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/32", nil)
require.NoError(t, err)

err = analyzer.Analyze(req)

assert.EqualError(t, err, "validation failure list:\n"+
"userID in header is required")
}
7 changes: 7 additions & 0 deletions dataset/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@
"application/json"
],
"parameters": [
{
"name": "userID",
"in": "header",
"description": "",
"required": true,
"type": "string"
},
{
"name": "petId",
"in": "path",
Expand Down

0 comments on commit f11b293

Please sign in to comment.