123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include <kbf/wifi_legacy.h>
- #include <kbf/http/client.h>
- #include <kbf/web_service.h>
- #include <unity.h>
- using namespace kbf;
- extern const unsigned char cert_pem_start[] asm("_binary_cert_pem_start");
- extern const unsigned char cert_pem_end[] asm("_binary_cert_pem_end");
- extern const unsigned char key_pem_start[] asm("_binary_key_pem_start");
- extern const unsigned char key_pem_end[] asm("_binary_key_pem_end");
- class CounterController : public WebService::Controller {
- public:
- CounterController() : Controller("/counter") {}
- http::Response get(const http::Request &request) override {
- return http::Response(std::to_string(counter));
- }
- void onResponseSent(const http::Response &response) override {
- counter++;
- }
- private:
- int counter = 0;
- };
- class EchoController : public WebService::Controller {
- public:
- EchoController() : Controller("/echo") {};
- http::Response get(const http::Request &request) override {
- return http::Response(request.query.at("str"));
- }
- http::Response post(const http::Request &request) override {
- auto requestJson = request.json();
- return http::Response(requestJson.find("str")->get<string>());
- }
- };
- TEST_CASE("WebService", "[kbf_web_service]") {
- wifi_legacy::start();
- auto webService = WebService();
- webService.controller<CounterController>();
- webService.controller<EchoController>();
- webService.startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
- http::Client::addCert(cert_pem_start, cert_pem_end);
- auto client = http::Client();
- auto response = client.get("https://localhost/counter");
- TEST_ASSERT_EQUAL(200, response->status);
- TEST_ASSERT_EQUAL_STRING("0", response->body.c_str());
- response = client.get("https://localhost/counter");
- TEST_ASSERT_EQUAL_STRING("1", response->body.c_str());
- response = client.post("https://localhost/counter", {{"str", "notallowed"}});
- TEST_ASSERT_EQUAL(405, response->status);
- TEST_ASSERT_EQUAL_STRING("method not allowed", response->body.c_str());
- response = client.get("https://localhost/echo?str=foo");
- TEST_ASSERT_EQUAL(200, response->status);
- TEST_ASSERT_EQUAL_STRING("foo", response->body.c_str());
- response = client.post("https://localhost/echo", {{"str", "bar"}});
- TEST_ASSERT_EQUAL(200, response->status);
- TEST_ASSERT_EQUAL_STRING("bar", response->body.c_str());
- webService.stop();
- wifi_legacy::stop();
- }
- class HeaderToParamMiddleware : public WebService::Middleware {
- public:
- http::Response run(const http::Request &request, WebService &webService) override {
- string str = request.readHeader("X-Request");
- auto changedRequest = request;
- if (request.method == http::GET) {
- changedRequest.query["str"] = str;
- } else if (request.method == http::POST) {
- changedRequest.body = R"({"str":")" + str + "\"}";
- } else {
- TEST_FAIL();
- }
- auto response = webService.next(changedRequest);
- response.headers["X-Response"] = response.body;
- response.body = "changed";
- return response;
- }
- };
- class ReverseMiddleware : public WebService::Middleware {
- public:
- http::Response run(const http::Request &request, WebService &webService) override {
- string str;
- if (request.method == http::GET) {
- str = request.query.at("str");
- } else if (request.method == http::POST) {
- str = request.json().find("str")->get<string>();
- } else {
- TEST_FAIL();
- }
- std::reverse(str.begin(), str.end());
- auto changedRequest = request;
- if (request.method == http::GET) {
- changedRequest.query["str"] = str;
- } else {
- changedRequest.body = R"({"str":")" + str + "\"}";
- }
- return webService.next(changedRequest);
- }
- };
- TEST_CASE("WebService Middleware", "[kbf_web_service]") {
- wifi_legacy::start();
- auto webService = WebService();
- webService.controller<EchoController>();
- webService.middleware<HeaderToParamMiddleware>();
- webService.middleware<ReverseMiddleware>();
- webService.start();
- auto client = http::Client();
- auto response = client.get("http://localhost/echo", {{{"X-Request", "hello"}}});
- TEST_ASSERT_EQUAL_STRING("changed", response->body.c_str());
- TEST_ASSERT_EQUAL_STRING("olleh", response->headers.at("X-Response").c_str());
- response = client.post("http://localhost/echo", nullptr,{{{"X-Request", "foo"}}});
- TEST_ASSERT_EQUAL_STRING("changed", response->body.c_str());
- TEST_ASSERT_EQUAL_STRING("oof", response->headers.at("X-Response").c_str());
- webService.stop();
- wifi_legacy::stop();
- }
|