Skip to content

Commit

Permalink
Add missing tests in Analyzer.validateFormDataParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Oct 1, 2018
1 parent 0917726 commit d44c366
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
5 changes: 3 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oaichecker
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -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{
Expand Down
52 changes: 52 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
}
4 changes: 2 additions & 2 deletions dataset/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
],
Expand Down

0 comments on commit d44c366

Please sign in to comment.