-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_format_validation.lua
More file actions
215 lines (173 loc) · 6.62 KB
/
test_format_validation.lua
File metadata and controls
215 lines (173 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env resty
--- Conformance tests for format keyword validation.
-- Covers uuid, date, date-time, uri, email, ipv4, hostname formats.
-- Each format has positive and negative cases.
dofile("t/lib/test_bootstrap.lua")
local T = require("test_helper")
local cjson = require("cjson.safe")
local ov = require("resty.openapi_validator")
local function make_format_spec(format_name)
return cjson.encode({
openapi = "3.0.0",
info = { title = "FormatTest", version = "0.1" },
paths = {
["/test"] = {
post = {
requestBody = {
required = true,
content = {
["application/json"] = {
schema = {
type = "object",
required = { "value" },
properties = {
value = {
type = "string",
format = format_name,
},
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})
end
local function validate_format(format_name, value)
local spec = make_format_spec(format_name)
local v, err = ov.compile(spec)
assert(v, "compile failed for format '" .. format_name .. "': " .. tostring(err))
return v:validate_request({
method = "POST",
path = "/test",
body = cjson.encode({ value = value }),
content_type = "application/json",
})
end
-- uuid
T.describe("format uuid: valid lowercase", function()
local ok, err = validate_format("uuid", "550e8400-e29b-41d4-a716-446655440000")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format uuid: valid uppercase", function()
local ok, err = validate_format("uuid", "550E8400-E29B-41D4-A716-446655440000")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format uuid: invalid - missing segment", function()
local ok = validate_format("uuid", "550e8400-e29b-41d4-a716")
T.ok(not ok, "should fail")
end)
T.describe("format uuid: invalid - no hyphens", function()
local ok = validate_format("uuid", "550e8400e29b41d4a716446655440000")
T.ok(not ok, "should fail")
end)
T.describe("format uuid: invalid - random string", function()
local ok = validate_format("uuid", "not-a-uuid")
T.ok(not ok, "should fail")
end)
-- date
T.describe("format date: valid", function()
local ok, err = validate_format("date", "2024-01-15")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format date: valid - end of month", function()
local ok, err = validate_format("date", "2024-12-31")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format date: invalid - wrong separator", function()
local ok = validate_format("date", "2024/01/15")
T.ok(not ok, "should fail")
end)
T.describe("format date: invalid - month 13", function()
local ok = validate_format("date", "2024-13-01")
T.ok(not ok, "should fail")
end)
T.describe("format date: invalid - day 32", function()
local ok = validate_format("date", "2024-01-32")
T.ok(not ok, "should fail")
end)
T.describe("format date: invalid - incomplete", function()
local ok = validate_format("date", "2024-01")
T.ok(not ok, "should fail")
end)
-- date-time
T.describe("format date-time: valid UTC", function()
local ok, err = validate_format("date-time", "2024-01-15T10:30:00Z")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format date-time: valid with offset", function()
local ok, err = validate_format("date-time", "2024-01-15T10:30:00+08:00")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format date-time: valid with fractional seconds", function()
local ok, err = validate_format("date-time", "2024-01-15T10:30:00.123Z")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format date-time: invalid - no timezone", function()
local ok = validate_format("date-time", "2024-01-15T10:30:00")
T.ok(not ok, "should fail")
end)
T.describe("format date-time: invalid - date only", function()
local ok = validate_format("date-time", "2024-01-15")
T.ok(not ok, "should fail")
end)
T.describe("format date-time: invalid - random string", function()
local ok = validate_format("date-time", "not-a-datetime")
T.ok(not ok, "should fail")
end)
-- uri
T.describe("format uri: valid https", function()
local ok, err = validate_format("uri", "https://example.com/path")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format uri: valid mailto", function()
local ok, err = validate_format("uri", "mailto:user@example.com")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format uri: valid urn", function()
local ok, err = validate_format("uri", "urn:isbn:0451450523")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format uri: invalid - no scheme", function()
local ok = validate_format("uri", "example.com/path")
T.ok(not ok, "should fail")
end)
T.describe("format uri: invalid - just a path", function()
local ok = validate_format("uri", "/just/a/path")
T.ok(not ok, "should fail")
end)
-- email (already supported, but add reject test)
T.describe("format email: valid", function()
local ok, err = validate_format("email", "user@example.com")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format email: invalid - no @", function()
local ok = validate_format("email", "userexample.com")
T.ok(not ok, "should fail")
end)
T.describe("format email: invalid - no domain", function()
local ok = validate_format("email", "user@")
T.ok(not ok, "should fail")
end)
-- ipv4 (already supported)
T.describe("format ipv4: valid", function()
local ok, err = validate_format("ipv4", "192.168.1.1")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format ipv4: invalid - out of range", function()
local ok = validate_format("ipv4", "256.1.1.1")
T.ok(not ok, "should fail")
end)
-- hostname (already supported)
T.describe("format hostname: valid", function()
local ok, err = validate_format("hostname", "example.com")
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("format hostname: invalid - has space", function()
local ok = validate_format("hostname", "exam ple.com")
T.ok(not ok, "should fail")
end)
T.done()