From d44c366fc90047a6de923b75eb286308752e7649 Mon Sep 17 00:00:00 2001 From: Peltoche Date: Mon, 1 Oct 2018 18:50:40 +0200 Subject: [PATCH] Add missing tests in Analyzer.validateFormDataParameter --- analyzer.go | 5 +++-- analyzer_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++ dataset/petstore.json | 4 ++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/analyzer.go b/analyzer.go index 6b4a195..5fa90bc 100644 --- a/analyzer.go +++ b/analyzer.go @@ -3,6 +3,7 @@ package oaichecker import ( "encoding/json" "errors" + "fmt" "net/http" "strconv" "strings" @@ -159,8 +160,8 @@ func (t *Analyzer) validateFormDataParameter(req *http.Request, param *spec.Para if param.Type == "file" { data, header, err := req.FormFile(param.Name) - if err != nil { - return err + if err != nil && param.ParamProps.Required { + return fmt.Errorf("validation failure list:\n%s in formData is required", param.Name) } res = runtime.File{ diff --git a/analyzer_test.go b/analyzer_test.go index dc0f10d..dec8cf7 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -178,6 +178,7 @@ func Test_Analyzer_Analyze_with_formData_file(t *testing.T) { var buf bytes.Buffer mp := multipart.NewWriter(&buf) + mp.WriteField("additionalMetadata", "foobar") fileWriter, err := mp.CreateFormFile("file", "file") require.NoError(t, err) _, err = fileWriter.Write([]byte("some-data")) @@ -195,3 +196,54 @@ func Test_Analyzer_Analyze_with_formData_file(t *testing.T) { assert.NoError(t, err) } + +func Test_Analyzer_Analyze_with_missing_formData_file(t *testing.T) { + specs, err := NewSpecsFromFile("./dataset/petstore.json") + require.NoError(t, err) + + analyzer := NewAnalyzer(specs) + + var buf bytes.Buffer + mp := multipart.NewWriter(&buf) + mp.WriteField("additionalMetadata", "foobar") + + err = mp.Close() + require.NoError(t, err) + + req, err := http.NewRequest("POST", "/pet/32/uploadImage", &buf) + require.NoError(t, err) + + req.Header.Set("Content-Type", mp.FormDataContentType()) + + err = analyzer.Analyze(req) + + assert.EqualError(t, err, "validation failure list:\n"+ + "file in formData is required") +} + +func Test_Analyzer_Analyze_with_missing_formData_field(t *testing.T) { + specs, err := NewSpecsFromFile("./dataset/petstore.json") + require.NoError(t, err) + + analyzer := NewAnalyzer(specs) + + var buf bytes.Buffer + mp := multipart.NewWriter(&buf) + fileWriter, err := mp.CreateFormFile("file", "file") + require.NoError(t, err) + _, err = fileWriter.Write([]byte("some-data")) + require.NoError(t, err) + + err = mp.Close() + require.NoError(t, err) + + req, err := http.NewRequest("POST", "/pet/32/uploadImage", &buf) + require.NoError(t, err) + + req.Header.Set("Content-Type", mp.FormDataContentType()) + + err = analyzer.Analyze(req) + + assert.EqualError(t, err, "validation failure list:\n"+ + "additionalMetadata in formData is required") +} diff --git a/dataset/petstore.json b/dataset/petstore.json index 937eb51..54de8e1 100644 --- a/dataset/petstore.json +++ b/dataset/petstore.json @@ -401,14 +401,14 @@ "name": "additionalMetadata", "in": "formData", "description": "Additional data to pass to server", - "required": false, + "required": true, "type": "string" }, { "name": "file", "in": "formData", "description": "file to upload", - "required": false, + "required": true, "type": "file" } ],