-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_issue201.lua
More file actions
88 lines (80 loc) · 2.77 KB
/
test_issue201.lua
File metadata and controls
88 lines (80 loc) · 2.77 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
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue201_test.go
-- Tests duplicate path templates with different methods and overlapping path segments.
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 = {
["/users/{id}"] = {
get = {
parameters = {
{ name = "id", ["in"] = "path", required = true,
schema = { type = "string" } },
},
responses = { ["200"] = { description = "OK" } },
},
post = {
parameters = {
{ name = "id", ["in"] = "path", required = true,
schema = { type = "string" } },
},
requestBody = {
required = true,
content = {
["application/json"] = {
schema = {
type = "object",
required = { "name" },
properties = {
name = { type = "string" },
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})
local v = ov.compile(spec)
assert(v, "compile failed")
T.describe("issue201: GET /users/123 (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/users/123",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue201: POST /users/123 with valid body (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/users/123",
body = '{"name": "alice"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.describe("issue201: POST /users/123 missing required body field (fail)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/users/123",
body = '{}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(not ok, "should fail - missing required field 'name'")
end)
T.describe("issue201: GET /users/abc string id (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/users/abc",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
T.done()