| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package responses |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/pb33f/libopenapi" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"github.com/pb33f/libopenapi-validator/config" |
| 15 |
) |
| 16 |
|
| 17 |
func TestValidateResponseHeaders(t *testing.T) { |
| 18 |
spec := `openapi: "3.0.0" |
| 19 |
info: |
| 20 |
title: Healthcheck |
| 21 |
version: '0.1.0' |
| 22 |
paths: |
| 23 |
/health: |
| 24 |
get: |
| 25 |
responses: |
| 26 |
'200': |
| 27 |
headers: |
| 28 |
chicken-nuggets: |
| 29 |
description: chicken nuggets response |
| 30 |
required: true |
| 31 |
schema: |
| 32 |
type: integer |
| 33 |
description: pet response` |
| 34 |
|
| 35 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 36 |
|
| 37 |
m, _ := doc.BuildV3Model() |
| 38 |
|
| 39 |
// build a request |
| 40 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil) |
| 41 |
|
| 42 |
// simulate a request/response |
| 43 |
res := httptest.NewRecorder() |
| 44 |
handler := func(w http.ResponseWriter, r *http.Request) { |
| 45 |
w.Header().Set("Chicken-Cakes", "I should fail") |
| 46 |
w.WriteHeader(http.StatusOK) |
| 47 |
_, _ = w.Write(nil) |
| 48 |
} |
| 49 |
|
| 50 |
// fire the request |
| 51 |
handler(res, request) |
| 52 |
|
| 53 |
// record response |
| 54 |
response := res.Result() |
| 55 |
|
| 56 |
headers := m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers |
| 57 |
|
| 58 |
// validate! |
| 59 |
valid, errors := ValidateResponseHeaders(request, response, headers, "/health", "200") |
| 60 |
|
| 61 |
assert.False(t, valid) |
| 62 |
assert.Len(t, errors, 1) |
| 63 |
assert.Equal(t, errors[0].Message, "Missing required header") |
| 64 |
assert.Equal(t, errors[0].Reason, "Required header 'chicken-nuggets' was not found in response") |
| 65 |
|
| 66 |
res = httptest.NewRecorder() |
| 67 |
handler = func(w http.ResponseWriter, r *http.Request) { |
| 68 |
w.Header().Set("Chicken-Nuggets", "I should fail") |
| 69 |
w.WriteHeader(http.StatusOK) |
| 70 |
_, _ = w.Write(nil) |
| 71 |
} |
| 72 |
|
| 73 |
// fire the request |
| 74 |
handler(res, request) |
| 75 |
|
| 76 |
response = res.Result() |
| 77 |
|
| 78 |
headers = m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers |
| 79 |
|
| 80 |
// validate! |
| 81 |
valid, errors = ValidateResponseHeaders(request, response, headers, "/health", "200") |
| 82 |
|
| 83 |
assert.False(t, valid) |
| 84 |
assert.Len(t, errors, 1) |
| 85 |
assert.Equal(t, errors[0].Message, "header 'chicken-nuggets' failed to validate") |
| 86 |
assert.Equal(t, errors[0].Reason, "response header 'chicken-nuggets' is defined as an integer, however it failed to pass a schema validation") |
| 87 |
} |
| 88 |
|
| 89 |
func TestValidateResponseHeaders_Valid(t *testing.T) { |
| 90 |
spec := `openapi: "3.0.0" |
| 91 |
info: |
| 92 |
title: Healthcheck |
| 93 |
version: '0.1.0' |
| 94 |
paths: |
| 95 |
/health: |
| 96 |
get: |
| 97 |
responses: |
| 98 |
'200': |
| 99 |
headers: |
| 100 |
chicken-nuggets: |
| 101 |
description: chicken nuggets response |
| 102 |
required: false |
| 103 |
schema: |
| 104 |
type: integer |
| 105 |
description: pet response` |
| 106 |
|
| 107 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 108 |
|
| 109 |
m, _ := doc.BuildV3Model() |
| 110 |
|
| 111 |
// build a request |
| 112 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil) |
| 113 |
|
| 114 |
// simulate a request/response |
| 115 |
res := httptest.NewRecorder() |
| 116 |
handler := func(w http.ResponseWriter, r *http.Request) { |
| 117 |
w.Header().Set("Chicken-Cakes", "I should fail") |
| 118 |
w.WriteHeader(http.StatusOK) |
| 119 |
_, _ = w.Write(nil) |
| 120 |
} |
| 121 |
|
| 122 |
// fire the request |
| 123 |
handler(res, request) |
| 124 |
|
| 125 |
response := res.Result() |
| 126 |
|
| 127 |
headers := m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers |
| 128 |
|
| 129 |
// validate! |
| 130 |
valid, errors := ValidateResponseHeaders(request, response, headers, "/health", "200") |
| 131 |
|
| 132 |
assert.True(t, valid) |
| 133 |
assert.Len(t, errors, 0) |
| 134 |
} |
| 135 |
|
| 136 |
func TestValidateResponseHeaders_StrictMode(t *testing.T) { |
| 137 |
spec := `openapi: "3.0.0" |
| 138 |
info: |
| 139 |
title: Healthcheck |
| 140 |
version: '0.1.0' |
| 141 |
paths: |
| 142 |
/health: |
| 143 |
get: |
| 144 |
responses: |
| 145 |
'200': |
| 146 |
headers: |
| 147 |
x-request-id: |
| 148 |
description: request ID |
| 149 |
required: false |
| 150 |
schema: |
| 151 |
type: string |
| 152 |
description: healthy response` |
| 153 |
|
| 154 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 155 |
m, _ := doc.BuildV3Model() |
| 156 |
|
| 157 |
// build a request |
| 158 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil) |
| 159 |
|
| 160 |
// simulate a response with an undeclared header |
| 161 |
res := httptest.NewRecorder() |
| 162 |
handler := func(w http.ResponseWriter, r *http.Request) { |
| 163 |
w.Header().Set("X-Request-Id", "abc-123") |
| 164 |
w.Header().Set("X-Undeclared-Header", "should fail in strict mode") |
| 165 |
w.WriteHeader(http.StatusOK) |
| 166 |
} |
| 167 |
|
| 168 |
handler(res, request) |
| 169 |
response := res.Result() |
| 170 |
|
| 171 |
headers := m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers |
| 172 |
|
| 173 |
// validate with strict mode - should find undeclared header |
| 174 |
valid, errors := ValidateResponseHeaders(request, response, headers, "/health", "200", config.WithStrictMode()) |
| 175 |
|
| 176 |
assert.False(t, valid) |
| 177 |
assert.Len(t, errors, 1) |
| 178 |
assert.Contains(t, errors[0].Message, "X-Undeclared-Header") |
| 179 |
assert.Contains(t, errors[0].Message, "not declared") |
| 180 |
} |
| 181 |
|
| 182 |
func TestValidateResponseHeaders_StrictMode_NoUndeclared(t *testing.T) { |
| 183 |
spec := `openapi: "3.0.0" |
| 184 |
info: |
| 185 |
title: Healthcheck |
| 186 |
version: '0.1.0' |
| 187 |
paths: |
| 188 |
/health: |
| 189 |
get: |
| 190 |
responses: |
| 191 |
'200': |
| 192 |
headers: |
| 193 |
x-request-id: |
| 194 |
description: request ID |
| 195 |
required: false |
| 196 |
schema: |
| 197 |
type: string |
| 198 |
description: healthy response` |
| 199 |
|
| 200 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 201 |
m, _ := doc.BuildV3Model() |
| 202 |
|
| 203 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil) |
| 204 |
|
| 205 |
// response with only declared headers (x-request-id is declared, Content-Type is default-ignored) |
| 206 |
res := httptest.NewRecorder() |
| 207 |
handler := func(w http.ResponseWriter, r *http.Request) { |
| 208 |
w.Header().Set("X-Request-Id", "abc-123") |
| 209 |
w.WriteHeader(http.StatusOK) |
| 210 |
} |
| 211 |
|
| 212 |
handler(res, request) |
| 213 |
response := res.Result() |
| 214 |
|
| 215 |
headers := m.Model.Paths.PathItems.GetOrZero("/health").Get.Responses.Codes.GetOrZero("200").Headers |
| 216 |
|
| 217 |
// validate with strict mode - should pass (no undeclared headers) |
| 218 |
valid, errors := ValidateResponseHeaders(request, response, headers, "/health", "200", config.WithStrictMode()) |
| 219 |
|
| 220 |
assert.True(t, valid) |
| 221 |
assert.Len(t, errors, 0) |
| 222 |
} |