70 lines
3.9 KiB
C++
70 lines
3.9 KiB
C++
// Routes: POST /google/discovery/{apis,methods,describe,plan,call}
|
|||
|
|
// Bad input is 422 (with or without a credential) before anything else; a
|
||
|
|
// well formed request without a credential is 503 google_not_configured;
|
||
|
|
// GET is 405; nothing reaches Google in any of these cases.
|
||
|
|
#include "support/GoogleRig.hpp"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
struct Route {
|
||
|
|
const char* path;
|
||
|
|
const char* valid;
|
||
|
|
const char* bad;
|
||
|
|
};
|
||
|
|
|
||
|
|
const Route kRoutes[] = {
|
||
|
|
{"/google/discovery/apis", "{\"filter\":\"bigquery\",\"limit\":10}", "{\"limit\":0}"},
|
||
|
|
{"/google/discovery/methods", "{\"api\":\"bigquery:v2\",\"filter\":\"jobs\"}", "{}"},
|
||
|
|
{"/google/discovery/describe", "{\"api\":\"bigquery\",\"method\":\"bigquery.jobs.query\"}",
|
||
|
|
"{\"api\":\"bigquery\"}"},
|
||
|
|
{"/google/discovery/plan",
|
||
|
|
"{\"api\":\"serviceusage\",\"method\":\"serviceusage.services.list\",\"params\":{\"parent\":\"projects/p\"}}",
|
||
|
|
"{\"api\":\"serviceusage\",\"method\":\"serviceusage.services.list\",\"params\":[1]}"},
|
||
|
|
{"/google/discovery/call",
|
||
|
|
"{\"api\":\"serviceusage\",\"method\":\"serviceusage.services.list\",\"params\":{\"parent\":\"projects/p\"},\"maxPages\":2}",
|
||
|
|
"{\"api\":\"serviceusage\",\"method\":\"serviceusage.services.list\",\"maxPages\":51}"},
|
||
|
|
};
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
GoogleRig rig(false);
|
||
|
|
Fixture f(rig.client);
|
||
|
|
for (const Route& r : kRoutes) {
|
||
|
|
std::string p = r.path;
|
||
|
|
auto ok = f.client->post(r.path, r.valid, "application/json");
|
||
|
|
check(ok.status == 503 && contains(ok.body, "google_not_configured"), p + ": no credential is 503");
|
||
|
|
check(f.client->post(r.path, r.bad, "application/json").status == 422, p + ": bad input is 422");
|
||
|
|
check(f.client->post(r.path, "not json", "application/json").status == 422, p + ": invalid json is 422");
|
||
|
|
check(f.client->post(r.path, "[]", "application/json").status == 422, p + ": non-object body is 422");
|
||
|
|
check(f.client->get(r.path).status == 405, p + ": get is 405");
|
||
|
|
}
|
||
|
|
// Shape rules shared by the method routes.
|
||
|
|
const char* methods[] = {"/google/discovery/describe", "/google/discovery/plan", "/google/discovery/call"};
|
||
|
|
for (const char* path : methods) {
|
||
|
|
std::string p = path;
|
||
|
|
check(f.client->post(path, "{\"api\":\"../x\",\"method\":\"a.b\"}", "application/json").status == 422, p + ": path-like api is 422");
|
||
|
|
check(f.client->post(path, "{\"api\":\"a:b:c\",\"method\":\"a.b\"}", "application/json").status == 422, p + ": two colons is 422");
|
||
|
|
check(f.client->post(path, "{\"api\":\"a\",\"method\":\"a b\"}", "application/json").status == 422, p + ": spaced method is 422");
|
||
|
|
check(f.client->post(path, "{\"api\":5,\"method\":\"a.b\"}", "application/json").status == 422, p + ": non-string api is 422");
|
||
|
|
}
|
||
|
|
const char* callers[] = {"/google/discovery/plan", "/google/discovery/call"};
|
||
|
|
for (const char* path : callers) {
|
||
|
|
std::string p = path;
|
||
|
|
std::string head = "{\"api\":\"tasks\",\"method\":\"tasks.tasks.list\",";
|
||
|
|
check(f.client->post(path, head + "\"auth\":\"magic\"}", "application/json").status == 422, p + ": unknown auth is 422");
|
||
|
|
check(f.client->post(path, head + "\"body\":[1]}", "application/json").status == 422, p + ": array body is 422");
|
||
|
|
check(f.client->post(path, head + "\"wait\":\"yes\"}", "application/json").status == 422, p + ": non-bool wait is 422");
|
||
|
|
check(f.client->post(path, head + "\"maxPages\":\"3\"}", "application/json").status == 422, p + ": text maxPages is 422");
|
||
|
|
}
|
||
|
|
check(f.client->post("/google/discovery/call",
|
||
|
|
"{\"api\":\"tasks\",\"method\":\"tasks.tasks.list\",\"confirm\":\"yes\"}",
|
||
|
|
"application/json").status == 422,
|
||
|
|
"non-bool confirm is 422");
|
||
|
|
std::string big(600 * 1024, 'a');
|
||
|
|
check(f.client->post("/google/discovery/plan", "{\"x\":\"" + big + "\"}", "application/json").status == 413,
|
||
|
|
"oversized body is 413");
|
||
|
|
check(rig.mock->send_count() == 0, "nothing reached Google");
|
||
|
|
return finish("google/discovery/routes");
|
||
|
|
}
|