test_https.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <nlohmann_json/json.hpp>
  2. #include "kbf.h"
  3. #include "kbf/wifi.h"
  4. #include "kbf/http/server.h"
  5. #include "kbf/http/client.h"
  6. #include "kbf/rtos.h"
  7. #include <unity.h>
  8. using namespace kbf;
  9. using namespace std;
  10. using nlohmann::json;
  11. TEST_CASE("HTTPS GET", "[kbf_http]") {
  12. wifi::start();
  13. auto server = http::Server();
  14. TEST_ASSERT_FALSE(server.isRunning())
  15. http::Response (*handleGet)(const http::Request &, void *) = {[](const http::Request &request, void *) {
  16. TEST_ASSERT_EQUAL(http::GET, request.method);
  17. return http::Response("OK");
  18. }};
  19. server.route({http::GET, "/get-me", handleGet, nullptr});
  20. server.startSSL();
  21. TEST_ASSERT_TRUE(server.isRunning())
  22. auto client = http::Client();
  23. auto response = client.get("https://localhost/get-me");
  24. TEST_ASSERT_EQUAL(200, response->status);
  25. TEST_ASSERT_EQUAL_STRING("OK", response->body.c_str());
  26. server.stop();
  27. wifi::stop();
  28. }
  29. // TODO KBF-22 fix async test
  30. static rtos::EventGroup eventGroup;
  31. TEST_CASE("HTTPS async", "[broken]") {
  32. wifi::start();
  33. http::Response (*handleRequest)(const http::Request &, void *) = {[](const http::Request &request, void *) {
  34. auto response = http::Response("OK");
  35. kbf::sleep(100);
  36. return response;
  37. }};
  38. auto server = http::Server()
  39. .route({http::GET, "/", handleRequest, nullptr})
  40. .startSSL();
  41. auto client = http::Client(true);
  42. client.onSuccess = {[](http::Client &client, const http::Response &response) {
  43. TEST_ASSERT_EQUAL_STRING("OK", response.body.data());
  44. eventGroup.setBit(0);
  45. }};
  46. auto response = client.get("https://localhost/");
  47. TEST_ASSERT_NULL(response)
  48. TEST_ASSERT_EQUAL(0, eventGroup.getBit(0));
  49. kbf::sleep(200);
  50. TEST_ASSERT_EQUAL(1, eventGroup.getBit(0));
  51. server.stop();
  52. wifi::stop();
  53. }