forked from microsoft/CCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.cpp
106 lines (85 loc) · 2.65 KB
/
openapi.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#include "ds/openapi.h"
#include "http/http_consts.h"
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
using namespace ds;
void print_doc(const std::string& title, const nlohmann::json& doc)
{
std::cout << title << std::endl;
std::cout << doc.dump(2) << std::endl;
}
#define REQUIRE_ELEMENT(j, name, type_fn) \
{ \
const auto name##_it = j.find(#name); \
REQUIRE(name##_it != j.end()); \
REQUIRE(name##_it->type_fn()); \
}
static constexpr auto server_url =
"https://not.a.real.server.example.com/testing_only";
// TODO: Use some external verifier to do this properly. This is the basic
// stuff for initial compatibility
void required_doc_elements(const nlohmann::json& j)
{
REQUIRE_ELEMENT(j, openapi, is_string);
REQUIRE_ELEMENT(j, info, is_object);
REQUIRE_ELEMENT(j, paths, is_object);
}
TEST_CASE("Required elements")
{
openapi::Document doc;
const nlohmann::json j = doc;
required_doc_elements(j);
}
TEST_CASE("Manual construction")
{
openapi::Document doc;
doc.info.title = "Test generated API";
doc.info.description = "Some longer description enhanced with **Markdown**";
doc.info.version = "0.1.42";
{
openapi::Server mockup_server;
mockup_server.url = server_url;
doc.servers.push_back(mockup_server);
}
{
doc.paths["/users/foo"][HTTP_GET][HTTP_STATUS_OK].description =
"Indicates that everything went ok";
}
const nlohmann::json j = doc;
required_doc_elements(j);
const auto& info_element = j["info"];
REQUIRE_ELEMENT(info_element, title, is_string);
REQUIRE_ELEMENT(info_element, description, is_string);
REQUIRE_ELEMENT(info_element, version, is_string);
REQUIRE_ELEMENT(j, servers, is_array);
const auto& servers_element = j["servers"];
REQUIRE(servers_element.size() == 1);
const auto& first_server = servers_element[0];
REQUIRE_ELEMENT(first_server, url, is_string);
print_doc("PATHS", doc);
}
TEST_CASE("Schema population")
{
openapi::Document doc;
doc.info.title = "Test generated API";
doc.info.description = "Some longer description enhanced with **Markdown**";
doc.info.version = "0.1.42";
{
openapi::Server mockup_server;
mockup_server.url = server_url;
doc.servers.push_back(mockup_server);
}
constexpr auto users_foo = "/users/foo";
constexpr auto foo_schema_name = "foo_str";
doc.add_response_schema<std::string>(
users_foo,
HTTP_GET,
HTTP_STATUS_OK,
http::headervalues::contenttype::TEXT,
foo_schema_name);
const nlohmann::json j = doc;
required_doc_elements(j);
print_doc("SCHEMA", doc);
}