summaryrefslogtreecommitdiff
path: root/test/xmlreq.c
blob: 1c3344dce8a39fe2530974c883f9bdadbe368736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 
   Test cases for the ne_xmlreq.h interface.
   Copyright (C) 2005-2006, Joe Orton <joe@manyfish.co.uk>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "config.h"

#include <sys/types.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "ne_xmlreq.h"

#include "tests.h"
#include "utils.h"

/* Dummy start_element callback; takes int * userdata and toggles the
 * pointed-to int iff the root element has name "hello".  Accepts all
 * elements. */
static int startelm(void *userdata, int state,
                    const char *nspace, const char *name,
		    const char **atts)
{
    int *flag = userdata;

    if (state == NE_XML_STATEROOT && strcmp(name, "hello") == 0) {
        *flag = !*flag;
    }

    return ++state;
}

static int success(void)
{
    ne_session *sess;
    ne_request *req;
    ne_xml_parser *parser;
    int flag = 0;

    CALL(make_session(&sess, single_serve_string, 
                      "HTTP/1.1 200 OK\r\n"
                      "Content-Type: text/xml\r\n"
                      "Connection: close\r\n" "\r\n"
                      "<?xml version='1.0' encoding='UTF-8'?>\n"
                      "<hello/>"));
    
    req = ne_request_create(sess, "PARSE", "/");
    parser = ne_xml_create();

    ne_xml_push_handler(parser, startelm, NULL, NULL, &flag);
    
    ONREQ(ne_xml_dispatch_request(req, parser));

    ONN("XML parser not invoked", !flag);
    
    ne_xml_destroy(parser);
    ne_request_destroy(req);
    ne_session_destroy(sess);
    return await_server();
}

static int failure(void)
{
    ne_session *sess;
    ne_request *req;
    ne_xml_parser *parser;
    
    CALL(make_session(&sess, single_serve_string, 
                      "HTTP/1.1 200 OK\r\n"
                      "Content-Type: text/xml\r\n"
                      "Connection: close\r\n" "\r\n"
                      "<?xml version='1.0' encoding='UTF-8'?>\n"
                      "<hello>"));
    
    req = ne_request_create(sess, "PARSE", "/");
    parser = ne_xml_create();
    
    ONN("XML parse did not fail",
        ne_xml_dispatch_request(req, parser) == NE_OK);

    NE_DEBUG(NE_DBG_HTTP, "error string: %s\n", ne_get_error(sess));
    
    ONV(strstr(ne_get_error(sess), "200 OK") != NULL,
        ("no error string set on parse error: '%s'", ne_get_error(sess)));

    ne_xml_destroy(parser);
    ne_request_destroy(req);
    ne_session_destroy(sess);
    return await_server();
}

static int types(void)
{
    static const struct {
        const char *type;
        int is_xml;
    } ts[] = {
        { "text/xml", 1 },
        { "tExT/XmL", 1 },
        { "text/html", 0 },
        { "application/foo+xml", 1 },
        { "aPpLiCaTION/FoOOO+xMl", 1 },
        { "application/xml", 1 },
        { "application/+xml", 0 },
        { "application/fish+xml2", 0 },
        { "foo/bar+xml", 1 },
        { "f/b", 0 },
        { "garble garble wotsit", 0 }
    };
    unsigned n;

    for (n = 0; n < sizeof(ts)/sizeof(ts[0]); n++) {
        char resp[128];
        ne_session *sess;
        ne_request *req;
        ne_xml_parser *parser;
        int flag = 0;

        ne_snprintf(resp, sizeof resp,
                    "HTTP/1.1 200 OK\r\n"
                    "Content-Type: %s\r\n"
                    "Connection: close\r\n" "\r\n"
                    "<?xml version='1.0' encoding='UTF-8'?>\n"
                    "<hello/>",
                    ts[n].type);

        CALL(make_session(&sess, single_serve_string, resp));
    
        req = ne_request_create(sess, "PARSE", "/");
        parser = ne_xml_create();
        
        ne_xml_push_handler(parser, startelm, NULL, NULL, &flag);
        
        ONREQ(ne_xml_dispatch_request(req, parser));
        
        ONV(flag && !ts[n].is_xml,
            ("XML parser invoked for non-XML type: %s", ts[n].type));
        ONV(!flag && ts[n].is_xml,
            ("XML parser not invoked for XML type: %s", ts[n].type));
        
        ne_xml_destroy(parser);
        ne_request_destroy(req);
        ne_session_destroy(sess);
        CALL(await_server());
    }

    return OK;
}

ne_test tests[] = {
    T(success),
    T(failure),
    T(types),
    T(NULL)
};