Merge branch 'master' into sru_2_0
[yaz-moved-to-github.git] / test / test_zgdu.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12
13 #include <yaz/test.h>
14 #include <yaz/comstack.h>
15 #include <yaz/tcpip.h>
16 #include <yaz/zgdu.h>
17
18 static void tst_http_response(void)
19 {
20     /* response, content  */
21     const char *http_buf =
22         /*123456789012345678 */
23         "HTTP/1.1 200 OK\r\n"
24         "Content-Length: 2\r\n"
25         "\r\n"
26         "12";
27
28     int r;
29     Z_GDU *zgdu;
30     ODR enc = odr_createmem(ODR_ENCODE);
31     ODR dec = odr_createmem(ODR_DECODE);
32     odr_setbuf(dec, (char *) http_buf, strlen(http_buf), 0);
33     r = z_GDU(dec, &zgdu, 0, 0);
34     YAZ_CHECK(r);
35     if (r)
36     {
37         char *http_buf1;
38         int http_len1;
39         YAZ_CHECK_EQ(zgdu->which, Z_GDU_HTTP_Response);
40
41         zgdu->u.HTTP_Response->content_len = 1;
42         /* we now have Content-Length=2, but content_len=1 */
43         z_GDU(enc, &zgdu, 0, 0);
44         http_buf1 = odr_getbuf(enc, &http_len1, 0);
45         YAZ_CHECK(http_buf1);
46         if (http_buf1)
47         {
48             const char *http_buf2 =
49                 /*123456789012345678 */
50                 "HTTP/1.1 200 OK\r\n"
51                 "Content-Length: 1\r\n" /* content_len takes precedence */
52                 "\r\n"
53                 "1";
54             YAZ_CHECK_EQ(http_len1, strlen(http_buf2));
55             YAZ_CHECK(http_len1 == strlen(http_buf2) &&
56                       memcmp(http_buf1, http_buf2, http_len1) == 0);
57         }
58     }
59     odr_destroy(enc);
60     odr_destroy(dec);
61 }
62
63
64 int main (int argc, char **argv)
65 {
66     YAZ_CHECK_INIT(argc, argv);
67     YAZ_CHECK_LOG();
68     tst_http_response();
69     YAZ_CHECK_TERM;
70 }
71
72 /*
73  * Local variables:
74  * c-basic-offset: 4
75  * c-file-style: "Stroustrup"
76  * indent-tabs-mode: nil
77  * End:
78  * vim: shiftwidth=4 tabstop=8 expandtab
79  */
80