Commit 8ada5777 authored by Siebers, Michael's avatar Siebers, Michael
Browse files

add JSON schema for state

update api tests
parent 730c4d68
......@@ -429,8 +429,9 @@ status of the server.
| Property | Type | Description |
| --- | --- | --- |
| =status= | string | The status of the server. Currently, always =|"running"|=. |
| =version= | string | The version number of the WebAPI server. |
| =version= | string | The [semantic version number](https://semver.org) of the WebAPI. |
Response follows this [JSON schema](</doc/schema/response-state-schema.json>).
#### Example Call
......
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "dare2del/response/state",
"type": "object",
"title": "state response",
"description": "The reply body when querying the server state.",
"properties": {
"status": {
"type": "string"
},
"version": {
"type": "string",
"pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
}
},
"required": [
"status",
"version"
]
}
......@@ -7,46 +7,116 @@
},
"item": [
{
"name": "Server is running as expected",
"name": "Server is up",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
"pm.test(pm.variables.get(\"test_prefix\") + \"Status code is no error\", function () {",
" pm.response.to.not.be.serverError;",
"});",
""
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{SERVER}}",
"protocol": "http",
"host": [
"{{SERVER}}"
]
}
},
"response": []
},
{
"name": "Documentation available",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"pm.test(pm.variables.get(\"test_prefix\") + \"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"var jsonData;",
"",
"pm.test(\"Response is JSON object\", function() {",
"pm.test(pm.variables.get(\"test_prefix\") + \"Body is HTML\", function() {",
" pm.response.to.be.withBody;",
" pm.response.to.be.json;",
"",
" jsonData = pm.response.json();",
" pm.expect(jsonData).is.an(\"object\");",
"});",
" var expected_tag;",
"",
"pm.test(\"Schema is valid \", function() {",
" pm.expect(jsonData.keys).to.eqls()",
" pm.expect(jsonData).to.have.all.keys(\"status\", \"version\");",
" for (expected_tag of [\"<html\", \"</html>\", \"<body\", \"</body>\", \"<p\", \"</p>\"])",
" pm.expect(pm.response.text()).to.include(expected_tag)",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{SERVER}}/doc/index.html",
"protocol": "http",
"host": [
"{{SERVER}}"
],
"path": [
"doc",
"index.html"
]
}
},
"response": []
},
{
"name": "Load State Response Schema",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var code_ok = false;",
"",
" pm.expect(jsonData.status).is.a(\"string\");",
" pm.expect(jsonData.version).is.a(\"string\");",
"pm.test(pm.variables.get(\"test_prefix\") + \"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" code_ok = true",
"});",
"",
"pm.test(\"Server is running\", function () {",
" pm.expect(jsonData.status).to.eql(\"running\");",
"});",
"",
"pm.test(\"Version OK\", function () {",
" versionNos = jsonData.version.split(\".\");",
" pm.expect(versionNos.length, \"Semantic version number with three components expected\").to.eql(3);",
" pm.expect(versionNos[0], \"Wrong major version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MAJOR\"));",
" pm.expect(versionNos[1], \"Wrong minor version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MINOR\"));",
"pm.test(pm.variables.get(\"test_prefix\") + \"Response is JSON object\", function() {",
" pm.response.to.be.withBody;",
" pm.response.to.be.json;",
"",
" buildNos = versionNos[2].split(\"-\", limit=2);",
" pm.expect(buildNos.length, \"Malformatted build number. Expected Number[-Text]\").to.be.oneOf([1,2]);",
" const jsonData = pm.response.json();",
" pm.expect(jsonData).is.an(\"object\");",
"",
" if(code_ok) pm.collectionVariables.set(\"expected_json_schema\", JSON.stringify(jsonData));",
"});"
],
"type": "text/javascript"
......@@ -57,68 +127,114 @@
"method": "GET",
"header": [],
"url": {
"raw": "http://{{SERVER}}/state",
"raw": "http://{{SERVER}}{{SCHEMA_PATH}}/response-state-schema.json",
"protocol": "http",
"host": [
"{{SERVER}}"
"{{SERVER}}{{SCHEMA_PATH}}"
],
"path": [
"state"
"response-state-schema.json"
]
}
},
"response": []
},
{
"name": "Documentation on root",
"name": "Selfreported State",
"event": [
{
"listen": "prerequest",
"listen": "test",
"script": {
"exec": [
"pm.test(pm.variables.get(\"test_prefix\") + \"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"",
"var jsonData;",
"",
"if(pm.collectionVariables.has(\"expected_json_schema\")) {",
" pm.test(pm.variables.get(\"test_prefix\") + \"Response JSON schema valid\", function() {",
" pm.response.to.be.withBody;",
" pm.response.to.be.json;",
"",
" const schema_text = pm.variables.get(\"expected_json_schema\")",
" const schema = JSON.parse(schema_text)",
"",
" jsonData = pm.response.json();",
" pm.expect(tv4.validate(jsonData, schema)).to.be.true;",
" });",
"",
" pm.test(pm.variables.get(\"test_prefix\") + \"Version OK\", function () {",
" // if response follows the schema, jsonData.version contains a valid version number",
" versionNos = jsonData.version.split(\".\", limit=3);",
" pm.expect(versionNos[0], \"Wrong major version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MAJOR\"));",
" pm.expect(versionNos[1], \"Wrong minor version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MINOR\"));",
" });",
"",
"} else {",
" pm.test(pm.variables.get(\"test_prefix\") + \"Response is JSON object\", function() {",
" pm.response.to.be.withBody;",
" pm.response.to.be.json;",
"",
" jsonData = pm.response.json();",
" pm.expect(jsonData).is.an(\"object\");",
" });",
"",
" pm.test(pm.variables.get(\"test_prefix\") + \"Response seems valid (Schema missing)\", function() {",
" pm.expect(jsonData).to.have.all.keys(\"status\", \"version\");",
"",
" pm.expect(jsonData.status).is.a(\"string\");",
" pm.expect(jsonData.version).is.a(\"string\");",
" });",
"",
" pm.test(pm.variables.get(\"test_prefix\") + \"Version OK\", function () {",
" versionNos = jsonData.version.split(\".\");",
" pm.expect(versionNos.length, \"Semantic version number with three components expected\").to.eql(3);",
" pm.expect(versionNos[0], \"Wrong major version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MAJOR\"));",
" pm.expect(versionNos[1], \"Wrong minor version!\").to.eql(pm.variables.get(\"EXPECTED_VERSION_MINOR\"));",
"",
" buildNos = versionNos[2].split(\"-\", limit=2);",
" pm.expect(buildNos.length, \"Malformatted build number. Expected Number[-Text]\").to.be.oneOf([1,2]);",
" });",
"}",
"",
"pm.test(pm.variables.get(\"test_prefix\") + \"Server is running\", function () {",
" pm.expect(jsonData.status).to.eql(\"running\");",
"});",
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"listen": "prerequest",
"script": {
"exec": [
"pm.test(\"Status is redirection\", function () {",
" pm.expect(pm.response.code).to.be.oneOf([301,302,307,308]);",
"});",
"",
"pm.test(\"Target location is documentation\", function() {",
" pm.response.to.have.header(\"Location\");",
"",
" const doc_location = \"/doc/index.html\"",
" pm.expect(pm.response.headers.get(\"Location\")).to.be.oneOf([doc_location, ",
" \"http://\" + pm.variables.get(\"SERVER\")+doc_location])",
"});"
""
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"followRedirects": false
},
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{SERVER}}",
"raw": "http://{{SERVER}}/state",
"protocol": "http",
"host": [
"{{SERVER}}"
],
"path": [
"state"
]
}
},
"response": []
},
{
"name": "Documentation Available Copy",
"name": "Documentation on root",
"event": [
{
"listen": "prerequest",
......@@ -133,39 +249,57 @@
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"pm.test(pm.variables.get(\"test_prefix\") + \"Status is redirection\", function () {",
" pm.expect(pm.response.code).to.be.oneOf([301,302,307,308]);",
"});",
"",
"pm.test(\"Body is HTML\", function() {",
" pm.response.to.be.withBody;",
"",
" var expected_tag;",
"pm.test(pm.variables.get(\"test_prefix\") + \"Target location is documentation\", function() {",
" pm.response.to.have.header(\"Location\");",
"",
" for (expected_tag of [\"<html\", \"</html>\", \"<body\", \"</body>\", \"<p\", \"</p>\"])",
" pm.expect(pm.response.text()).to.include(expected_tag)",
" const doc_location = \"/doc/index.html\"",
" pm.expect(pm.response.headers.get(\"Location\")).to.be.oneOf([doc_location, ",
" \"http://\" + pm.variables.get(\"SERVER\")+doc_location])",
"});"
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"followRedirects": false
},
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{SERVER}}/doc/index.html",
"raw": "http://{{SERVER}}",
"protocol": "http",
"host": [
"{{SERVER}}"
],
"path": [
"doc",
"index.html"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
"pm.variables.set(\"test_prefix\", pm.info.requestName + \": \");"
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment