-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_issue639.lua
More file actions
149 lines (138 loc) · 5.27 KB
/
test_issue639.lua
File metadata and controls
149 lines (138 loc) · 5.27 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
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue639_test.go
-- Tests request body decode edge cases: empty objects, optional bodies,
-- additional properties, and nested object validation.
dofile("t/lib/test_bootstrap.lua")
local T = require("test_helper")
local cjson = require("cjson.safe")
local ov = require("resty.openapi_validator")
local spec = cjson.encode({
openapi = "3.0.0",
info = { title = "Sample API", version = "1.0.0" },
paths = {
["/items"] = {
post = {
requestBody = {
required = false,
content = {
["application/json"] = {
schema = {
type = "object",
properties = {
name = { type = "string" },
count = { type = "integer" },
metadata = {
type = "object",
properties = {
tags = {
type = "array",
items = { type = "string" },
},
nested = {
type = "object",
properties = {
level = { type = "integer" },
},
},
},
},
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
["/strict"] = {
post = {
requestBody = {
required = true,
content = {
["application/json"] = {
schema = {
type = "object",
required = { "id" },
properties = {
id = { type = "integer" },
label = { type = "string" },
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})
local v = ov.compile(spec)
assert(v, "compile failed")
T.describe("issue639: empty object with only optional properties (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = "{}",
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue639: no body when body is not required (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue639: object with all fields (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "count": 5}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue639: object with additional properties (valid - no restriction)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "extra_field": "hello", "another": 42}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue639: deeply nested valid object (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "metadata": {"tags": ["a", "b"], "nested": {"level": 3}}}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue639: missing required field in /strict (fail)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/strict",
body = '{"label": "test"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(not ok, "should fail - missing required field 'id'")
end)
T.describe("issue639: valid required field in /strict (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/strict",
body = '{"id": 1, "label": "test"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.done()