Statistics
| Branch: | Revision:

peerstreamer-src / Test / router_test.c @ 9eb656e7

History | View | Annotate | Download (2.48 KB)

1
#include <assert.h>
2
#include <stdio.h>
3

    
4
#include <router.h>
5

    
6

    
7
char users_path[] = "/users/ciao";
8
char posts_path[] = "/users/ciao/posts/5";
9

    
10

    
11
void handler_posts(struct mg_connection *nc, struct http_message *hm)
12
{
13
        assert(mg_vcmp(&hm->uri, posts_path) == 0);
14
}
15

    
16
void handler_users(struct mg_connection *nc, struct http_message *hm)
17
{
18
        assert(mg_vcmp(&hm->uri, users_path) == 0);
19
}
20

    
21
void router_destroy_test()
22
{
23
        struct router * r = NULL;
24

    
25
        router_destroy(NULL);
26
        router_destroy(&r);
27
        r = router_create(0);
28
        router_destroy(&r);
29
        r = router_create(100);
30
        router_destroy(&r);
31
        fprintf(stderr,"%s successfully passed!\n",__func__);
32
}
33

    
34
void router_add_route_test()
35
{
36
        struct router * r;
37
        uint8_t res;
38

    
39
        r = router_create(0);
40

    
41
        res = router_add_route(NULL, NULL, NULL, NULL);
42
        assert(res);
43
        res = router_add_route(r, NULL, NULL, NULL);
44
        assert(res);
45
        res = router_add_route(r, "GET", NULL, NULL);
46
        assert(res);
47
        res = router_add_route(r, "GET", "/users/[a-z]+", NULL);
48
        assert(res);
49
        res = router_add_route(NULL, "GET", "/users/[a-z]+", handler_users);
50
        assert(res);
51
        res = router_add_route(r, NULL, "/users/[a-z]+", handler_users);
52
        assert(res);
53
        res = router_add_route(r, "GET", NULL, handler_users);
54
        assert(res);
55
        res = router_add_route(r, "GET", "^/users/[a-z]+$", handler_users);
56
        assert(res == 0);
57
        res = router_add_route(r, "GET", "^/users/[a-z]+/posts/[0-9]+$", handler_posts);
58
        assert(res == 0);
59

    
60
        router_destroy(&r);
61
        fprintf(stderr,"%s successfully passed!\n",__func__);
62
}
63

    
64
void router_handle_test()
65
{
66
        struct router * r = NULL;
67
        struct http_message hm;
68
        char method[] = "GET";
69
        uint8_t res;
70

    
71
        r = router_create(0);
72
        res = router_add_route(r, "GET", "^/users/[a-z]+$", handler_users);
73
        assert(res == 0);
74
        res = router_add_route(r, "GET", "^/users/[a-z]+/posts/[0-9]+$", handler_posts);
75
        assert(res == 0);
76
        hm.method.p = method;
77
        hm.method.len = strlen(method);
78

    
79
        res = router_handle(NULL, NULL, NULL);
80
        assert(res);
81
        res = router_handle(r, NULL, NULL);
82
        assert(res);
83
        res = router_handle(NULL, NULL, &hm);
84
        assert(res);
85

    
86
        hm.uri.p = users_path;
87
        hm.uri.len = strlen(users_path);
88
        res = router_handle(r, NULL, &hm);
89
        assert(res == 0);
90

    
91
        hm.uri.p = posts_path;
92
        hm.uri.len = strlen(posts_path);
93
        res = router_handle(r, NULL, &hm);
94
        assert(res == 0);
95

    
96
        hm.uri.p = "foo";
97
        hm.uri.len = 3;
98
        res = router_handle(r, NULL, &hm);
99
        assert(res);
100

    
101
        router_destroy(&r);
102
        fprintf(stderr,"%s successfully passed!\n",__func__);
103
}
104

    
105
int main(int argc, char ** argv)
106
{
107
        router_destroy_test();
108
        router_add_route_test();
109
        router_handle_test();
110
        return 0;
111
}
112