|
| 1 | +package openapi3filter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "mime/multipart" |
| 8 | + "net/http" |
| 9 | + "net/textproto" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/getkin/kin-openapi/openapi3" |
| 16 | + "github.com/getkin/kin-openapi/openapi3filter" |
| 17 | + "github.com/getkin/kin-openapi/routers/gorillamux" |
| 18 | +) |
| 19 | + |
| 20 | +func TestValidateCsvFileUpload(t *testing.T) { |
| 21 | + const spec = ` |
| 22 | +openapi: 3.0.0 |
| 23 | +info: |
| 24 | + title: 'Validator' |
| 25 | + version: 0.0.1 |
| 26 | +paths: |
| 27 | + /test: |
| 28 | + post: |
| 29 | + requestBody: |
| 30 | + required: true |
| 31 | + content: |
| 32 | + multipart/form-data: |
| 33 | + schema: |
| 34 | + type: object |
| 35 | + required: |
| 36 | + - file |
| 37 | + properties: |
| 38 | + file: |
| 39 | + type: string |
| 40 | + format: string |
| 41 | + responses: |
| 42 | + '200': |
| 43 | + description: Created |
| 44 | +` |
| 45 | + |
| 46 | + loader := openapi3.NewLoader() |
| 47 | + doc, err := loader.LoadFromData([]byte(spec)) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + err = doc.Validate(loader.Context) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + router, err := gorillamux.NewRouter(doc) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + csvData string |
| 58 | + wantErr bool |
| 59 | + }{ |
| 60 | + { |
| 61 | + `foo,bar`, |
| 62 | + false, |
| 63 | + }, |
| 64 | + { |
| 65 | + `"foo","bar"`, |
| 66 | + false, |
| 67 | + }, |
| 68 | + { |
| 69 | + `foo,bar |
| 70 | +baz,qux`, |
| 71 | + false, |
| 72 | + }, |
| 73 | + { |
| 74 | + `foo,bar |
| 75 | +baz,qux,quux`, |
| 76 | + true, |
| 77 | + }, |
| 78 | + { |
| 79 | + `"""`, |
| 80 | + true, |
| 81 | + }, |
| 82 | + } |
| 83 | + for _, tt := range tests { |
| 84 | + body := &bytes.Buffer{} |
| 85 | + writer := multipart.NewWriter(body) |
| 86 | + |
| 87 | + { // Add file data |
| 88 | + h := make(textproto.MIMEHeader) |
| 89 | + h.Set("Content-Disposition", `form-data; name="file"; filename="hello.csv"`) |
| 90 | + h.Set("Content-Type", "text/csv") |
| 91 | + |
| 92 | + fw, err := writer.CreatePart(h) |
| 93 | + require.NoError(t, err) |
| 94 | + _, err = io.Copy(fw, strings.NewReader(tt.csvData)) |
| 95 | + |
| 96 | + require.NoError(t, err) |
| 97 | + } |
| 98 | + |
| 99 | + writer.Close() |
| 100 | + |
| 101 | + req, err := http.NewRequest(http.MethodPost, "/test", bytes.NewReader(body.Bytes())) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + req.Header.Set("Content-Type", writer.FormDataContentType()) |
| 105 | + |
| 106 | + route, pathParams, err := router.FindRoute(req) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + if err = openapi3filter.ValidateRequestBody( |
| 110 | + context.Background(), |
| 111 | + &openapi3filter.RequestValidationInput{ |
| 112 | + Request: req, |
| 113 | + PathParams: pathParams, |
| 114 | + Route: route, |
| 115 | + }, |
| 116 | + route.Operation.RequestBody.Value, |
| 117 | + ); err != nil { |
| 118 | + if !tt.wantErr { |
| 119 | + t.Errorf("got %v", err) |
| 120 | + } |
| 121 | + continue |
| 122 | + } |
| 123 | + if tt.wantErr { |
| 124 | + t.Errorf("want err") |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments