Add some utility to manage query strings.
[bertos.git] / bertos / net / http_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief HTTP Server test
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include <cfg/compiler.h>
39 #include <cfg/test.h>
40 #include <cfg/debug.h>
41
42 #include <net/http.h>
43
44 #include <string.h>
45
46 static const char get_str[] = "\
47 GET /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
48 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
49 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
50 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
51 Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
52 q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
53
54
55 static const char get_str1[] = "\
56 GET /test/page1";
57
58 static const char get_str2[] = "\
59 GET ";
60
61 static const char get_str3[] = "\
62 GAT /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
63 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
64 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
65 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
66 Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
67 q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
68
69
70 static const char uri[] = "test%5B%5D!@;'%22%5C.%20";
71 static const char uri_check[] = "test[]!@;'\"\\. ";
72
73 static const char uri1[] = "!*'();:@&=%2B%24%2C/?#%5B%5D%3C%3E%7E.%22%7B%7D%7C%5C-%60_%5E%25";
74 static const char uri_check1[] = "!*'();:@&=+$,/?#[]<>~.\"{}|\\-`_^%";
75
76 static char token_str[] = "var1=1&var2=2&var3=3&var4=4";
77 static char token_str1[] = "var1=1&var2=2&=3&var4=";
78
79 int http_testSetup(void)
80 {
81         kdbg_init();
82         return 0;
83 }
84
85 int http_testRun(void)
86 {
87         char name[80];
88         memset(name, 0, sizeof(name));
89         http_getPageName(get_str, sizeof(get_str), name, sizeof(name));
90
91         if (strcmp("test/page1", name))
92         {
93                 kprintf("error 0 %s\n", name);
94                 goto error;
95         }
96
97         http_getPageName(get_str1, sizeof(get_str1), name, sizeof(name));
98
99         if (strcmp("test/page1", name))
100         {
101                 kprintf("error 1 %s\n", name);
102                 goto error;
103         }
104
105
106         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
107
108         if (name[0] != '\0')
109         {
110                 kprintf("error 2 %s\n", name);
111                 goto error;
112         }
113
114
115         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
116
117         if (name[0] != '\0')
118         {
119                 kprintf("error 3 %s\n", name);
120                 goto error;
121         }
122
123
124         char decoded[sizeof(uri)];
125         http_decodeUri(uri,sizeof(uri), decoded, sizeof(uri));
126
127         if (strcmp(decoded, uri_check))
128         {
129                 kprintf("error 4 %s\n", decoded);
130                 goto error;
131         }
132
133         char decoded1[sizeof(uri1)];
134         http_decodeUri(uri1,sizeof(uri1), decoded1, sizeof(uri1));
135
136         if (strcmp(decoded1, uri_check1))
137         {
138                 kprintf("error 5 %s\n", decoded1);
139                 goto error;
140         }
141
142
143         int len = http_tokenizeGetRequest(token_str, sizeof(token_str));
144         if (len != 4)
145         {
146                 kprintf("error 6 len %d expect %d\n", len, 4);
147                 goto error;
148         }
149
150         char value[80];
151         http_getValue(token_str, sizeof(token_str), "var1", value, sizeof(value));
152         if (strcmp(value, "1"))
153         {
154                 kprintf("error 6 value %s expect %s\n", value, "1");
155                 goto error;
156
157         }
158
159         http_getValue(token_str, sizeof(token_str), "var4", value, sizeof(value));
160         if (strcmp(value, "4"))
161         {
162                 kprintf("error 6 value %s expect %s\n", value, "4");
163                 goto error;
164
165         }
166
167
168         len = http_tokenizeGetRequest(token_str1, sizeof(token_str1));
169         if (len != 4)
170         {
171                 kprintf("error 7 len %d expect %d\n", len, 4);
172                 goto error;
173         }
174
175         http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value));
176         if (strcmp(value, "1"))
177         {
178                 kprintf("error 7 value %s expect %s\n", value, "1");
179                 goto error;
180
181         }
182
183         http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value));
184         if (strcmp(value, ""))
185         {
186                 kprintf("error 7 value %s expect %s\n", value, "");
187                 goto error;
188
189         }
190
191         return 0;
192
193 error:
194         kprintf("Error!\n");
195         return -1;
196 }
197
198 int http_testTearDown(void)
199 {
200         return 0;
201 }
202
203 TEST_MAIN(http);