|
| 1 | +package openapi3filter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/getkin/kin-openapi/openapi3" |
| 13 | + "github.com/getkin/kin-openapi/routers/gorillamux" |
| 14 | +) |
| 15 | + |
| 16 | +func TestIssue201(t *testing.T) { |
| 17 | + loader := openapi3.NewLoader() |
| 18 | + ctx := loader.Context |
| 19 | + spec := ` |
| 20 | +openapi: '3' |
| 21 | +info: |
| 22 | + version: 1.0.0 |
| 23 | + title: Sample API |
| 24 | +paths: |
| 25 | + /_: |
| 26 | + get: |
| 27 | + description: '' |
| 28 | + responses: |
| 29 | + default: |
| 30 | + description: '' |
| 31 | + content: |
| 32 | + application/json: |
| 33 | + schema: |
| 34 | + type: object |
| 35 | + headers: |
| 36 | + X-Blip: |
| 37 | + description: '' |
| 38 | + required: true |
| 39 | + schema: |
| 40 | + pattern: '^blip$' |
| 41 | + x-blop: |
| 42 | + description: '' |
| 43 | + schema: |
| 44 | + pattern: '^blop$' |
| 45 | + X-Blap: |
| 46 | + description: '' |
| 47 | + required: true |
| 48 | + schema: |
| 49 | + pattern: '^blap$' |
| 50 | + X-Blup: |
| 51 | + description: '' |
| 52 | + required: true |
| 53 | + schema: |
| 54 | + pattern: '^blup$' |
| 55 | +`[1:] |
| 56 | + |
| 57 | + doc, err := loader.LoadFromData([]byte(spec)) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + err = doc.Validate(ctx) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + for name, testcase := range map[string]struct { |
| 64 | + headers map[string]string |
| 65 | + err string |
| 66 | + }{ |
| 67 | + |
| 68 | + "no error": { |
| 69 | + headers: map[string]string{ |
| 70 | + "X-Blip": "blip", |
| 71 | + "x-blop": "blop", |
| 72 | + "X-Blap": "blap", |
| 73 | + "X-Blup": "blup", |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + "missing non-required header": { |
| 78 | + headers: map[string]string{ |
| 79 | + "X-Blip": "blip", |
| 80 | + // "x-blop": "blop", |
| 81 | + "X-Blap": "blap", |
| 82 | + "X-Blup": "blup", |
| 83 | + }, |
| 84 | + }, |
| 85 | + |
| 86 | + "missing required header": { |
| 87 | + err: `response header "X-Blip" missing`, |
| 88 | + headers: map[string]string{ |
| 89 | + // "X-Blip": "blip", |
| 90 | + "x-blop": "blop", |
| 91 | + "X-Blap": "blap", |
| 92 | + "X-Blup": "blup", |
| 93 | + }, |
| 94 | + }, |
| 95 | + |
| 96 | + "invalid required header": { |
| 97 | + err: `response header "X-Blup" doesn't match the schema: string doesn't match the regular expression "^blup$"`, |
| 98 | + headers: map[string]string{ |
| 99 | + "X-Blip": "blip", |
| 100 | + "x-blop": "blop", |
| 101 | + "X-Blap": "blap", |
| 102 | + "X-Blup": "bluuuuuup", |
| 103 | + }, |
| 104 | + }, |
| 105 | + } { |
| 106 | + t.Run(name, func(t *testing.T) { |
| 107 | + router, err := gorillamux.NewRouter(doc) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + r, err := http.NewRequest(http.MethodGet, `/_`, nil) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + r.Header.Add(headerCT, "application/json") |
| 114 | + for k, v := range testcase.headers { |
| 115 | + r.Header.Add(k, v) |
| 116 | + } |
| 117 | + |
| 118 | + route, pathParams, err := router.FindRoute(r) |
| 119 | + require.NoError(t, err) |
| 120 | + |
| 121 | + err = ValidateResponse(context.Background(), &ResponseValidationInput{ |
| 122 | + RequestValidationInput: &RequestValidationInput{ |
| 123 | + Request: r, |
| 124 | + PathParams: pathParams, |
| 125 | + Route: route, |
| 126 | + }, |
| 127 | + Status: 200, |
| 128 | + Header: r.Header, |
| 129 | + Body: io.NopCloser(strings.NewReader(`{}`)), |
| 130 | + }) |
| 131 | + if e := testcase.err; e != "" { |
| 132 | + require.ErrorContains(t, err, e) |
| 133 | + } else { |
| 134 | + require.NoError(t, err) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments