Add test for blanks in URL path
[yaz-moved-to-github.git] / test / test_solr.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 <yaz/srw.h>
11 #include <yaz/log.h>
12 #if YAZ_HAVE_XML2
13 #include <libxml/parser.h>
14 #endif
15 #include <yaz/test.h>
16 #include <yaz/yaz-version.h>
17 #include <yaz/pquery.h>
18
19 #if YAZ_HAVE_XML2
20 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
21                      const char *charset, const char *expect)
22 {
23     int r;
24     Z_GDU *gdu = 0;
25     YAZ_CHECK(sr);
26
27     if (!sr)
28         return 0;
29
30     gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
31     YAZ_CHECK(gdu);
32     if (!gdu)
33         return 0;
34
35     yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
36
37     r = z_GDU(odr, &gdu, 0, 0);
38     YAZ_CHECK(r);
39     if (r)
40     {
41         int len = 0;
42         char *buf = odr_getbuf(odr, &len, 0);
43
44         if (buf)
45         {
46             if (len == strlen(expect) && !memcmp(buf, expect, len))
47             {
48                 odr_reset(odr);
49                 return 1;
50             }
51             yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
52             yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
53          }
54     }
55     odr_reset(odr);
56     return 0;
57 }
58 #endif
59
60 void tst_encoding(void)
61 {
62 #if YAZ_HAVE_XML2
63     ODR odr = odr_createmem(ODR_ENCODE);
64
65     {
66         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
67                                         "1.2");
68
69         sr->u.request->query = "title:solr";
70         YAZ_CHECK(compare_solr_req(
71                       odr, sr, 0,
72                       "GET Default/select?q=title%3Asolr HTTP/1.1\r\n"
73                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
74                       "Host: localhost\r\n"
75                       "Content-Type: text/xml\r\n\r\n"));
76     }
77
78     {
79         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
80                                         "1.2");
81         sr->u.request->query = "title:solr";
82         YAZ_CHECK(compare_solr_req(
83                       odr, sr, "utf-8",
84                       "GET Default/select?q=title%3Asolr HTTP/1.1\r\n"
85                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
86                       "Host: localhost\r\n"
87                       "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
88     }
89
90     {
91         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
92                                         "1.2");
93
94         sr->u.request->query = "title:solr";
95         sr->u.request->startRecord = odr_intdup(odr, 3);
96         sr->u.request->maximumRecords = odr_intdup(odr, 10);
97
98         YAZ_CHECK(compare_solr_req(
99                       odr, sr, 0,
100                       "GET Default/select?q=title%3Asolr&start=2&rows=10"
101                       " HTTP/1.1\r\n"
102                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
103                       "Host: localhost\r\n"
104                       "Content-Type: text/xml\r\n\r\n"));
105     }
106
107     {
108         Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
109                                         "1.2");
110
111         sr->u.request->query = "title:solr";
112         sr->u.request->startRecord = odr_intdup(odr, 3);
113         sr->u.request->maximumRecords = odr_intdup(odr, 10);
114         sr->u.request->facetList = yaz_pqf_parse_facet_list(
115             odr, "@attr 1=date @attr 2=0, @attr 1=title_exact @attr 3=17");
116
117         YAZ_CHECK(compare_solr_req(
118                       odr, sr, 0,
119                       "GET Default/select?q=title%3Asolr&start=2&rows=10"
120                       "&facet=true&facet.mincount=1&facet.field=date"
121                       "&facet.field=title_exact&f.title_exact.facet.limit=17"
122                       " HTTP/1.1\r\n"
123                       "User-Agent: YAZ/" YAZ_VERSION "\r\n"
124                       "Host: localhost\r\n"
125                       "Content-Type: text/xml\r\n\r\n"));
126     }
127
128     odr_destroy(odr);
129 /* YAZ_HAVE_XML2 */
130 #endif
131 }
132
133
134 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
135 {
136     int r;
137     Z_GDU *gdu;
138     Z_SRW_PDU *sr_p;
139     char *http_response = odr_malloc(o, strlen(content) + 300);
140
141     strcpy(http_response,
142            "HTTP/1.1 200 OK\r\n"
143            "Last-Modified: Wed, 13 Apr 2011 08:30:59 GMT\r\n"
144            "ETag: \"MjcyMWE5M2JiNDgwMDAwMFNvbHI=\"\r\n"
145            "Content-Type: text/xml; charset=utf-8\r\n");
146     sprintf(http_response + strlen(http_response),
147             "Content-Length: %d\r\n\r\n", (int) strlen(content));
148     strcat(http_response, content);
149
150     odr_setbuf(o, http_response, strlen(http_response), 0);
151
152     r = z_GDU(o, &gdu, 0, 0);
153     if (!r || gdu->which != Z_GDU_HTTP_Response)
154         return 0;
155     r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
156     if (r)
157         return 0;
158     if (sr_p->which != Z_SRW_searchRetrieve_response)
159         return 0;
160     *p = sr_p->u.response;
161     return 1;
162 }
163
164 void tst_decoding(void)
165 {
166 #if YAZ_HAVE_XML2
167     ODR odr = odr_createmem(ODR_DECODE);
168     Z_SRW_searchRetrieveResponse *response;
169
170     YAZ_CHECK(check_response(
171                   odr,
172                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
173                   "<response>\n"
174                   "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
175                   "<int name=\"QTime\">1</int><lst name=\"params\">"
176                   "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
177                   "<str name=\"rows\">0</str></lst>"
178                   "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
179                   "</response>\n", &response));
180     YAZ_CHECK_EQ(*response->numberOfRecords, 91);
181     YAZ_CHECK_EQ(response->num_records, 0);
182     YAZ_CHECK(response->records == 0);
183     YAZ_CHECK_EQ(response->num_diagnostics, 0);
184     YAZ_CHECK(response->diagnostics == 0);
185     YAZ_CHECK(response->nextRecordPosition == 0);
186     YAZ_CHECK(response->facetList == 0);
187
188     odr_reset(odr);
189
190     YAZ_CHECK(
191         check_response(
192             odr,
193             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
194             "<response><lst name=\"responseHeader\">"
195             "<int name=\"status\">0</int><int name=\"QTime\">2</int>"
196             "<lst name=\"params\"><str name=\"facet\">true</str>"
197             "<str name=\"facet.mincount\">1</str><str name=\"start\">0</str>"
198             "<str name=\"q\">@attr 1=title solr</str>"
199             "<str name=\"f.date.facet.limit\">5</str>"
200             "<str name=\"facet.field\">date</str>"
201             "<str name=\"rows\">1</str></lst>"
202             "</lst><result name=\"response\" numFound=\"91000000000\" start=\"0\">"
203             "<doc><str name=\"author\">Alenius, Hans,</str>"
204             "<str name=\"author-date\">1937-</str>"
205             "<str name=\"author-title\"/>"
206             "<arr name=\"date\"><str>1969</str></arr>"
207             "<str name=\"id\">   73857731 </str>"
208             "<arr name=\"lccn\"><str>   73857731 </str></arr>"
209             "<arr name=\"medium\"><str>book</str></arr>"
210             "<arr name=\"medium_exact\"><str>book</str></arr>"
211             "<arr name=\"physical-accomp\"><str/></arr>"
212             "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
213             "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
214             "<arr name=\"physical-format\"><str>illus.</str></arr>"
215             "<arr name=\"physical-specified\"><str/></arr>"
216             "<arr name=\"physical-unitsize\"><str/></arr>"
217             "<arr name=\"physical-unittype\"><str/></arr>"
218             "<arr name=\"publication-date\"><str>1969.</str></arr>"
219             "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
220             "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
221             "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
222             "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
223             "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
224             "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
225             "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
226             "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
227             "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
228             "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
229             "</doc></result><lst name=\"facet_counts\">"
230             "<lst name=\"facet_queries\"/>"
231             "<lst name=\"facet_fields\">"
232             "<lst name=\"date\"><int name=\"1978\">5000000000</int><int name=\"1983\">4</int>"
233             "<int name=\"1987\">4</int><int name=\"1988\">4</int>"
234             "<int name=\"2003\">3</int></lst></lst><lst name=\"facet_dates\"/>"
235             "</lst></response>", &response));
236 #if HAVE_LONG_LONG
237     YAZ_CHECK(*response->numberOfRecords == 91000000000LL);
238 #endif
239     YAZ_CHECK_EQ(response->num_records, 1);
240     YAZ_CHECK(response->records);
241     if (response->records)
242     {
243         const char *doc =
244             "<doc><str name=\"author\">Alenius, Hans,</str>"
245             "<str name=\"author-date\">1937-</str>"
246             "<str name=\"author-title\"/>"
247             "<arr name=\"date\"><str>1969</str></arr>"
248             "<str name=\"id\">   73857731 </str>"
249             "<arr name=\"lccn\"><str>   73857731 </str></arr>"
250             "<arr name=\"medium\"><str>book</str></arr>"
251             "<arr name=\"medium_exact\"><str>book</str></arr>"
252             "<arr name=\"physical-accomp\"><str/></arr>"
253             "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
254             "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
255             "<arr name=\"physical-format\"><str>illus.</str></arr>"
256             "<arr name=\"physical-specified\"><str/></arr>"
257             "<arr name=\"physical-unitsize\"><str/></arr>"
258             "<arr name=\"physical-unittype\"><str/></arr>"
259             "<arr name=\"publication-date\"><str>1969.</str></arr>"
260             "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
261             "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
262             "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
263             "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
264             "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
265             "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
266             "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
267             "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
268             "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
269             "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
270             "</doc>";
271
272         Z_SRW_record *record = response->records;
273
274         YAZ_CHECK(record->recordData_len == strlen(doc) &&
275                   !memcmp(record->recordData_buf, doc, record->recordData_len));
276     }
277     YAZ_CHECK_EQ(response->num_diagnostics, 0);
278     YAZ_CHECK(response->diagnostics == 0);
279     YAZ_CHECK(response->nextRecordPosition == 0);
280
281     YAZ_CHECK(response->facetList);
282     if (response->facetList)
283     {
284         Z_FacetList *facetList = response->facetList;
285
286         YAZ_CHECK(facetList->num == 1);
287         if (facetList->num == 1)
288         {
289             Z_FacetField *facetField = facetList->elements[0];
290             int i;
291
292             YAZ_CHECK(facetField->num_terms == 5);
293             if (facetField->num_terms == 5)
294             {
295                 for (i = 0; i < facetField->num_terms; i++)
296                 {
297                     YAZ_CHECK(
298                         facetField->terms[i] &&
299                         facetField->terms[i]->term &&
300                         facetField->terms[i]->term->which == Z_Term_general);
301                 }
302 #if HAVE_LONG_LONG
303                 YAZ_CHECK(*facetField->terms[0]->count == 5000000000LL);
304 #endif
305                 YAZ_CHECK(facetField->terms[0]->term->u.general->len == 4
306                           && !memcmp(facetField->terms[0]->term->u.general->buf,
307                                      "1978", 4));
308                 YAZ_CHECK(*facetField->terms[1]->count == 4);
309                 YAZ_CHECK(facetField->terms[1]->term->u.general->len == 4
310                           && !memcmp(facetField->terms[1]->term->u.general->buf,
311                                      "1983", 4));
312                 YAZ_CHECK(*facetField->terms[2]->count == 4);
313                 YAZ_CHECK(facetField->terms[2]->term->u.general->len == 4
314                           && !memcmp(facetField->terms[2]->term->u.general->buf,
315                                      "1987", 4));
316                 YAZ_CHECK(*facetField->terms[3]->count == 4);
317                 YAZ_CHECK(facetField->terms[3]->term->u.general->len == 4
318                           && !memcmp(facetField->terms[3]->term->u.general->buf,
319                                      "1988", 4));
320                 YAZ_CHECK(*facetField->terms[4]->count == 3);
321                 YAZ_CHECK(facetField->terms[4]->term->u.general->len == 4
322                           && !memcmp(facetField->terms[4]->term->u.general->buf,
323                                      "2003", 4));
324             }
325         }
326     }
327
328     odr_reset(odr);
329
330     odr_destroy(odr);
331 #endif
332 }
333
334 void tst_yaz_700(void)
335 {
336     ODR odr = odr_createmem(ODR_ENCODE);
337     int r;
338     const char *url =
339         "http://localhost:9036/XXX/cproxydebug-7/node102/p/105/c=content_connector"
340         "a=usr/pw#&? r=cfusr/cfpw p=1.2.3.4:80/www.indexdata.com/staff/";
341     int use_full_host = 0;
342     Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, url, 0, use_full_host);
343     Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
344     hreq->method = "GET";
345
346     hreq->content_buf = odr_strdup(odr, "");
347     hreq->content_len = 0;
348
349     r = z_GDU(odr, &gdu_req, 0, 0);
350     YAZ_CHECK(r);
351     if (r)
352     {
353         int len;
354         char *buf = odr_getbuf(odr, &len, 0);
355         ODR decode = odr_createmem(ODR_DECODE);
356         YAZ_CHECK(buf);
357         if (buf)
358         {
359             odr_setbuf(decode, buf, len, 0);
360             r = z_GDU(decode, &gdu_req, 0, 0);
361             YAZ_CHECK(r);
362         }
363         odr_destroy(decode);
364     }
365     odr_destroy(odr);
366 }
367
368
369 int main(int argc, char **argv)
370 {
371     YAZ_CHECK_INIT(argc, argv);
372 #if YAZ_HAVE_XML2
373     LIBXML_TEST_VERSION;
374 #endif
375     tst_encoding();
376     tst_decoding();
377     tst_yaz_700();
378     YAZ_CHECK_TERM;
379 }
380
381
382 /*
383  * Local variables:
384  * c-basic-offset: 4
385  * c-file-style: "Stroustrup"
386  * indent-tabs-mode: nil
387  * End:
388  * vim: shiftwidth=4 tabstop=8 expandtab
389  */
390