chiark / gitweb /
Shift many cgi_ functions into lib/, and add a test for them. The
[disorder] / lib / t-cgi.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include "test.h"
21 #include "cgi.h"
22
23 static void input_from(const char *s) {
24   FILE *fp = tmpfile();
25   char buffer[64];
26
27   if(fputs(s, fp) < 0 || fflush(fp) < 0)
28     fatal(errno, "writing to temporary file");
29   rewind(fp);
30   xdup2(fileno(fp), 0);
31   lseek(0, 0/*offset*/, SEEK_SET);
32   snprintf(buffer, sizeof buffer, "%zu", strlen(s));
33   setenv("CONTENT_LENGTH", buffer, 1);
34 }
35
36 static void test_cgi(void) {
37   struct dynstr d[1];
38
39   setenv("REQUEST_METHOD", "GET", 1);
40   setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
41   cgi_init();
42   check_string(cgi_get("foo"), "bar");
43   check_string(cgi_get("a"), "b c");
44   check_string(cgi_get("c"), "x~y");
45
46   setenv("REQUEST_METHOD", "POST", 1);
47   unsetenv("QUERY_STRING");
48   input_from("foo=xbar&a=xb+c&c=xx%7ey");
49   cgi_init();
50   check_string(cgi_get("foo"), "xbar");
51   check_string(cgi_get("a"), "xb c");
52   check_string(cgi_get("c"), "xx~y");
53
54   /* TODO multipart/form-data */
55
56   check_string(cgi_sgmlquote("foobar"), "foobar");
57   check_string(cgi_sgmlquote("<wibble>"), "&#60;wibble&#62;");
58   check_string(cgi_sgmlquote("\"&\""), "&#34;&#38;&#34;");
59   check_string(cgi_sgmlquote("\xC2\xA3"), "&#163;");
60
61   dynstr_init(d);
62   cgi_opentag(sink_dynstr(d), "element",
63               "foo", "bar",
64               "foo", "has space",
65               "foo", "has \"quotes\"",
66               (char *)NULL);
67   dynstr_terminate(d);
68   check_string(d->vec, "<element foo=bar foo=\"has space\" foo=\"has &#34;quotes&#34;\">");
69   
70   dynstr_init(d);
71   cgi_opentag(sink_dynstr(d), "element",
72               "foo", (char *)NULL,
73               (char *)NULL);
74   dynstr_terminate(d);
75   check_string(d->vec, "<element foo>");
76   
77   dynstr_init(d);
78   cgi_closetag(sink_dynstr(d), "element");
79   dynstr_terminate(d);
80   check_string(d->vec, "</element>");
81
82   check_string(cgi_makeurl("http://example.com/", (char *)NULL),
83                "http://example.com/");
84   check_string(cgi_makeurl("http://example.com/",
85                            "foo", "bar",
86                            "a", "b c",
87                            "d", "f=g+h",
88                            (char *)NULL),
89                "http://example.com/?foo=bar&a=b%20c&d=f%3dg%2bh");
90   
91 }
92
93 TEST(cgi);
94
95 /*
96 Local Variables:
97 c-basic-offset:2
98 comment-column:40
99 fill-column:79
100 indent-tabs-mode:nil
101 End:
102 */