30 lines
1.9 KiB
C++
30 lines
1.9 KiB
C++
// Route: GET /expose/stats, POST /expose/record (per-method table, windowed kB/s)
|
|||
|
|
#include "support/Assert.hpp"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
using molo::example::fw::Method;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
Fixture f;
|
||
|
|
auto empty = f.client->get("/expose/stats");
|
||
|
|
check(empty.status == 200 && contains(empty.body, "_total"), "page with total row");
|
||
|
|
auto form = f.client->get("/expose/record");
|
||
|
|
check(form.status == 200 && contains(form.body, "<form"), "record form");
|
||
|
|
const std::map<std::string, std::string> h = {{"Content-Type", "application/x-www-form-urlencoded"}};
|
||
|
|
auto ok = f.client->request(Method::Post, "/expose/record", "method=GET&bytes_up=2048&bytes_down=10240µs=4000", h);
|
||
|
|
check(ok.status == 303 && ok.headers.get("Location") == "/expose/stats", "redirect");
|
||
|
|
f.client->request(Method::Post, "/expose/record", "method=GET&bytes_up=0&bytes_down=0µs=2000", h);
|
||
|
|
auto js = f.client->request(Method::Get, "/expose/stats", "", {{"Accept", "application/json"}});
|
||
|
|
check(contains(js.body, "\"GET\":{\"count\":2"), "count per method");
|
||
|
|
check(contains(js.body, "\"kb_up\":2.0") && contains(js.body, "\"kb_down\":10.0"), "kB up and down");
|
||
|
|
check(contains(js.body, "\"avg_ms\":3.00"), "average latency");
|
||
|
|
check(contains(js.body, "\"kb_s_down\":0.17"), "windowed kB/s (10 kB over 60 s)");
|
||
|
|
auto bad = f.client->request(Method::Post, "/expose/record", "method=FETCH&bytes_up=-1&bytes_down=xµs=", h);
|
||
|
|
check(bad.status == 422 && contains(bad.body, "field-error"), "inline errors in page");
|
||
|
|
auto badjson = f.client->request(Method::Post, "/expose/record", "method=FETCH&bytes_up=1&bytes_down=1µs=1",
|
||
|
|
{{"Content-Type", "application/x-www-form-urlencoded"}, {"Accept", "application/json"}});
|
||
|
|
check(badjson.status == 422 && contains(badjson.body, "method"), "json client gets json");
|
||
|
|
check(f.client->request(Method::Delete, "/expose/stats").status == 405, "delete is 405");
|
||
|
|
return finish("expose/stats");
|
||
|
|
}
|