1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
13 #include <libxml/parser.h>
16 #include <yaz/yaz-version.h>
17 #include <yaz/pquery.h>
20 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
21 const char *charset, const char *expect)
30 gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
35 yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
37 r = z_GDU(odr, &gdu, 0, 0);
42 char *buf = odr_getbuf(odr, &len, 0);
46 if (len == strlen(expect) && !memcmp(buf, expect, len))
51 yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
52 yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
60 void tst_encoding(void)
63 ODR odr = odr_createmem(ODR_ENCODE);
66 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
69 sr->u.request->query = "title:solr";
70 YAZ_CHECK(compare_solr_req(
72 "GET Default/select?defType=lucene&q=title%3Asolr "
74 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
76 "Content-Type: text/xml\r\n\r\n"));
80 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
82 sr->u.request->query = "title:solr";
83 YAZ_CHECK(compare_solr_req(
85 "GET Default/select?defType=lucene&q=title%3Asolr "
87 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
89 "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
93 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
96 sr->u.request->query = "title:solr";
97 sr->u.request->startRecord = odr_intdup(odr, 3);
98 sr->u.request->maximumRecords = odr_intdup(odr, 10);
100 YAZ_CHECK(compare_solr_req(
102 "GET Default/select?defType=lucene&q=title%3Asolr&"
105 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
106 "Host: localhost\r\n"
107 "Content-Type: text/xml\r\n\r\n"));
111 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
114 sr->u.request->query = "title:solr";
115 sr->u.request->startRecord = odr_intdup(odr, 3);
116 sr->u.request->maximumRecords = odr_intdup(odr, 10);
117 sr->u.request->facetList = yaz_pqf_parse_facet_list(
118 odr, "@attr 1=date @attr 2=0, @attr 1=title_exact @attr 3=17");
120 YAZ_CHECK(compare_solr_req(
122 "GET Default/select?defType=lucene&q=title%3Asolr&"
124 "&facet=true&facet.mincount=1&facet.field=date"
125 "&facet.field=title_exact&f.title_exact.facet.limit=17"
127 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
128 "Host: localhost\r\n"
129 "Content-Type: text/xml\r\n\r\n"));
138 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
143 char *http_response = odr_malloc(o, strlen(content) + 300);
145 strcpy(http_response,
146 "HTTP/1.1 200 OK\r\n"
147 "Last-Modified: Wed, 13 Apr 2011 08:30:59 GMT\r\n"
148 "ETag: \"MjcyMWE5M2JiNDgwMDAwMFNvbHI=\"\r\n"
149 "Content-Type: text/xml; charset=utf-8\r\n");
150 sprintf(http_response + strlen(http_response),
151 "Content-Length: %d\r\n\r\n", (int) strlen(content));
152 strcat(http_response, content);
154 odr_setbuf(o, http_response, strlen(http_response), 0);
156 r = z_GDU(o, &gdu, 0, 0);
157 if (!r || gdu->which != Z_GDU_HTTP_Response)
159 r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
162 if (sr_p->which != Z_SRW_searchRetrieve_response)
164 *p = sr_p->u.response;
168 void tst_decoding(void)
171 ODR odr = odr_createmem(ODR_DECODE);
172 Z_SRW_searchRetrieveResponse *response;
174 YAZ_CHECK(check_response(
176 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
178 "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
179 "<int name=\"QTime\">1</int><lst name=\"params\">"
180 "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
181 "<str name=\"rows\">0</str></lst>"
182 "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
183 "</response>\n", &response));
184 YAZ_CHECK_EQ(*response->numberOfRecords, 91);
185 YAZ_CHECK_EQ(response->num_records, 0);
186 YAZ_CHECK(response->records == 0);
187 YAZ_CHECK_EQ(response->num_diagnostics, 0);
188 YAZ_CHECK(response->diagnostics == 0);
189 YAZ_CHECK(response->nextRecordPosition == 0);
190 YAZ_CHECK(response->facetList == 0);
197 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
198 "<response><lst name=\"responseHeader\">"
199 "<int name=\"status\">0</int><int name=\"QTime\">2</int>"
200 "<lst name=\"params\"><str name=\"facet\">true</str>"
201 "<str name=\"facet.mincount\">1</str><str name=\"start\">0</str>"
202 "<str name=\"q\">@attr 1=title solr</str>"
203 "<str name=\"f.date.facet.limit\">5</str>"
204 "<str name=\"facet.field\">date</str>"
205 "<str name=\"rows\">1</str></lst>"
206 "</lst><result name=\"response\" numFound=\"91000000000\" start=\"0\">"
207 "<doc><str name=\"author\">Alenius, Hans,</str>"
208 "<str name=\"author-date\">1937-</str>"
209 "<str name=\"author-title\"/>"
210 "<arr name=\"date\"><str>1969</str></arr>"
211 "<str name=\"id\"> 73857731 </str>"
212 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
213 "<arr name=\"medium\"><str>book</str></arr>"
214 "<arr name=\"medium_exact\"><str>book</str></arr>"
215 "<arr name=\"physical-accomp\"><str/></arr>"
216 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
217 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
218 "<arr name=\"physical-format\"><str>illus.</str></arr>"
219 "<arr name=\"physical-specified\"><str/></arr>"
220 "<arr name=\"physical-unitsize\"><str/></arr>"
221 "<arr name=\"physical-unittype\"><str/></arr>"
222 "<arr name=\"publication-date\"><str>1969.</str></arr>"
223 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
224 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
225 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
226 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
227 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
228 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
229 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
230 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
231 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
232 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
233 "</doc></result><lst name=\"facet_counts\">"
234 "<lst name=\"facet_queries\"/>"
235 "<lst name=\"facet_fields\">"
236 "<lst name=\"date\"><int name=\"1978\">5000000000</int><int name=\"1983\">4</int>"
237 "<int name=\"1987\">4</int><int name=\"1988\">4</int>"
238 "<int name=\"2003\">3</int></lst></lst><lst name=\"facet_dates\"/>"
239 "</lst></response>", &response));
241 YAZ_CHECK(*response->numberOfRecords == 91000000000LL);
243 YAZ_CHECK_EQ(response->num_records, 1);
244 YAZ_CHECK(response->records);
245 if (response->records)
248 "<doc><str name=\"author\">Alenius, Hans,</str>"
249 "<str name=\"author-date\">1937-</str>"
250 "<str name=\"author-title\"/>"
251 "<arr name=\"date\"><str>1969</str></arr>"
252 "<str name=\"id\"> 73857731 </str>"
253 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
254 "<arr name=\"medium\"><str>book</str></arr>"
255 "<arr name=\"medium_exact\"><str>book</str></arr>"
256 "<arr name=\"physical-accomp\"><str/></arr>"
257 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
258 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
259 "<arr name=\"physical-format\"><str>illus.</str></arr>"
260 "<arr name=\"physical-specified\"><str/></arr>"
261 "<arr name=\"physical-unitsize\"><str/></arr>"
262 "<arr name=\"physical-unittype\"><str/></arr>"
263 "<arr name=\"publication-date\"><str>1969.</str></arr>"
264 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
265 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
266 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
267 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
268 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
269 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
270 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
271 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
272 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
273 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
276 Z_SRW_record *record = response->records;
278 YAZ_CHECK(record->recordData_len == strlen(doc) &&
279 !memcmp(record->recordData_buf, doc, record->recordData_len));
281 YAZ_CHECK_EQ(response->num_diagnostics, 0);
282 YAZ_CHECK(response->diagnostics == 0);
283 YAZ_CHECK(response->nextRecordPosition == 0);
285 YAZ_CHECK(response->facetList);
286 if (response->facetList)
288 Z_FacetList *facetList = response->facetList;
290 YAZ_CHECK(facetList->num == 1);
291 if (facetList->num == 1)
293 Z_FacetField *facetField = facetList->elements[0];
296 YAZ_CHECK(facetField->num_terms == 5);
297 if (facetField->num_terms == 5)
299 for (i = 0; i < facetField->num_terms; i++)
302 facetField->terms[i] &&
303 facetField->terms[i]->term &&
304 facetField->terms[i]->term->which == Z_Term_general);
307 YAZ_CHECK(*facetField->terms[0]->count == 5000000000LL);
309 YAZ_CHECK(facetField->terms[0]->term->u.general->len == 4
310 && !memcmp(facetField->terms[0]->term->u.general->buf,
312 YAZ_CHECK(*facetField->terms[1]->count == 4);
313 YAZ_CHECK(facetField->terms[1]->term->u.general->len == 4
314 && !memcmp(facetField->terms[1]->term->u.general->buf,
316 YAZ_CHECK(*facetField->terms[2]->count == 4);
317 YAZ_CHECK(facetField->terms[2]->term->u.general->len == 4
318 && !memcmp(facetField->terms[2]->term->u.general->buf,
320 YAZ_CHECK(*facetField->terms[3]->count == 4);
321 YAZ_CHECK(facetField->terms[3]->term->u.general->len == 4
322 && !memcmp(facetField->terms[3]->term->u.general->buf,
324 YAZ_CHECK(*facetField->terms[4]->count == 3);
325 YAZ_CHECK(facetField->terms[4]->term->u.general->len == 4
326 && !memcmp(facetField->terms[4]->term->u.general->buf,
338 void tst_yaz_700(void)
340 ODR odr = odr_createmem(ODR_ENCODE);
343 "http://localhost:9036/XXX/cproxydebug-7/node102/p/105/c=content_connector"
344 "a=usr/pw#&? r=cfusr/cfpw p=1.2.3.4:80/www.indexdata.com/staff/";
345 int use_full_host = 0;
346 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, url, 0, use_full_host);
347 Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
348 hreq->method = "GET";
350 hreq->content_buf = odr_strdup(odr, "");
351 hreq->content_len = 0;
353 r = z_GDU(odr, &gdu_req, 0, 0);
358 char *buf = odr_getbuf(odr, &len, 0);
359 ODR decode = odr_createmem(ODR_DECODE);
363 odr_setbuf(decode, buf, len, 0);
364 r = z_GDU(decode, &gdu_req, 0, 0);
373 int main(int argc, char **argv)
375 YAZ_CHECK_INIT(argc, argv);
389 * c-file-style: "Stroustrup"
390 * indent-tabs-mode: nil
392 * vim: shiftwidth=4 tabstop=8 expandtab