-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_issue707.lua
More file actions
143 lines (129 loc) · 4.43 KB
/
test_issue707.lua
File metadata and controls
143 lines (129 loc) · 4.43 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
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue707_test.go
-- Tests path parameter edge cases: type coercion, multiple params,
-- and min/max constraints.
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/{itemId}"] = {
get = {
parameters = {
{ name = "itemId", ["in"] = "path", required = true,
schema = { type = "string" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/orders/{orderId}"] = {
get = {
parameters = {
{ name = "orderId", ["in"] = "path", required = true,
schema = { type = "integer" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/bounded/{val}"] = {
get = {
parameters = {
{ name = "val", ["in"] = "path", required = true,
schema = { type = "integer", minimum = 1, maximum = 100 } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/multi/{region}/{id}"] = {
get = {
parameters = {
{ name = "region", ["in"] = "path", required = true,
schema = { type = "string" } },
{ name = "id", ["in"] = "path", required = true,
schema = { type = "integer" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})
local v = ov.compile(spec)
assert(v, "compile failed")
-- String path param accepts anything
T.describe("issue707: string path param with numeric-looking value (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/items/42",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue707: string path param with alpha value (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/items/hello-world",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
-- Integer path param rejects non-integer
T.describe("issue707: integer path param with valid integer (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/orders/999",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue707: integer path param with non-integer (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/orders/not-a-number",
})
T.ok(not ok, "should fail - 'not-a-number' is not an integer")
end)
-- Bounded integer path param
T.describe("issue707: bounded path param within range (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/50",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue707: bounded path param at minimum (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/1",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue707: bounded path param below minimum (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/0",
})
T.ok(not ok, "should fail - 0 is below minimum 1")
end)
T.describe("issue707: bounded path param above maximum (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/101",
})
T.ok(not ok, "should fail - 101 is above maximum 100")
end)
-- Multiple path params
T.describe("issue707: multiple path params both valid (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/multi/us-east/42",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue707: multiple path params, integer param invalid (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/multi/eu-west/abc",
})
T.ok(not ok, "should fail - 'abc' is not a valid integer for id param")
end)
T.done()