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);
157 r = z_GDU(o, &gdu, 0, 0);
158 if (!r || gdu->which != Z_GDU_HTTP_Response)
160 r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
163 if (sr_p->which != Z_SRW_searchRetrieve_response)
165 *p = sr_p->u.response;
169 void tst_decoding(void)
172 ODR odr = odr_createmem(ODR_DECODE);
173 Z_SRW_searchRetrieveResponse *response;
175 YAZ_CHECK(check_response(
177 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
179 "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
180 "<int name=\"QTime\">1</int><lst name=\"params\">"
181 "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
182 "<str name=\"rows\">0</str></lst>"
183 "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
184 "</response>\n", &response));
187 YAZ_CHECK_EQ(*response->numberOfRecords, 91);
188 YAZ_CHECK_EQ(response->num_records, 0);
189 YAZ_CHECK(response->records == 0);
190 YAZ_CHECK_EQ(response->num_diagnostics, 0);
191 YAZ_CHECK(response->diagnostics == 0);
192 YAZ_CHECK(response->nextRecordPosition == 0);
193 YAZ_CHECK(response->facetList == 0);
200 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
201 "<response><lst name=\"responseHeader\">"
202 "<int name=\"status\">0</int><int name=\"QTime\">2</int>"
203 "<lst name=\"params\"><str name=\"facet\">true</str>"
204 "<str name=\"facet.mincount\">1</str><str name=\"start\">0</str>"
205 "<str name=\"q\">@attr 1=title solr</str>"
206 "<str name=\"f.date.facet.limit\">5</str>"
207 "<str name=\"facet.field\">date</str>"
208 "<str name=\"rows\">1</str></lst>"
209 "</lst><result name=\"response\" numFound=\"91000000000\" start=\"0\">"
210 "<doc><str name=\"author\">Alenius, Hans,</str>"
211 "<str name=\"author-date\">1937-</str>"
212 "<str name=\"author-title\"/>"
213 "<arr name=\"date\"><str>1969</str></arr>"
214 "<str name=\"id\"> 73857731 </str>"
215 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
216 "<arr name=\"medium\"><str>book</str></arr>"
217 "<arr name=\"medium_exact\"><str>book</str></arr>"
218 "<arr name=\"physical-accomp\"><str/></arr>"
219 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
220 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
221 "<arr name=\"physical-format\"><str>illus.</str></arr>"
222 "<arr name=\"physical-specified\"><str/></arr>"
223 "<arr name=\"physical-unitsize\"><str/></arr>"
224 "<arr name=\"physical-unittype\"><str/></arr>"
225 "<arr name=\"publication-date\"><str>1969.</str></arr>"
226 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
227 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
228 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
229 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
230 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
231 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
232 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
233 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
234 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
235 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
236 "</doc></result><lst name=\"facet_counts\">"
237 "<lst name=\"facet_queries\"/>"
238 "<lst name=\"facet_fields\">"
239 "<lst name=\"date\"><int name=\"1978\">5000000000</int><int name=\"1983\">4</int>"
240 "<int name=\"1987\">4</int><int name=\"1988\">4</int>"
241 "<int name=\"2003\">3</int></lst></lst><lst name=\"facet_dates\"/>"
242 "</lst></response>", &response));
246 YAZ_CHECK(*response->numberOfRecords == 91000000000LL);
248 YAZ_CHECK_EQ(response->num_records, 1);
249 YAZ_CHECK(response->records);
251 if (response && response->records)
254 "<doc><str name=\"author\">Alenius, Hans,</str>"
255 "<str name=\"author-date\">1937-</str>"
256 "<str name=\"author-title\"/>"
257 "<arr name=\"date\"><str>1969</str></arr>"
258 "<str name=\"id\"> 73857731 </str>"
259 "<arr name=\"lccn\"><str> 73857731 </str></arr>"
260 "<arr name=\"medium\"><str>book</str></arr>"
261 "<arr name=\"medium_exact\"><str>book</str></arr>"
262 "<arr name=\"physical-accomp\"><str/></arr>"
263 "<arr name=\"physical-dimensions\"><str>20 cm.</str></arr>"
264 "<arr name=\"physical-extent\"><str>140, (1) p.</str></arr>"
265 "<arr name=\"physical-format\"><str>illus.</str></arr>"
266 "<arr name=\"physical-specified\"><str/></arr>"
267 "<arr name=\"physical-unitsize\"><str/></arr>"
268 "<arr name=\"physical-unittype\"><str/></arr>"
269 "<arr name=\"publication-date\"><str>1969.</str></arr>"
270 "<arr name=\"publication-name\"><str>Norstedt,</str></arr>"
271 "<arr name=\"publication-place\"><str>Stockholm,</str></arr>"
272 "<arr name=\"subject\"><str>Photography</str><str>Artistic</str></arr>"
273 "<arr name=\"subject-long\"><str>Photography, Artistic.</str></arr>"
274 "<arr name=\"subject_exact\"><str>Photography</str><str>Artistic</str></arr>"
275 "<arr name=\"system-control-nr\"><str>(OCoLC)36247690</str></arr>"
276 "<str name=\"title\">Solring.</str><str name=\"title-complete\">Solring.</str>"
277 "<str name=\"title-dates\"/><str name=\"title-medium\"/>"
278 "<str name=\"title-number-section\"/><str name=\"title-remainder\"/>"
279 "<str name=\"title-responsibility\"/><str name=\"title_exact\">Solring.</str>"
282 Z_SRW_record *record = response->records;
284 YAZ_CHECK(record->recordData_len == strlen(doc) &&
285 !memcmp(record->recordData_buf, doc, record->recordData_len));
289 YAZ_CHECK_EQ(response->num_diagnostics, 0);
290 YAZ_CHECK(response->diagnostics == 0);
291 YAZ_CHECK(response->nextRecordPosition == 0);
292 YAZ_CHECK(response->facetList);
294 if (response && response->facetList)
296 Z_FacetList *facetList = response->facetList;
298 YAZ_CHECK(facetList->num == 1);
299 if (facetList->num == 1)
301 Z_FacetField *facetField = facetList->elements[0];
304 YAZ_CHECK(facetField->num_terms == 5);
305 if (facetField->num_terms == 5)
307 for (i = 0; i < facetField->num_terms; i++)
310 facetField->terms[i] &&
311 facetField->terms[i]->term &&
312 facetField->terms[i]->term->which == Z_Term_general);
315 YAZ_CHECK(*facetField->terms[0]->count == 5000000000LL);
317 YAZ_CHECK(facetField->terms[0]->term->u.general->len == 4
318 && !memcmp(facetField->terms[0]->term->u.general->buf,
320 YAZ_CHECK(*facetField->terms[1]->count == 4);
321 YAZ_CHECK(facetField->terms[1]->term->u.general->len == 4
322 && !memcmp(facetField->terms[1]->term->u.general->buf,
324 YAZ_CHECK(*facetField->terms[2]->count == 4);
325 YAZ_CHECK(facetField->terms[2]->term->u.general->len == 4
326 && !memcmp(facetField->terms[2]->term->u.general->buf,
328 YAZ_CHECK(*facetField->terms[3]->count == 4);
329 YAZ_CHECK(facetField->terms[3]->term->u.general->len == 4
330 && !memcmp(facetField->terms[3]->term->u.general->buf,
332 YAZ_CHECK(*facetField->terms[4]->count == 3);
333 YAZ_CHECK(facetField->terms[4]->term->u.general->len == 4
334 && !memcmp(facetField->terms[4]->term->u.general->buf,
346 void tst_yaz_700(void)
348 ODR odr = odr_createmem(ODR_ENCODE);
351 "http://localhost:9036/XXX/cproxydebug-7/node102/p/105/c=content_connector"
352 "a=usr/pw#&? r=cfusr/cfpw p=1.2.3.4:80/www.indexdata.com/staff/";
353 int use_full_host = 0;
354 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr, url, 0, use_full_host);
355 Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
356 hreq->method = "GET";
358 hreq->content_buf = odr_strdup(odr, "");
359 hreq->content_len = 0;
361 r = z_GDU(odr, &gdu_req, 0, 0);
366 char *buf = odr_getbuf(odr, &len, 0);
367 ODR decode = odr_createmem(ODR_DECODE);
371 odr_setbuf(decode, buf, len, 0);
372 r = z_GDU(decode, &gdu_req, 0, 0);
381 int main(int argc, char **argv)
383 YAZ_CHECK_INIT(argc, argv);
397 * c-file-style: "Stroustrup"
398 * indent-tabs-mode: nil
400 * vim: shiftwidth=4 tabstop=8 expandtab