@@ -8,15 +8,20 @@ import {
88} from "https://deno.land/std@0.170.0/testing/bdd.ts" ;
99import {
1010 assertEquals ,
11+ assertExists ,
1112 assertInstanceOf ,
1213 assertRejects ,
1314} from "https://deno.land/std@0.170.0/testing/asserts.ts" ;
1415import { Stub , stub } from "https://deno.land/std@0.170.0/testing/mock.ts" ;
1516import { _internals } from "../src/utils.ts" ;
1617import {
18+ BaseResponse ,
1719 config ,
1820 getAccount ,
21+ getHtml ,
22+ getJson ,
1923 getLocations ,
24+ InvalidArgumentTypesError ,
2025 InvalidTimeoutError ,
2126 MissingApiKeyError ,
2227} from "../mod.ts" ;
@@ -194,3 +199,258 @@ describe("getLocations", {
194199 assertInstanceOf ( locations , Array ) ;
195200 } ) ;
196201} ) ;
202+
203+ describe ( "getJson" , {
204+ sanitizeOps : false ,
205+ sanitizeResources : false ,
206+ } , ( ) => {
207+ let urlStub : Stub ;
208+
209+ beforeAll ( ( ) => {
210+ urlStub = stub ( _internals , "getBaseUrl" , ( ) => BASE_URL ) ;
211+ } ) ;
212+
213+ afterEach ( ( ) => {
214+ config . api_key = null ;
215+ } ) ;
216+
217+ afterAll ( ( ) => {
218+ urlStub . restore ( ) ;
219+ } ) ;
220+
221+ it ( "with no api_key" , ( ) => {
222+ assertRejects (
223+ async ( ) => await getJson ( { engine : "google" , q : "Paris" } ) ,
224+ MissingApiKeyError ,
225+ ) ;
226+ assertRejects (
227+ async ( ) => await getJson ( "google" , { q : "Paris" } ) ,
228+ MissingApiKeyError ,
229+ ) ;
230+ assertRejects (
231+ // @ts -ignore testing invalid usage
232+ async ( ) => await getJson ( { } ) ,
233+ MissingApiKeyError ,
234+ ) ;
235+ } ) ;
236+
237+ it ( "with invalid arguments" , ( ) => {
238+ assertRejects (
239+ // @ts -ignore testing invalid usage
240+ async ( ) => await getJson ( "google" ) ,
241+ InvalidArgumentTypesError ,
242+ ) ;
243+ assertRejects (
244+ // @ts -ignore testing invalid usage
245+ async ( ) => await getJson ( ) ,
246+ InvalidArgumentTypesError ,
247+ ) ;
248+ } ) ;
249+
250+ it ( "with invalid timeout" , {
251+ ignore : ! HAS_API_KEY ,
252+ } , ( ) => {
253+ config . api_key = SERPAPI_TEST_KEY ;
254+ assertRejects (
255+ async ( ) => await getJson ( { engine : "google" , q : "Paris" , timeout : 0 } ) ,
256+ InvalidTimeoutError ,
257+ ) ;
258+ assertRejects (
259+ async ( ) => await getJson ( { engine : "google" , q : "Paris" , timeout : - 10 } ) ,
260+ InvalidTimeoutError ,
261+ ) ;
262+ assertRejects (
263+ async ( ) => await getJson ( "google" , { q : "Paris" , timeout : 0 } ) ,
264+ InvalidTimeoutError ,
265+ ) ;
266+ assertRejects (
267+ async ( ) => await getJson ( "google" , { q : "Paris" , timeout : - 10 } ) ,
268+ InvalidTimeoutError ,
269+ ) ;
270+ } ) ;
271+
272+ it ( "async/await" , {
273+ ignore : ! HAS_API_KEY ,
274+ } , async ( ) => {
275+ const json = await getJson ( {
276+ engine : "google" ,
277+ q : "Paris" ,
278+ api_key : SERPAPI_TEST_KEY ,
279+ timeout : 10000 ,
280+ } ) ;
281+ assertEquals ( json . search_metadata [ "status" ] , "Success" ) ;
282+ assertExists ( json . organic_results ) ;
283+
284+ // old API
285+ const json2 = await getJson ( "google" , {
286+ q : "Paris" ,
287+ api_key : SERPAPI_TEST_KEY ,
288+ timeout : 10000 ,
289+ } ) ;
290+ assertEquals ( json2 . search_metadata . id , json . search_metadata . id ) ;
291+ } ) ;
292+
293+ it ( "callback" , {
294+ ignore : ! HAS_API_KEY ,
295+ } , async ( ) => {
296+ const json = await new Promise < BaseResponse < "google" > > ( ( done ) => {
297+ getJson ( {
298+ engine : "google" ,
299+ q : "Paris" ,
300+ api_key : SERPAPI_TEST_KEY ,
301+ timeout : 10000 ,
302+ } , done ) ;
303+ } ) ;
304+ assertEquals ( json . search_metadata [ "status" ] , "Success" ) ;
305+ assertExists ( json . organic_results ) ;
306+
307+ // old API
308+ const json2 = await new Promise < BaseResponse < "google" > > ( ( done ) => {
309+ getJson ( "google" , {
310+ q : "Paris" ,
311+ api_key : SERPAPI_TEST_KEY ,
312+ timeout : 10000 ,
313+ } , done ) ;
314+ } ) ;
315+ assertEquals ( json2 . search_metadata . id , json . search_metadata . id ) ;
316+ } ) ;
317+
318+ it ( "rely on global config" , {
319+ ignore : ! HAS_API_KEY ,
320+ } , async ( ) => {
321+ config . api_key = SERPAPI_TEST_KEY ;
322+ const json = await getJson ( {
323+ engine : "google" ,
324+ q : "Paris" ,
325+ timeout : 10000 ,
326+ } ) ;
327+ assertEquals ( json . search_metadata [ "status" ] , "Success" ) ;
328+ assertExists ( json . organic_results ) ;
329+ } ) ;
330+ } ) ;
331+
332+ describe ( "getHtml" , {
333+ sanitizeOps : false ,
334+ sanitizeResources : false ,
335+ } , ( ) => {
336+ let urlStub : Stub ;
337+
338+ beforeAll ( ( ) => {
339+ urlStub = stub ( _internals , "getBaseUrl" , ( ) => BASE_URL ) ;
340+ } ) ;
341+
342+ afterEach ( ( ) => {
343+ config . api_key = null ;
344+ } ) ;
345+
346+ afterAll ( ( ) => {
347+ urlStub . restore ( ) ;
348+ } ) ;
349+
350+ it ( "with no api_key" , ( ) => {
351+ assertRejects (
352+ async ( ) => await getHtml ( { engine : "google" , q : "Paris" } ) ,
353+ MissingApiKeyError ,
354+ ) ;
355+ assertRejects (
356+ async ( ) => await getHtml ( "google" , { q : "Paris" } ) ,
357+ MissingApiKeyError ,
358+ ) ;
359+ assertRejects (
360+ // @ts -ignore testing invalid usage
361+ async ( ) => await getHtml ( { } ) ,
362+ MissingApiKeyError ,
363+ ) ;
364+ } ) ;
365+
366+ it ( "with invalid arguments" , ( ) => {
367+ assertRejects (
368+ // @ts -ignore testing invalid usage
369+ async ( ) => await getHtml ( "google" ) ,
370+ InvalidArgumentTypesError ,
371+ ) ;
372+ assertRejects (
373+ // @ts -ignore testing invalid usage
374+ async ( ) => await getHtml ( ) ,
375+ InvalidArgumentTypesError ,
376+ ) ;
377+ } ) ;
378+
379+ it ( "with invalid timeout" , {
380+ ignore : ! HAS_API_KEY ,
381+ } , ( ) => {
382+ config . api_key = SERPAPI_TEST_KEY ;
383+ assertRejects (
384+ async ( ) => await getHtml ( { engine : "google" , q : "Paris" , timeout : 0 } ) ,
385+ InvalidTimeoutError ,
386+ ) ;
387+ assertRejects (
388+ async ( ) => await getHtml ( { engine : "google" , q : "Paris" , timeout : - 10 } ) ,
389+ InvalidTimeoutError ,
390+ ) ;
391+ assertRejects (
392+ async ( ) => await getHtml ( "google" , { q : "Paris" , timeout : 0 } ) ,
393+ InvalidTimeoutError ,
394+ ) ;
395+ assertRejects (
396+ async ( ) => await getHtml ( "google" , { q : "Paris" , timeout : - 10 } ) ,
397+ InvalidTimeoutError ,
398+ ) ;
399+ } ) ;
400+
401+ it ( "async/await" , {
402+ ignore : ! HAS_API_KEY ,
403+ } , async ( ) => {
404+ const html = await getHtml ( {
405+ engine : "google" ,
406+ q : "Paris" ,
407+ api_key : SERPAPI_TEST_KEY ,
408+ timeout : 10000 ,
409+ } ) ;
410+ assertEquals ( html . includes ( "Paris" ) , true ) ;
411+
412+ // old API
413+ const html2 = await getHtml ( "google" , {
414+ q : "Paris" ,
415+ api_key : SERPAPI_TEST_KEY ,
416+ timeout : 10000 ,
417+ } ) ;
418+ assertEquals ( html2 , html ) ;
419+ } ) ;
420+
421+ it ( "callback" , {
422+ ignore : ! HAS_API_KEY ,
423+ } , async ( ) => {
424+ const html = await new Promise < string > ( ( done ) => {
425+ getHtml ( {
426+ engine : "google" ,
427+ q : "Paris" ,
428+ api_key : SERPAPI_TEST_KEY ,
429+ timeout : 10000 ,
430+ } , done ) ;
431+ } ) ;
432+ assertEquals ( html . includes ( "Paris" ) , true ) ;
433+
434+ // old API
435+ const html2 = await new Promise < string > ( ( done ) => {
436+ getHtml ( "google" , {
437+ q : "Paris" ,
438+ api_key : SERPAPI_TEST_KEY ,
439+ timeout : 10000 ,
440+ } , done ) ;
441+ } ) ;
442+ assertEquals ( html2 , html ) ;
443+ } ) ;
444+
445+ it ( "rely on global config" , {
446+ ignore : ! HAS_API_KEY ,
447+ } , async ( ) => {
448+ config . api_key = SERPAPI_TEST_KEY ;
449+ const html = await getHtml ( {
450+ engine : "google" ,
451+ q : "Paris" ,
452+ timeout : 10000 ,
453+ } ) ;
454+ assertEquals ( html . includes ( "Paris" ) , true ) ;
455+ } ) ;
456+ } ) ;
0 commit comments