xmlreq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Test cases for the ne_xmlreq.h interface.
  3. Copyright (C) 2005-2006, Joe Orton <[email protected]>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "config.h"
  17. #include <sys/types.h>
  18. #ifdef HAVE_STDLIB_H
  19. #include <stdlib.h>
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include "ne_xmlreq.h"
  25. #include "tests.h"
  26. #include "utils.h"
  27. /* Dummy start_element callback; takes int * userdata and toggles the
  28. * pointed-to int iff the root element has name "hello". Accepts all
  29. * elements. */
  30. static int startelm(void *userdata, int state,
  31. const char *nspace, const char *name,
  32. const char **atts)
  33. {
  34. int *flag = userdata;
  35. if (state == NE_XML_STATEROOT && strcmp(name, "hello") == 0) {
  36. *flag = !*flag;
  37. }
  38. return ++state;
  39. }
  40. static int success(void)
  41. {
  42. ne_session *sess;
  43. ne_request *req;
  44. ne_xml_parser *parser;
  45. int flag = 0;
  46. CALL(make_session(&sess, single_serve_string,
  47. "HTTP/1.1 200 OK\r\n"
  48. "Content-Type: text/xml\r\n"
  49. "Connection: close\r\n" "\r\n"
  50. "<?xml version='1.0' encoding='UTF-8'?>\n"
  51. "<hello/>"));
  52. req = ne_request_create(sess, "PARSE", "/");
  53. parser = ne_xml_create();
  54. ne_xml_push_handler(parser, startelm, NULL, NULL, &flag);
  55. ONREQ(ne_xml_dispatch_request(req, parser));
  56. ONN("XML parser not invoked", !flag);
  57. ne_xml_destroy(parser);
  58. ne_request_destroy(req);
  59. return destroy_and_wait(sess);
  60. }
  61. static int failure(void)
  62. {
  63. ne_session *sess;
  64. ne_request *req;
  65. ne_xml_parser *parser;
  66. CALL(make_session(&sess, single_serve_string,
  67. "HTTP/1.1 200 OK\r\n"
  68. "Content-Type: text/xml\r\n"
  69. "Connection: close\r\n" "\r\n"
  70. "<?xml version='1.0' encoding='UTF-8'?>\n"
  71. "<hello>"));
  72. req = ne_request_create(sess, "PARSE", "/");
  73. parser = ne_xml_create();
  74. ONN("XML parse did not fail",
  75. ne_xml_dispatch_request(req, parser) == NE_OK);
  76. NE_DEBUG(NE_DBG_HTTP, "error string: %s\n", ne_get_error(sess));
  77. ONV(strstr(ne_get_error(sess), "200 OK") != NULL,
  78. ("no error string set on parse error: '%s'", ne_get_error(sess)));
  79. ne_xml_destroy(parser);
  80. ne_request_destroy(req);
  81. return destroy_and_wait(sess);
  82. }
  83. static int types(void)
  84. {
  85. static const struct {
  86. const char *type;
  87. int is_xml;
  88. } ts[] = {
  89. { "text/xml", 1 },
  90. { "tExT/XmL", 1 },
  91. { "text/html", 0 },
  92. { "application/foo+xml", 1 },
  93. { "aPpLiCaTION/FoOOO+xMl", 1 },
  94. { "application/xml", 1 },
  95. { "application/+xml", 0 },
  96. { "application/fish+xml2", 0 },
  97. { "foo/bar+xml", 1 },
  98. { "f/b", 0 },
  99. { "garble garble wotsit", 0 }
  100. };
  101. unsigned n;
  102. for (n = 0; n < sizeof(ts)/sizeof(ts[0]); n++) {
  103. char resp[128];
  104. ne_session *sess;
  105. ne_request *req;
  106. ne_xml_parser *parser;
  107. int flag = 0;
  108. ne_snprintf(resp, sizeof resp,
  109. "HTTP/1.1 200 OK\r\n"
  110. "Content-Type: %s\r\n"
  111. "Connection: close\r\n" "\r\n"
  112. "<?xml version='1.0' encoding='UTF-8'?>\n"
  113. "<hello/>",
  114. ts[n].type);
  115. CALL(make_session(&sess, single_serve_string, resp));
  116. req = ne_request_create(sess, "PARSE", "/");
  117. parser = ne_xml_create();
  118. ne_xml_push_handler(parser, startelm, NULL, NULL, &flag);
  119. ONREQ(ne_xml_dispatch_request(req, parser));
  120. ONV(flag && !ts[n].is_xml,
  121. ("XML parser invoked for non-XML type: %s", ts[n].type));
  122. ONV(!flag && ts[n].is_xml,
  123. ("XML parser not invoked for XML type: %s", ts[n].type));
  124. ne_xml_destroy(parser);
  125. ne_request_destroy(req);
  126. ne_session_destroy(sess);
  127. CALL(await_server());
  128. }
  129. return OK;
  130. }
  131. ne_test tests[] = {
  132. T(success),
  133. T(failure),
  134. T(types),
  135. T(NULL)
  136. };