unit_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "mongoose.c"
  2. static void test_should_keep_alive(void) {
  3. struct mg_connection conn;
  4. struct mg_context ctx;
  5. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  6. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  7. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  8. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  9. memset(&conn, 0, sizeof(conn));
  10. conn.ctx = &ctx;
  11. parse_http_request(req1, &conn.request_info);
  12. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  13. assert(should_keep_alive(&conn) == 0);
  14. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  15. assert(should_keep_alive(&conn) == 1);
  16. conn.must_close = 1;
  17. assert(should_keep_alive(&conn) == 0);
  18. conn.must_close = 0;
  19. parse_http_request(req2, &conn.request_info);
  20. assert(should_keep_alive(&conn) == 0);
  21. parse_http_request(req3, &conn.request_info);
  22. assert(should_keep_alive(&conn) == 0);
  23. parse_http_request(req4, &conn.request_info);
  24. assert(should_keep_alive(&conn) == 1);
  25. conn.request_info.status_code = 401;
  26. assert(should_keep_alive(&conn) == 0);
  27. conn.request_info.status_code = 200;
  28. conn.must_close = 1;
  29. assert(should_keep_alive(&conn) == 0);
  30. }
  31. static void test_match_prefix(void) {
  32. assert(match_prefix("/a/", 3, "/a/b/c") == 3);
  33. assert(match_prefix("/a/", 3, "/ab/c") == -1);
  34. assert(match_prefix("/*/", 3, "/ab/c") == 4);
  35. assert(match_prefix("**", 2, "/a/b/c") == 6);
  36. assert(match_prefix("/*", 2, "/a/b/c") == 2);
  37. assert(match_prefix("*/*", 3, "/a/b/c") == 2);
  38. assert(match_prefix("**/", 3, "/a/b/c") == 5);
  39. assert(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  40. assert(match_prefix("a|b|cd", 6, "cdef") == 2);
  41. assert(match_prefix("a|b|c?", 6, "cdef") == 2);
  42. assert(match_prefix("a|?|cd", 6, "cdef") == 1);
  43. assert(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  44. assert(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  45. assert(match_prefix("**/", 3, "/a/b/c") == 5);
  46. assert(match_prefix("**/$", 4, "/a/b/c") == -1);
  47. assert(match_prefix("**/$", 4, "/a/b/") == 5);
  48. assert(match_prefix("$", 1, "") == 0);
  49. assert(match_prefix("$", 1, "x") == -1);
  50. assert(match_prefix("*$", 2, "x") == 1);
  51. assert(match_prefix("/$", 2, "/") == 1);
  52. assert(match_prefix("**/$", 4, "/a/b/c") == -1);
  53. assert(match_prefix("**/$", 4, "/a/b/") == 5);
  54. assert(match_prefix("*", 1, "/hello/") == 0);
  55. assert(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  56. assert(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  57. assert(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  58. }
  59. static void test_remove_double_dots() {
  60. struct { char before[20], after[20]; } data[] = {
  61. {"////a", "/a"},
  62. {"/.....", "/."},
  63. {"/......", "/"},
  64. {"...", "..."},
  65. {"/...///", "/./"},
  66. {"/a...///", "/a.../"},
  67. {"/.x", "/.x"},
  68. #if defined(_WIN32)
  69. {"/\\", "/"},
  70. #else
  71. {"/\\", "/\\"},
  72. #endif
  73. {"/a\\", "/a\\"},
  74. };
  75. size_t i;
  76. for (i = 0; i < ARRAY_SIZE(data); i++) {
  77. //printf("[%s] -> [%s]\n", data[i].before, data[i].after);
  78. remove_double_dots_and_double_slashes(data[i].before);
  79. assert(strcmp(data[i].before, data[i].after) == 0);
  80. }
  81. }
  82. int main(void) {
  83. test_match_prefix();
  84. test_remove_double_dots();
  85. test_should_keep_alive();
  86. return 0;
  87. }