11import { loadSync } from "https://deno.land/std@0.170.0/dotenv/mod.ts" ;
22import {
33 afterAll ,
4- afterEach ,
54 beforeAll ,
65 describe ,
76 it ,
@@ -16,110 +15,14 @@ import {
1615 assertEquals ,
1716 assertMatch ,
1817 assertRejects ,
19- assertThrows ,
2018} from "https://deno.land/std@0.170.0/testing/asserts.ts" ;
21- import {
22- _internals ,
23- buildUrl ,
24- execute ,
25- validateApiKey ,
26- validateTimeout ,
27- } from "../src/utils.ts" ;
28- import { config } from "../src/config.ts" ;
29- import { InvalidTimeoutError , MissingApiKeyError } from "../src/errors.ts" ;
19+ import { _internals , buildUrl , execute } from "../src/utils.ts" ;
3020
3121loadSync ( { export : true } ) ;
3222const BASE_URL = Deno . env . get ( "ENV_TYPE" ) === "local"
3323 ? "http://localhost:3000"
3424 : "https://serpapi.com" ;
3525
36- describe ( "validateApiKey" , ( ) => {
37- afterEach ( ( ) => {
38- config . api_key = null ;
39- } ) ;
40-
41- it ( "with no api_key in config" , ( ) => {
42- assertThrows ( ( ) => validateApiKey ( "" ) , MissingApiKeyError ) ;
43- assertThrows ( ( ) => validateApiKey ( undefined ) , MissingApiKeyError ) ;
44- assertThrows ( ( ) => validateApiKey ( null ) , MissingApiKeyError ) ;
45- assertEquals ( validateApiKey ( " " ) , " " ) ;
46- assertEquals ( validateApiKey ( "asd" ) , "asd" ) ;
47- } ) ;
48-
49- it ( "with api_key in config" , ( ) => {
50- config . api_key = "api_key" ;
51- assertThrows ( ( ) => validateApiKey ( "" ) , MissingApiKeyError ) ;
52- assertEquals ( validateApiKey ( undefined ) , "api_key" ) ;
53- assertEquals ( validateApiKey ( null ) , "api_key" ) ;
54- assertEquals ( validateApiKey ( " " ) , " " ) ;
55- assertEquals ( validateApiKey ( "asd" ) , "asd" ) ;
56- } ) ;
57-
58- it ( "with empty api_key in config" , ( ) => {
59- config . api_key = "" ;
60- assertThrows ( ( ) => validateApiKey ( "" ) , MissingApiKeyError ) ;
61- assertThrows ( ( ) => validateApiKey ( undefined ) , MissingApiKeyError ) ;
62- assertThrows ( ( ) => validateApiKey ( null ) , MissingApiKeyError ) ;
63- assertEquals ( validateApiKey ( "api_key_2" ) , "api_key_2" ) ;
64- assertEquals ( validateApiKey ( " " ) , " " ) ;
65- assertEquals ( validateApiKey ( "asd" ) , "asd" ) ;
66- } ) ;
67-
68- it ( "with no api_key in config and allowNull is true" , ( ) => {
69- assertThrows ( ( ) => validateApiKey ( "" , true ) , MissingApiKeyError ) ;
70- assertThrows ( ( ) => validateApiKey ( undefined , true ) , MissingApiKeyError ) ;
71- assertEquals ( validateApiKey ( null , true ) , undefined ) ;
72- assertEquals ( validateApiKey ( " " , true ) , " " ) ;
73- assertEquals ( validateApiKey ( "asd" , true ) , "asd" ) ;
74- } ) ;
75-
76- it ( "with api_key in config and allowNull is true" , ( ) => {
77- config . api_key = "api_key" ;
78- assertThrows ( ( ) => validateApiKey ( "" , true ) , MissingApiKeyError ) ;
79- assertEquals ( validateApiKey ( undefined , true ) , "api_key" ) ;
80- assertEquals ( validateApiKey ( null , true ) , undefined ) ;
81- assertEquals ( validateApiKey ( " " , true ) , " " ) ;
82- assertEquals ( validateApiKey ( "asd" , true ) , "asd" ) ;
83- } ) ;
84-
85- it ( "with empty api_key in config and allowNull is true" , ( ) => {
86- config . api_key = "" ;
87- assertThrows ( ( ) => validateApiKey ( "" , true ) , MissingApiKeyError ) ;
88- assertThrows ( ( ) => validateApiKey ( undefined , true ) , MissingApiKeyError ) ;
89- assertEquals ( validateApiKey ( null , true ) , undefined ) ;
90- assertEquals ( validateApiKey ( " " , true ) , " " ) ;
91- assertEquals ( validateApiKey ( "asd" , true ) , "asd" ) ;
92- } ) ;
93- } ) ;
94-
95- describe ( "validateTimeout" , ( ) => {
96- afterEach ( ( ) => {
97- config . timeout = 60000 ;
98- } ) ;
99-
100- it ( "with invalid timeout" , ( ) => {
101- assertThrows ( ( ) => validateTimeout ( 0 ) , InvalidTimeoutError ) ;
102- assertThrows ( ( ) => validateTimeout ( - 10 ) , InvalidTimeoutError ) ;
103- } ) ;
104-
105- it ( "with timeout set in config" , ( ) => {
106- config . timeout = 10000 ;
107- assertThrows ( ( ) => validateTimeout ( 0 ) , InvalidTimeoutError ) ;
108- assertEquals ( validateTimeout ( undefined ) , 10000 ) ;
109- } ) ;
110-
111- it ( "with invalid timeout set in config" , ( ) => {
112- config . timeout = - 1 ;
113- assertThrows ( ( ) => validateTimeout ( 0 ) , InvalidTimeoutError ) ;
114- assertThrows ( ( ) => validateTimeout ( undefined ) , InvalidTimeoutError ) ;
115- } ) ;
116-
117- it ( "with valid timeout" , ( ) => {
118- assertEquals ( validateTimeout ( 1 ) , 1 ) ;
119- assertEquals ( validateTimeout ( 10000 ) , 10000 ) ;
120- } ) ;
121- } ) ;
122-
12326describe ( "buildUrl" , ( ) => {
12427 let urlStub : Stub ;
12528
0 commit comments