2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
23 static void input_from(const char *s) {
27 if(fputs(s, fp) < 0 || fflush(fp) < 0)
28 fatal(errno, "writing to temporary file");
31 lseek(0, 0/*offset*/, SEEK_SET);
32 snprintf(buffer, sizeof buffer, "%zu", strlen(s));
33 setenv("CONTENT_LENGTH", buffer, 1);
36 static void test_cgi(void) {
39 setenv("REQUEST_METHOD", "GET", 1);
40 setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
42 check_string(cgi_get("foo"), "bar");
43 check_string(cgi_get("a"), "b c");
44 check_string(cgi_get("c"), "x~y");
46 setenv("REQUEST_METHOD", "POST", 1);
47 unsetenv("QUERY_STRING");
48 input_from("foo=xbar&a=xb+c&c=xx%7ey");
50 check_string(cgi_get("foo"), "xbar");
51 check_string(cgi_get("a"), "xb c");
52 check_string(cgi_get("c"), "xx~y");
54 /* TODO multipart/form-data */
56 check_string(cgi_sgmlquote("foobar"), "foobar");
57 check_string(cgi_sgmlquote("<wibble>"), "<wibble>");
58 check_string(cgi_sgmlquote("\"&\""), ""&"");
59 check_string(cgi_sgmlquote("\xC2\xA3"), "£");
62 cgi_opentag(sink_dynstr(d), "element",
65 "foo", "has \"quotes\"",
68 check_string(d->vec, "<element foo=bar foo=\"has space\" foo=\"has "quotes"\">");
71 cgi_opentag(sink_dynstr(d), "element",
75 check_string(d->vec, "<element foo>");
78 cgi_closetag(sink_dynstr(d), "element");
80 check_string(d->vec, "</element>");
82 check_string(cgi_makeurl("http://example.com/", (char *)NULL),
83 "http://example.com/");
84 check_string(cgi_makeurl("http://example.com/",
89 "http://example.com/?foo=bar&a=b%20c&d=f%3dg%2bh");