github.com/w3security/vervet/v5@v5.3.1-0.20230618081846-5bd9b5d799dc/versionware/validator_resolve_test.go (about) 1 package versionware 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 qt "github.com/frankban/quicktest" 10 "github.com/getkin/kin-openapi/openapi3" 11 ) 12 13 func TestResolveStability(t *testing.T) { 14 c := qt.New(t) 15 v2022_07_10_beta := parseOpenAPI(c, ` 16 openapi: 3.0.0 17 x-w3security-api-version: 2022-07-10~beta 18 info: 19 title: 'test' 20 paths: 21 /foo: 22 get: 23 operationId: getFoo 24 parameters: 25 - in: query 26 name: version 27 schema: 28 type: string 29 required: true 30 - in: query 31 name: color 32 schema: 33 type: string 34 required: true 35 responses: 36 '200': 37 content: 38 application/json: 39 schema: 40 type: object 41 `) 42 v2022_07_20_ga := parseOpenAPI(c, ` 43 openapi: 3.0.0 44 x-w3security-api-version: 2022-07-20~ga 45 info: 46 title: 'test' 47 paths: 48 /foo: 49 get: 50 operationId: getFoo 51 parameters: 52 - in: query 53 name: version 54 schema: 55 type: string 56 required: true 57 - in: query 58 name: flavor 59 schema: 60 type: string 61 required: true 62 responses: 63 '200': 64 content: 65 application/json: 66 schema: 67 type: object 68 `) 69 70 v, err := NewValidator(nil, v2022_07_10_beta, v2022_07_20_ga) 71 c.Assert(err, qt.IsNil) 72 h := v.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 73 _, err := w.Write([]byte("{}")) 74 c.Assert(err, qt.IsNil) 75 })) 76 77 tests := []struct { 78 desc string 79 url string 80 status int 81 }{{ 82 desc: "valid request, version resolves to 2022-07-10~beta", 83 url: "/foo?color=red&version=2022-07-19~beta", 84 status: 200, 85 }, { 86 desc: "invalid request, version resolves to 2022-07-10~beta", 87 url: "/foo?flavor=blue&version=2022-07-19~beta", 88 status: 400, 89 }, { 90 desc: "valid request, version resolves to 2022-07-20~ga", 91 url: "/foo?flavor=flav&version=2022-07-20", 92 status: 200, 93 }, { 94 desc: "valid request, version resolves to 2022-07-20~ga", 95 url: "/foo?flavor=flav&version=2022-07-21", 96 status: 200, 97 }, { 98 desc: "invalid request, version resolves to 2022-07-10~beta", 99 url: "/foo?flavor=flav&version=2022-07-21~beta", 100 status: 400, 101 }, { 102 desc: "valid request, version resolves to 2022-07-10~beta", 103 url: "/foo?color=octarine&version=2022-07-21~beta", 104 status: 200, 105 }, { 106 desc: "invalid request, version resolves to 2022-07-20~ga", 107 url: "/foo?color=red&version=2022-07-21~ga", 108 status: 400, 109 }, { 110 desc: "valid request, version resolves to 2022-07-10~beta", 111 url: "/foo?color=red&version=2022-07-20~beta", 112 status: 200, 113 }, { 114 desc: "valid request, version resolves to expected validator", 115 url: "/foo?flavor=flav&version=2022-07-21", 116 status: 200, 117 }, { 118 desc: "version does not resolve, no such version", 119 url: "/foo?version=2022-01-01", 120 status: 404, 121 }} 122 123 for i, test := range tests { 124 c.Run(fmt.Sprintf("test %d", i), func(c *qt.C) { 125 req := httptest.NewRequest("GET", test.url, nil) 126 w := httptest.NewRecorder() 127 h.ServeHTTP(w, req) 128 resp := w.Result() 129 c.Check(resp.StatusCode, qt.Equals, test.status, qt.Commentf("%s %s", test.desc, test.url)) 130 }) 131 } 132 } 133 134 func parseOpenAPI(c *qt.C, docstr string) *openapi3.T { 135 loader := openapi3.NewLoader() 136 doc, err := loader.LoadFromData([]byte(docstr)) 137 c.Assert(err, qt.IsNil) 138 return doc 139 }