feat: cJSON, Gerätenamen und HTTP-Parser
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "net/nl_http.h"
|
||||
|
||||
void setUp(void) {}
|
||||
void tearDown(void) {}
|
||||
|
||||
static nl_http_parse_t parse(const char *s, nl_http_request_t *r)
|
||||
{
|
||||
return nl_http_parse(s, strlen(s), r);
|
||||
}
|
||||
|
||||
static void test_simple_get(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, parse("GET /api/v1/device/state HTTP/1.1\r\nHost: x\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_GET, r.method);
|
||||
TEST_ASSERT_EQUAL_STRING("/api/v1/device/state", r.path);
|
||||
TEST_ASSERT_EQUAL_STRING("", r.auth);
|
||||
TEST_ASSERT_EQUAL_UINT(0, r.body_len);
|
||||
}
|
||||
|
||||
static void test_post_body_arrives_in_parts(void)
|
||||
{
|
||||
const char *full = "POST /api/v1/device/state HTTP/1.1\r\nContent-Length: 11\r\n\r\n{\"on\":true}";
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(full, 20, &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(full, strlen(full) - 1, &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, nl_http_parse(full, strlen(full), &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_POST, r.method);
|
||||
TEST_ASSERT_EQUAL_UINT(11, r.body_len);
|
||||
TEST_ASSERT_EQUAL_MEMORY("{\"on\":true}", r.body, 11);
|
||||
}
|
||||
|
||||
static void test_headers_case_insensitive_and_bearer(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE,
|
||||
parse("PUT /api/v1/system/command HTTP/1.1\r\nauthorization: Bearer abc123\r\n"
|
||||
"CONTENT-LENGTH: 2\r\n\r\n{}", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_PUT, r.method);
|
||||
TEST_ASSERT_EQUAL_STRING("abc123", r.auth);
|
||||
TEST_ASSERT_EQUAL_UINT(2, r.body_len);
|
||||
}
|
||||
|
||||
static void test_plain_token_and_query_string(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE,
|
||||
parse("GET /api/v1/system/info?x=1 HTTP/1.0\r\nAuthorization: aabbccddeeff0011223344\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL_STRING("/api/v1/system/info", r.path);
|
||||
TEST_ASSERT_EQUAL_STRING("aabbccddeeff0011223344", r.auth);
|
||||
}
|
||||
|
||||
static void test_other_method(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, parse("DELETE /api/v1/device/state HTTP/1.1\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_OTHER, r.method);
|
||||
}
|
||||
|
||||
static void test_body_too_large(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_TOO_LARGE, parse("POST / HTTP/1.1\r\nContent-Length: 1025\r\n\r\n", &r));
|
||||
}
|
||||
|
||||
static void test_headers_never_end(void)
|
||||
{
|
||||
static char buf[NL_HTTP_MAX_REQUEST];
|
||||
nl_http_request_t r;
|
||||
memset(buf, 'a', sizeof buf);
|
||||
memcpy(buf, "GET / HTTP/1.1\r\n", 16);
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_TOO_LARGE, nl_http_parse(buf, sizeof buf, &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(buf, 100, &r));
|
||||
}
|
||||
|
||||
static void test_bad_requests(void)
|
||||
{
|
||||
nl_http_request_t r;
|
||||
char longpath[200];
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("GET /x\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("GET /x FTP/1.1\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("\r\n\r\n", &r));
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r));
|
||||
strcpy(longpath, "GET /");
|
||||
memset(longpath + 5, 'p', 100);
|
||||
strcpy(longpath + 105, " HTTP/1.1\r\n\r\n");
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse(longpath, &r));
|
||||
}
|
||||
|
||||
static void test_response(void)
|
||||
{
|
||||
char out[256];
|
||||
size_t n = nl_http_response(out, sizeof out, 404, "{\"error\":\"not found\"}");
|
||||
TEST_ASSERT_EQUAL_STRING("HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n"
|
||||
"Content-Length: 21\r\nConnection: close\r\n\r\n{\"error\":\"not found\"}", out);
|
||||
TEST_ASSERT_EQUAL_UINT(strlen(out), n);
|
||||
TEST_ASSERT_EQUAL_UINT(0, nl_http_response(out, 20, 200, "{}"));
|
||||
TEST_ASSERT_EQUAL_STRING("Accepted", nl_http_reason(202));
|
||||
TEST_ASSERT_EQUAL_STRING("Payload Too Large", nl_http_reason(413));
|
||||
TEST_ASSERT_EQUAL_STRING("Not Implemented", nl_http_reason(501));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_simple_get);
|
||||
RUN_TEST(test_post_body_arrives_in_parts);
|
||||
RUN_TEST(test_headers_case_insensitive_and_bearer);
|
||||
RUN_TEST(test_plain_token_and_query_string);
|
||||
RUN_TEST(test_other_method);
|
||||
RUN_TEST(test_body_too_large);
|
||||
RUN_TEST(test_headers_never_end);
|
||||
RUN_TEST(test_bad_requests);
|
||||
RUN_TEST(test_response);
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user