123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <nlohmann_json/json.hpp>
- #include "kbf.h"
- #include "kbf/wifi_legacy.h"
- #include "kbf/http/server.h"
- #include "kbf/http/client.h"
- #include "kbf/http/exception.h"
- #include "kbf/rtos.h"
- #include <unity.h>
- using namespace kbf;
- using namespace std;
- using nlohmann::json;
- 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 cert_wrong_pem_start[] asm("_binary_wrong_cert_pem_start");
- extern const unsigned char cert_wrong_pem_end[] asm("_binary_wrong_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");
- static void assert_fail(http::Client &client, const string &message) {
- bool caught = false;
- try {
- auto response = client.get("https://localhost/get-me");
- ESP_LOGI("assert", "gonna fail, got %d", response->status);
- } catch (http::exception::ConnectionError &e) {
- caught = true;
- }
- TEST_ASSERT_TRUE_MESSAGE(caught, message.c_str());
- }
- static void assert_ok(http::Client &client, const string &message) {
- auto response = client.get("https://localhost/get-me");
- TEST_ASSERT_EQUAL_MESSAGE(200, response->status, message.c_str());
- TEST_ASSERT_EQUAL_STRING("OK", response->body.c_str());
- }
- TEST_CASE("HTTPS GET", "[kbf_http]") {
- wifi_legacy::start();
- auto server = http::Server();
- TEST_ASSERT_FALSE(server.isRunning())
- http::Response (*handleGet)(const http::Request &, void *) = {[](const http::Request &request, void *) {
- TEST_ASSERT_EQUAL(http::GET, request.method);
- return http::Response("OK");
- }};
- server.route({http::GET, "/get-me", handleGet, nullptr});
- server.startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
- TEST_ASSERT_TRUE(server.isRunning())
- auto client = http::Client();
- assert_fail(client, "no certs");
- http::Client::addCert(cert_pem_start, cert_pem_end);
- assert_ok(client, "right cert");
- client.disconnect();
- http::Client::clearCerts();
- assert_fail(client, "certs cleared");
- http::Client::addCert(cert_wrong_pem_start, cert_wrong_pem_end);
- assert_fail(client, "wrong cert");
- http::Client::addCert(cert_pem_start, cert_pem_end);
- assert_ok(client, "wrong + right cert");
- client.disconnect();
- http::Client::clearCerts();
- http::Client::addCert(cert_pem_start, cert_pem_end);
- http::Client::addCert(cert_wrong_pem_start, cert_wrong_pem_end);
- assert_ok(client, "right + wrong cert");
- server.stop();
- wifi_legacy::stop();
- }
- // TODO KBF-22 fix async test
- static rtos::EventGroup eventGroup;
- TEST_CASE("HTTPS async", "[broken]") {
- wifi_legacy::start();
- http::Response (*handleRequest)(const http::Request &, void *) = {[](const http::Request &request, void *) {
- auto response = http::Response("OK");
- kbf::sleep(100);
- return response;
- }};
- auto server = http::Server()
- .route({http::GET, "/", handleRequest, nullptr})
- .startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
- auto client = http::Client(true);
- client.onSuccess = {[](http::Client &client, const http::Response &response) {
- TEST_ASSERT_EQUAL_STRING("OK", response.body.data());
- eventGroup.setBit(0);
- }};
- auto response = client.get("https://localhost/");
- TEST_ASSERT_NULL(response)
- TEST_ASSERT_EQUAL(0, eventGroup.getBit(0));
- kbf::sleep(200);
- TEST_ASSERT_EQUAL(1, eventGroup.getBit(0));
- server.stop();
- wifi_legacy::stop();
- }
|