|
| 1 | +#!/usr/bin/env resty |
| 2 | +--- Conformance test ported from lua-resty-openapi-validate: body_31.t |
| 3 | +-- Validates request body for OpenAPI 3.1 features: type arrays (nullable), const. |
| 4 | +dofile("t/lib/test_bootstrap.lua") |
| 5 | + |
| 6 | +local T = require("test_helper") |
| 7 | +local cjson = require("cjson.safe") |
| 8 | +local ov = require("resty.openapi_validator") |
| 9 | + |
| 10 | +local function read_file(path) |
| 11 | + local f = io.open(path, "r") |
| 12 | + assert(f, "cannot open " .. path) |
| 13 | + local data = f:read("*a") |
| 14 | + f:close() |
| 15 | + return data |
| 16 | +end |
| 17 | + |
| 18 | +local spec_str = read_file("t/specs/body_31.json") |
| 19 | +local validator, compile_err = ov.compile(spec_str, { strict = false }) |
| 20 | +assert(validator, "compile failed: " .. tostring(compile_err)) |
| 21 | + |
| 22 | +-- TEST 1: valid body (basic 3.1 compatibility) |
| 23 | +T.describe("body_31: valid request body", function() |
| 24 | + local ok, err = validator:validate_request({ |
| 25 | + method = "POST", |
| 26 | + path = "/user", |
| 27 | + body = cjson.encode({ |
| 28 | + username = "alphabeta", |
| 29 | + email = "alphabeta@gamma.zeta", |
| 30 | + }), |
| 31 | + content_type = "application/json", |
| 32 | + }) |
| 33 | + T.ok(ok, "valid body passes: " .. tostring(err)) |
| 34 | +end) |
| 35 | + |
| 36 | +-- TEST 2: nullable field with null value (3.1 type array ["string", "null"]) |
| 37 | +T.describe("body_31: nullable email with null", function() |
| 38 | + local ok, err = validator:validate_request({ |
| 39 | + method = "POST", |
| 40 | + path = "/user", |
| 41 | + body = cjson.encode({ |
| 42 | + username = "alphabeta", |
| 43 | + email = cjson.null, |
| 44 | + }), |
| 45 | + content_type = "application/json", |
| 46 | + }) |
| 47 | + T.ok(ok, "nullable email with null passes: " .. tostring(err)) |
| 48 | +end) |
| 49 | + |
| 50 | +-- TEST 3: missing required field 'username' |
| 51 | +T.describe("body_31: missing required username", function() |
| 52 | + local ok, err = validator:validate_request({ |
| 53 | + method = "POST", |
| 54 | + path = "/user", |
| 55 | + body = cjson.encode({ |
| 56 | + email = "alphabeta@gamma.zeta", |
| 57 | + }), |
| 58 | + content_type = "application/json", |
| 59 | + }) |
| 60 | + T.ok(not ok, "missing username fails") |
| 61 | + T.like(err, "username", "error mentions username") |
| 62 | +end) |
| 63 | + |
| 64 | +-- TEST 4: correct const value |
| 65 | +T.describe("body_31: correct const value", function() |
| 66 | + local ok, err = validator:validate_request({ |
| 67 | + method = "POST", |
| 68 | + path = "/user", |
| 69 | + body = cjson.encode({ |
| 70 | + username = "alphabeta", |
| 71 | + role = "admin", |
| 72 | + }), |
| 73 | + content_type = "application/json", |
| 74 | + }) |
| 75 | + T.ok(ok, "correct const value passes: " .. tostring(err)) |
| 76 | +end) |
| 77 | + |
| 78 | +-- TEST 5: wrong const value |
| 79 | +T.describe("body_31: wrong const value", function() |
| 80 | + local ok, err = validator:validate_request({ |
| 81 | + method = "POST", |
| 82 | + path = "/user", |
| 83 | + body = cjson.encode({ |
| 84 | + username = "alphabeta", |
| 85 | + role = "user", |
| 86 | + }), |
| 87 | + content_type = "application/json", |
| 88 | + }) |
| 89 | + T.ok(not ok, "wrong const value fails") |
| 90 | + T.like(err, "const", "error mentions const") |
| 91 | +end) |
| 92 | + |
| 93 | +T.done() |
0 commit comments