1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
12 #include <libxml/parser.h>
15 #include <yaz/yaz-version.h>
16 #include <yaz/pquery.h>
19 int compare_solr_req(ODR odr, Z_SRW_PDU *sr,
20 const char *charset, const char *expect)
29 gdu = z_get_HTTP_Request_host_path(odr, "localhost", "Default");
34 yaz_solr_encode_request(gdu->u.HTTP_Request, sr, odr, charset);
36 r = z_GDU(odr, &gdu, 0, 0);
41 char *buf = odr_getbuf(odr, &len, 0);
45 if (len == strlen(expect) && !memcmp(buf, expect, len))
50 yaz_log(YLOG_WARN, "Expect:\n%s\n", expect);
51 yaz_log(YLOG_WARN, "Got:\n%.*s\n", len, buf);
59 void tst_encoding(void)
62 ODR odr = odr_createmem(ODR_ENCODE);
65 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
68 YAZ_CHECK(compare_solr_req(
70 "GET Default/select? HTTP/1.1\r\n"
71 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
73 "Content-Type: text/xml\r\n\r\n"));
77 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
79 YAZ_CHECK(compare_solr_req(
81 "GET Default/select? HTTP/1.1\r\n"
82 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
84 "Content-Type: text/xml; charset=utf-8\r\n\r\n"));
88 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
91 sr->u.request->query_type = Z_SRW_query_type_cql;
92 sr->u.request->query.cql = "title:solr";
93 sr->u.request->startRecord = odr_intdup(odr, 3);
94 sr->u.request->maximumRecords = odr_intdup(odr, 10);
96 YAZ_CHECK(compare_solr_req(
98 "GET Default/select?q=title%3Asolr&start=2&rows=10"
100 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
101 "Host: localhost\r\n"
102 "Content-Type: text/xml\r\n\r\n"));
106 Z_SRW_PDU *sr = yaz_srw_get_pdu(odr, Z_SRW_searchRetrieve_request,
109 sr->u.request->query_type = Z_SRW_query_type_cql;
110 sr->u.request->query.cql = "title:solr";
111 sr->u.request->startRecord = odr_intdup(odr, 3);
112 sr->u.request->maximumRecords = odr_intdup(odr, 10);
113 sr->u.request->facetList = yaz_pqf_parse_facet_list(
114 odr, "@attr 1=date @attr 2=0, @attr 1=title_exact @attr 3=17");
116 YAZ_CHECK(compare_solr_req(
118 "GET Default/select?q=title%3Asolr&start=2&rows=10"
119 "&facet=true&facet.mincount=1&facet.field=date"
120 "&facet.field=title_exact&f.title_exact.facet.limit=17"
122 "User-Agent: YAZ/" YAZ_VERSION "\r\n"
123 "Host: localhost\r\n"
124 "Content-Type: text/xml\r\n\r\n"));
133 int check_response(ODR o, const char *content, Z_SRW_searchRetrieveResponse **p)
138 char *http_response = odr_malloc(o, strlen(content) + 300);
140 strcpy(http_response,
141 "HTTP/1.1 200 OK\r\n"
142 "Last-Modified: Wed, 13 Apr 2011 08:30:59 GMT\r\n"
143 "ETag: \"MjcyMWE5M2JiNDgwMDAwMFNvbHI=\"\r\n"
144 "Content-Type: text/xml; charset=utf-8\r\n");
145 sprintf(http_response + strlen(http_response),
146 "Content-Length: %d\r\n\r\n", (int) strlen(content));
147 strcat(http_response, content);
149 odr_setbuf(o, http_response, strlen(http_response), 0);
151 r = z_GDU(o, &gdu, 0, 0);
152 if (!r || gdu->which != Z_GDU_HTTP_Response)
154 r = yaz_solr_decode_response(o, gdu->u.HTTP_Response, &sr_p);
157 if (sr_p->which != Z_SRW_searchRetrieve_response)
159 *p = sr_p->u.response;
163 void tst_decoding(void)
166 ODR odr = odr_createmem(ODR_DECODE);
168 Z_SRW_searchRetrieveResponse *response;
169 YAZ_CHECK(check_response(
171 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
173 "<lst name=\"responseHeader\"><int name=\"status\">0</int>"
174 "<int name=\"QTime\">1</int><lst name=\"params\">"
175 "<str name=\"start\">0</str><str name=\"q\">@attr 1=title solr</str>"
176 "<str name=\"rows\">0</str></lst>"
177 "</lst><result name=\"response\" numFound=\"91\" start=\"0\"/>\n"
178 "</response>\n", &response));
179 YAZ_CHECK_EQ(*response->numberOfRecords, 91);
180 YAZ_CHECK_EQ(response->num_records, 0);
181 YAZ_CHECK(response->records == 0);
182 YAZ_CHECK_EQ(response->num_diagnostics, 0);
183 YAZ_CHECK(response->diagnostics == 0);
184 YAZ_CHECK(response->nextRecordPosition == 0);
190 int main(int argc, char **argv)
192 YAZ_CHECK_INIT(argc, argv);
205 * c-file-style: "Stroustrup"
206 * indent-tabs-mode: nil
208 * vim: shiftwidth=4 tabstop=8 expandtab