@@ -11,13 +11,24 @@ import {
11
11
assertInstanceOf ,
12
12
assertMatch ,
13
13
} from "https://deno.land/[email protected] /testing/asserts.ts" ;
14
- import { _internals , buildUrl , execute , getSource } from "../src/utils.ts" ;
14
+ import {
15
+ _internals ,
16
+ buildRequestOptions ,
17
+ execute ,
18
+ getSource ,
19
+ } from "../src/utils.ts" ;
15
20
import { RequestTimeoutError } from "../src/errors.ts" ;
16
21
17
22
loadSync ( { export : true } ) ;
18
- const BASE_URL = Deno . env . get ( "ENV_TYPE" ) === "local"
19
- ? "http://localhost:3000"
20
- : "https://serpapi.com" ;
23
+ const BASE_OPTIONS = Deno . env . get ( "ENV_TYPE" ) === "local"
24
+ ? {
25
+ hostname : "localhost" ,
26
+ port : 3000 ,
27
+ }
28
+ : {
29
+ hostname : "serpapi.com" ,
30
+ port : 443 ,
31
+ } ;
21
32
22
33
describe ( "getSource" , ( ) => {
23
34
it ( "use runtime version" , async ( ) => {
@@ -28,67 +39,80 @@ describe("getSource", () => {
28
39
} ) ;
29
40
} ) ;
30
41
31
- describe ( "buildUrl " , ( ) => {
42
+ describe ( "buildRequestOptions " , ( ) => {
32
43
let urlStub : Stub ;
33
44
34
45
beforeAll ( ( ) => {
35
- urlStub = stub ( _internals , "getBaseUrl " , ( ) => BASE_URL ) ;
46
+ urlStub = stub ( _internals , "getHostnameAndPort " , ( ) => BASE_OPTIONS ) ;
36
47
} ) ;
37
48
38
49
afterAll ( ( ) => {
39
50
urlStub . restore ( ) ;
40
51
} ) ;
41
52
42
53
it ( "with blank path and empty parameters" , async ( ) => {
43
- assertEquals ( await buildUrl ( "" , { } ) , ` ${ BASE_URL } ?` ) ;
54
+ assertEquals ( await buildRequestOptions ( "" , { } ) , BASE_OPTIONS ) ;
44
55
} ) ;
45
56
46
57
it ( "with path and empty parameters" , async ( ) => {
47
- assertEquals ( await buildUrl ( "/" , { } ) , ` ${ BASE_URL } /?` ) ;
58
+ assertEquals ( await buildRequestOptions ( "/" , { } ) , BASE_OPTIONS ) ;
48
59
} ) ;
49
60
50
61
it ( "with path and parameters" , async ( ) => {
51
62
assertEquals (
52
- await buildUrl ( "/search" , { q : "coffee" , gl : "us" } ) ,
53
- ` ${ BASE_URL } /search?q=coffee&gl=us` ,
63
+ await buildRequestOptions ( "/search" , { q : "coffee" , gl : "us" } ) ,
64
+ BASE_OPTIONS ,
54
65
) ;
55
66
} ) ;
56
67
57
68
it ( "with source" , async ( ) => {
58
- const url = await buildUrl ( "/search" , { source : await getSource ( ) } ) ;
69
+ const options = await buildRequestOptions ( "/search" , {
70
+ source : await getSource ( ) ,
71
+ } ) ;
59
72
assertMatch (
60
- url ,
73
+ options . path as string ,
61
74
/ s o u r c e = ( n o d e j s | d e n o ) % 4 0 \d + \. \d + \. \d + % 2 C s e r p a p i % 4 0 \d + \. \d + \. \d + $ / ,
62
75
) ;
63
76
} ) ;
64
77
65
78
it ( "with undefined parameters" , async ( ) => {
66
79
assertEquals (
67
- await buildUrl ( "/search" , { q : "coffee" , gl : undefined , hl : null } ) ,
68
- `${ BASE_URL } /search?q=coffee&hl=` ,
80
+ await buildRequestOptions ( "/search" , {
81
+ q : "coffee" ,
82
+ gl : undefined ,
83
+ hl : null ,
84
+ } ) ,
85
+ {
86
+ ...BASE_OPTIONS ,
87
+ path : "/search?q=coffee&hl=" ,
88
+ } ,
69
89
) ;
70
90
} ) ;
71
91
} ) ;
72
92
73
- describe ( "execute" , {
74
- sanitizeOps : false ,
75
- sanitizeResources : false ,
76
- } , ( ) => {
77
- let urlStub : Stub ;
93
+ describe (
94
+ "execute" ,
95
+ {
96
+ sanitizeOps : false ,
97
+ sanitizeResources : false ,
98
+ } ,
99
+ ( ) => {
100
+ let urlStub : Stub ;
78
101
79
- beforeAll ( ( ) => {
80
- urlStub = stub ( _internals , "getBaseUrl " , ( ) => BASE_URL ) ;
81
- } ) ;
102
+ beforeAll ( ( ) => {
103
+ urlStub = stub ( _internals , "getHostnameAndPort " , ( ) => BASE_OPTIONS ) ;
104
+ } ) ;
82
105
83
- afterAll ( ( ) => {
84
- urlStub . restore ( ) ;
85
- } ) ;
106
+ afterAll ( ( ) => {
107
+ urlStub . restore ( ) ;
108
+ } ) ;
86
109
87
- it ( "with short timeout" , async ( ) => {
88
- try {
89
- await execute ( "/search" , { q : "coffee" , gl : "us" } , 1 ) ;
90
- } catch ( e ) {
91
- assertInstanceOf ( e , RequestTimeoutError ) ;
92
- }
93
- } ) ;
94
- } ) ;
110
+ it ( "with short timeout" , async ( ) => {
111
+ try {
112
+ await execute ( "/search" , { q : "coffee" , gl : "us" } , 1 ) ;
113
+ } catch ( e ) {
114
+ assertInstanceOf ( e , RequestTimeoutError ) ;
115
+ }
116
+ } ) ;
117
+ } ,
118
+ ) ;
0 commit comments