mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-14 10:20:01 +00:00
Added unit test
This commit is contained in:
1 parent
97392645c4
commit
7d5eb279a7
1 file changed
+63
-35
+63
-35
@@ -1,5 +1,31 @@
|
||||
#include "mongoose.c"
|
||||
|
||||
#define FATAL(str, line) do { \
|
||||
printf("Fail on line %d: [%s]\n", line, str); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
#define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
|
||||
|
||||
static void test_parse_http_request() {
|
||||
struct mg_request_info ri;
|
||||
char req1[] = "GET / HTTP/1.1\r\n\r\n";
|
||||
char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
|
||||
char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
|
||||
|
||||
ASSERT(parse_http_request(req1, &ri) == 1);
|
||||
ASSERT(strcmp(ri.http_version, "1.1") == 0);
|
||||
ASSERT(ri.num_headers == 0);
|
||||
|
||||
ASSERT(parse_http_request(req2, &ri) == 0);
|
||||
|
||||
// TODO(lsm): Fix this. Bah is not a valid header.
|
||||
ASSERT(parse_http_request(req3, &ri) == 1);
|
||||
ASSERT(ri.num_headers == 1);
|
||||
ASSERT(strcmp(ri.http_headers[0].name, "Bah\r\n") == 0);
|
||||
|
||||
// TODO(lsm): add more tests.
|
||||
}
|
||||
|
||||
static void test_should_keep_alive(void) {
|
||||
struct mg_connection conn;
|
||||
struct mg_context ctx;
|
||||
@@ -13,59 +39,60 @@ static void test_should_keep_alive(void) {
|
||||
parse_http_request(req1, &conn.request_info);
|
||||
|
||||
ctx.config[ENABLE_KEEP_ALIVE] = "no";
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
|
||||
ctx.config[ENABLE_KEEP_ALIVE] = "yes";
|
||||
assert(should_keep_alive(&conn) == 1);
|
||||
ASSERT(should_keep_alive(&conn) == 1);
|
||||
|
||||
conn.must_close = 1;
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
|
||||
conn.must_close = 0;
|
||||
parse_http_request(req2, &conn.request_info);
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
|
||||
parse_http_request(req3, &conn.request_info);
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
|
||||
parse_http_request(req4, &conn.request_info);
|
||||
assert(should_keep_alive(&conn) == 1);
|
||||
ASSERT(should_keep_alive(&conn) == 1);
|
||||
|
||||
conn.request_info.status_code = 401;
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
|
||||
conn.request_info.status_code = 200;
|
||||
conn.must_close = 1;
|
||||
assert(should_keep_alive(&conn) == 0);
|
||||
ASSERT(should_keep_alive(&conn) == 0);
|
||||
}
|
||||
|
||||
static void test_match_prefix(void) {
|
||||
assert(match_prefix("/a/", 3, "/a/b/c") == 3);
|
||||
assert(match_prefix("/a/", 3, "/ab/c") == -1);
|
||||
assert(match_prefix("/*/", 3, "/ab/c") == 4);
|
||||
assert(match_prefix("**", 2, "/a/b/c") == 6);
|
||||
assert(match_prefix("/*", 2, "/a/b/c") == 2);
|
||||
assert(match_prefix("*/*", 3, "/a/b/c") == 2);
|
||||
assert(match_prefix("**/", 3, "/a/b/c") == 5);
|
||||
assert(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
|
||||
assert(match_prefix("a|b|cd", 6, "cdef") == 2);
|
||||
assert(match_prefix("a|b|c?", 6, "cdef") == 2);
|
||||
assert(match_prefix("a|?|cd", 6, "cdef") == 1);
|
||||
assert(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
|
||||
assert(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
|
||||
assert(match_prefix("**/", 3, "/a/b/c") == 5);
|
||||
assert(match_prefix("**/$", 4, "/a/b/c") == -1);
|
||||
assert(match_prefix("**/$", 4, "/a/b/") == 5);
|
||||
assert(match_prefix("$", 1, "") == 0);
|
||||
assert(match_prefix("$", 1, "x") == -1);
|
||||
assert(match_prefix("*$", 2, "x") == 1);
|
||||
assert(match_prefix("/$", 2, "/") == 1);
|
||||
assert(match_prefix("**/$", 4, "/a/b/c") == -1);
|
||||
assert(match_prefix("**/$", 4, "/a/b/") == 5);
|
||||
assert(match_prefix("*", 1, "/hello/") == 0);
|
||||
assert(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
|
||||
assert(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
|
||||
assert(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
|
||||
ASSERT(match_prefix("/api", 4, "/api") == 4);
|
||||
ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
|
||||
ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
|
||||
ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
|
||||
ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
|
||||
ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
|
||||
ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
|
||||
ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
|
||||
ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
|
||||
ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
|
||||
ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
|
||||
ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
|
||||
ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
|
||||
ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
|
||||
ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
|
||||
ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
|
||||
ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
|
||||
ASSERT(match_prefix("$", 1, "") == 0);
|
||||
ASSERT(match_prefix("$", 1, "x") == -1);
|
||||
ASSERT(match_prefix("*$", 2, "x") == 1);
|
||||
ASSERT(match_prefix("/$", 2, "/") == 1);
|
||||
ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
|
||||
ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
|
||||
ASSERT(match_prefix("*", 1, "/hello/") == 0);
|
||||
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
|
||||
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
|
||||
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
|
||||
}
|
||||
|
||||
static void test_remove_double_dots() {
|
||||
@@ -89,7 +116,7 @@ static void test_remove_double_dots() {
|
||||
for (i = 0; i < ARRAY_SIZE(data); i++) {
|
||||
//printf("[%s] -> [%s]\n", data[i].before, data[i].after);
|
||||
remove_double_dots_and_double_slashes(data[i].before);
|
||||
assert(strcmp(data[i].before, data[i].after) == 0);
|
||||
ASSERT(strcmp(data[i].before, data[i].after) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,5 +124,6 @@ int main(void) {
|
||||
test_match_prefix();
|
||||
test_remove_double_dots();
|
||||
test_should_keep_alive();
|
||||
test_parse_http_request();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user