-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spec: add specs for PolicyConfigValidator
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
local cjson = require('cjson') | ||
local policy_config_validator = require('apicast.policy_config_validator') | ||
|
||
describe('policy_config_validator', function() | ||
-- We use the echo policy schema as an example for the tests. | ||
local test_config_schema = cjson.decode([[ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"status": { | ||
"description": "HTTP status code to be returned", | ||
"type": "integer" | ||
}, | ||
"exit": { | ||
"description": "Exit mode", | ||
"type": "string", | ||
"oneOf": [{ | ||
"enum": ["request"], | ||
"description": "Interrupts the processing of the request." | ||
}, | ||
{ | ||
"enum": ["set"], | ||
"description": "Only skips the rewrite phase." | ||
} | ||
] | ||
} | ||
} | ||
} | ||
]]) | ||
|
||
it('returns true with a config that conforms with the schema', function() | ||
local valid_config = { status = 200, exit = "request" } | ||
|
||
local is_valid, err = policy_config_validator.validate_config( | ||
valid_config, test_config_schema) | ||
|
||
assert.is_true(is_valid) | ||
assert.is_nil(err) | ||
end) | ||
|
||
it('returns false with a config that does not conform with the schema', function() | ||
local invalid_config = { status = "not_an_integer" } | ||
|
||
local is_valid, err = policy_config_validator.validate_config( | ||
invalid_config, test_config_schema) | ||
|
||
assert.is_false(is_valid) | ||
assert.is_not_nil(err) | ||
end) | ||
|
||
it('returns true when the schema is empty', function() | ||
assert.is_true(policy_config_validator.validate_config({ a_param = 1 }, {})) | ||
end) | ||
|
||
it('returns true when the schema is nil', function() | ||
assert.is_true(policy_config_validator.validate_config({ a_param = 1 }, nil)) | ||
end) | ||
end) |