Skip to content

Commit

Permalink
Add test for serverIndex.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbirkner committed Jul 15, 2024
1 parent bd5f1f2 commit f3edafc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
3 changes: 1 addition & 2 deletions libs/pyzswagcl/py-openapi-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ PyOpenApiClient::PyOpenApiClient(std::string const& openApiUrl,
std::ifstream fs(openApiUrl);
return parseOpenAPIConfig(fs);
}
else
return fetchOpenAPIConfig(openApiUrl, *httpClient, httpConfig);
return fetchOpenAPIConfig(openApiUrl, *httpClient, httpConfig);
}();

client_ = std::make_unique<OpenAPIClient>(
Expand Down
2 changes: 1 addition & 1 deletion libs/zswag/test/calc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run_test(aspect, request, fn, expect, auth_args):
try:
print(f"[py-test-client] Test#{counter}: {aspect}", flush=True)
print(f"[py-test-client] -> Instantiating client.", flush=True)
oa_client = OAClient(f"http://{host}:{port}/openapi.json", **auth_args)
oa_client = OAClient(f"http://{host}:{port}/openapi.json", **auth_args, server_index=0)
# Just make sure that OpenAPI JSON content is parsable
assert oa_client.config().content and json.loads(oa_client.config().content)
client = api.Calculator.Client(oa_client)
Expand Down
30 changes: 16 additions & 14 deletions libs/zswag/test/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main (int argc, char* argv[]) {

spdlog::info("[cpp-test-client] Starting integration tests with {}", specUrl);

auto runTest = [&] (auto const& fn, auto expect, std::string const& aspect, std::function<void(httpcl::Config&)> const& authFun)
auto runTest = [&] (auto const& fn, auto expect, std::string const& aspect, std::function<void(Config&)> const& authFun)
{
++testCounter;
spdlog::info("[cpp-test-client] Executing test #{}: {} ...", testCounter, aspect);
Expand All @@ -28,9 +28,11 @@ int main (int argc, char* argv[]) {
spdlog::info("[cpp-test-client] => Instantiating client.");
auto httpClient = std::make_unique<HttpLibHttpClient>();
auto openApiConfig = fetchOpenAPIConfig(specUrl, *httpClient);
httpcl::Config authHttpConf;
// See https://github.com/spec-first/connexion/issues/1139
openApiConfig.servers.insert(openApiConfig.servers.begin(), URIComponents::fromStrPath("/bad/path/we/dont/access"));
Config authHttpConf;
authFun(authHttpConf);
auto oaClient = OAClient(openApiConfig, std::move(httpClient), authHttpConf);
auto oaClient = OAClient(openApiConfig, std::move(httpClient), authHttpConf, 1);
spdlog::info("[cpp-test-client] => Running request.");
calculator::Calculator::Client calcClient(oaClient);
auto response = fn(calcClient);
Expand All @@ -49,22 +51,22 @@ int main (int argc, char* argv[]) {
calculator::BaseAndExponent req(calculator::I32(2), calculator::I32(3), 0, "", .0, std::vector<bool>{});
return calcClient.powerMethod(req);
}, 8., "Pass fields in path and header",
[](httpcl::Config& conf){});
[](Config& conf){});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Integers req(std::vector<int32_t>{100, -200, 400});
return calcClient.intSumMethod(req);
}, 300., "Pass hex-encoded array in query",
[](httpcl::Config& conf){
[](Config& conf){
conf.headers.insert({"Authorization", "Bearer 123"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Bytes req(std::vector<uint8_t>{8, 16, 32, 64});
return calcClient.byteSumMethod(req);
}, 120., "Pass base64url-encoded byte array in path",
[](httpcl::Config& conf){
conf.auth = httpcl::Config::BasicAuthentication{
[](Config& conf){
conf.auth = Config::BasicAuthentication{
"u", "pw", ""
};
});
Expand All @@ -73,55 +75,55 @@ int main (int argc, char* argv[]) {
calculator::Integers req(std::vector<int32_t>{1, 2, 3, 4});
return calcClient.intMulMethod(req);
}, 24., "Pass base64-encoded long array in path",
[](httpcl::Config& conf){
[](Config& conf){
conf.query.insert({"api-key", "42"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Doubles req(std::vector<double>{34.5, 2.});
return calcClient.floatMulMethod(req);
}, 69., "Pass float array in query.",
[](httpcl::Config& conf){
[](Config& conf){
conf.cookies.insert({"api-cookie", "42"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Bools req(std::vector<bool>{true, false});
return calcClient.bitMulMethod(req);
}, false, "Pass bool array in query (expect false).",
[](httpcl::Config& conf){
[](Config& conf){
conf.apiKey = "42";
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Bools req(std::vector<bool>{true, true});
return calcClient.bitMulMethod(req);
}, true, "Pass bool array in query (expect true).",
[](httpcl::Config& conf){
[](Config& conf){
conf.headers.insert({"X-Generic-Token", "42"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Double req(1.);
return calcClient.identityMethod(req);
}, 1., "Pass request as blob in body",
[](httpcl::Config& conf){
[](Config& conf){
conf.cookies.insert({"api-cookie", "42"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::Strings req(std::vector<std::string>{"foo", "bar"});
return calcClient.concatMethod(req);
}, std::string("foobar"), "Pass base64-encoded strings.",
[](httpcl::Config& conf){
[](Config& conf){
conf.headers.insert({"Authorization", "Bearer 123"});
});

runTest([](calculator::Calculator::Client& calcClient){
calculator::EnumWrapper req(calculator::Enum::TEST_ENUM_0);
return calcClient.nameMethod(req);
}, std::string("TEST_ENUM_0"), "Pass enum.",
[](httpcl::Config& conf){
[](Config& conf){
conf.apiKey = "42";
});

Expand Down
5 changes: 3 additions & 2 deletions libs/zswagcl/include/zswagcl/oaclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class OAClient : public ::zserio::IServiceClient
{
public:
OAClient(
zswagcl::OpenAPIConfig config,
OpenAPIConfig config,
std::unique_ptr<httpcl::IHttpClient> client,
httpcl::Config httpConfig = {});
httpcl::Config httpConfig = {},
uint32_t serverIndex = 0);

std::vector<uint8_t> callMethod(
zserio::StringView methodName,
Expand Down
5 changes: 3 additions & 2 deletions libs/zswagcl/src/oaclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace zswagcl

OAClient::OAClient(zswagcl::OpenAPIConfig config,
std::unique_ptr<httpcl::IHttpClient> client,
httpcl::Config httpConfig)
: client_(std::move(config), std::move(httpConfig), std::move(client))
httpcl::Config httpConfig,
uint32_t serverIndex)
: client_(std::move(config), std::move(httpConfig), std::move(client), serverIndex)
{}

template<typename arr_elem_t>
Expand Down

0 comments on commit f3edafc

Please sign in to comment.