Files

78 lines
4.1 KiB
C++
Raw Permalink Normal View History

// Routes: every POST that would call Google Maps. No credential is 503
// google_not_configured, bad input is 422 before any traffic, GET is 405, and
// with a credential but no confirm the answer is a dry run that shows the
// exact request and sends nothing.
#include <string>
#include <vector>
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
namespace {
struct Case {
std::string path;
std::string good;
std::string bad;
std::string expect_in_request; // proof of the exact request shape
};
} // namespace
int main() {
const std::string json = "application/json";
const std::vector<Case> cases = {
{"/google/maps/places/search", "{\"query\":\"pizza\",\"limit\":\"3\"}", "{\"query\":\"\"}",
"/v1/places:searchText?fields=places.id"},
{"/google/maps/places/nearby", "{\"lat\":\"52.37\",\"lng\":\"4.89\",\"radius\":\"300\",\"type\":\"cafe\"}",
"{\"lat\":\"99\",\"lng\":\"4.89\"}", "/v1/places:searchNearby"},
{"/google/maps/places/autocomplete", "{\"input\":\"Dam\"}", "{}", "/v1/places:autocomplete"},
{"/google/maps/places/details", "{\"place_id\":\"ChIJabc\"}", "{\"place_id\":\"a/../b\"}",
"/v1/places/ChIJabc?fields="},
{"/google/maps/address/validate", "{\"region\":\"NL\",\"lines\":\"Dam 1\"}", "{\"region\":\"NL\"}",
"/v1:validateAddress"},
{"/google/maps/solar", "{\"lat\":\"52.37\",\"lng\":\"4.89\"}", "{\"lat\":\"x\",\"lng\":\"4\"}",
"/v1/buildingInsights:findClosest?location.latitude=52.37"},
{"/google/maps/pollen", "{\"lat\":\"52.37\",\"lng\":\"4.89\",\"days\":\"2\"}",
"{\"lat\":\"52.37\",\"lng\":\"4.89\",\"days\":\"9\"}", "/v1/forecast:lookup?"},
{"/google/maps/air", "{\"lat\":\"52.37\",\"lng\":\"4.89\"}", "{\"lat\":\"52.37\"}",
"/v1/currentConditions:lookup"},
{"/google/maps/area", "{\"lat\":\"52.37\",\"lng\":\"4.89\",\"radius\":\"500\"}",
"{\"lat\":\"52.37\",\"lng\":\"4.89\",\"radius\":\"0\"}", "/v1:computeInsights"},
{"/google/maps/routes", "{\"origin\":\"52.37,4.89\",\"destination\":\"Utrecht Centraal\"}",
"{\"origin\":\"52.37,4.89\"}", "/directions/v2:computeRoutes?fields=routes.duration"},
{"/google/maps/geocode", "{\"address\":\"Dam 1 Amsterdam\"}", "{\"lat\":\"1\"}",
"/maps/api/geocode/json?address=Dam%201%20Amsterdam"},
{"/google/maps/timezone", "{\"lat\":\"52.37\",\"lng\":\"4.89\",\"timestamp\":\"1700000000\"}",
"{\"lat\":\"52.37\",\"lng\":\"4.89\"}", "/maps/api/timezone/json?location=52.3700000"},
{"/google/maps/elevation", "{\"locations\":\"52.37,4.89|52.38,4.90\"}", "{\"locations\":\"nope\"}",
"/maps/api/elevation/json?locations=52.3700000"},
{"/google/maps/streetview/list", "{\"limit\":\"2\"}", "{\"limit\":\"500\"}", "/v1/photos?pageSize=2"},
};
for (const Case& c : cases) {
{
GoogleRig rig(false);
Fixture f(rig.client);
auto res = f.client->post(c.path, c.good, json);
check(res.status == 503, c.path + ": no credential is 503");
check(contains(res.body, "google_not_configured"), c.path + ": error code google_not_configured");
check(contains(res.headers.get("Content-Type"), "application/json"), c.path + ": error body is json");
check(rig.mock->send_count() == 0, c.path + ": nothing sent without a credential");
}
{
GoogleRig rig;
Fixture f(rig.client);
auto bad = f.client->post(c.path, c.bad, json);
check(bad.status == 422, c.path + ": bad input is 422");
check(contains(bad.body, "validation_error"), c.path + ": error code validation_error");
check(f.client->get(c.path).status == 405, c.path + ": get is 405");
auto dry = f.client->post(c.path, c.good, json);
check(dry.status == 200, c.path + ": dry run is 200");
check(contains(dry.body, "\"dry_run\":true"), c.path + ": answer says dry run");
check(contains(dry.body, c.expect_in_request), c.path + ": exact request shown (" + c.expect_in_request + ")");
check(!contains(dry.body, kTestKey), c.path + ": credential never shown");
check(rig.mock->send_count() == 0, c.path + ": side effect: a dry run sends nothing");
}
}
return finish("google/maps/calls");
}