@@ -11,13 +11,24 @@ import {
1111 assertInstanceOf ,
1212 assertMatch ,
1313} from "https://deno.land/std@0.170.0/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" ;
1520import { RequestTimeoutError } from "../src/errors.ts" ;
1621
1722loadSync ( { 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+ } ;
2132
2233describe ( "getSource" , ( ) => {
2334 it ( "use runtime version" , async ( ) => {
@@ -28,67 +39,80 @@ describe("getSource", () => {
2839 } ) ;
2940} ) ;
3041
31- describe ( "buildUrl " , ( ) => {
42+ describe ( "buildRequestOptions " , ( ) => {
3243 let urlStub : Stub ;
3344
3445 beforeAll ( ( ) => {
35- urlStub = stub ( _internals , "getBaseUrl " , ( ) => BASE_URL ) ;
46+ urlStub = stub ( _internals , "getHostnameAndPort " , ( ) => BASE_OPTIONS ) ;
3647 } ) ;
3748
3849 afterAll ( ( ) => {
3950 urlStub . restore ( ) ;
4051 } ) ;
4152
4253 it ( "with blank path and empty parameters" , async ( ) => {
43- assertEquals ( await buildUrl ( "" , { } ) , ` ${ BASE_URL } ?` ) ;
54+ assertEquals ( await buildRequestOptions ( "" , { } ) , BASE_OPTIONS ) ;
4455 } ) ;
4556
4657 it ( "with path and empty parameters" , async ( ) => {
47- assertEquals ( await buildUrl ( "/" , { } ) , ` ${ BASE_URL } /?` ) ;
58+ assertEquals ( await buildRequestOptions ( "/" , { } ) , BASE_OPTIONS ) ;
4859 } ) ;
4960
5061 it ( "with path and parameters" , async ( ) => {
5162 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 ,
5465 ) ;
5566 } ) ;
5667
5768 it ( "with source" , async ( ) => {
58- const url = await buildUrl ( "/search" , { source : await getSource ( ) } ) ;
69+ const options = await buildRequestOptions ( "/search" , {
70+ source : await getSource ( ) ,
71+ } ) ;
5972 assertMatch (
60- url ,
73+ options . path as string ,
6174 / 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 + $ / ,
6275 ) ;
6376 } ) ;
6477
6578 it ( "with undefined parameters" , async ( ) => {
6679 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+ } ,
6989 ) ;
7090 } ) ;
7191} ) ;
7292
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 ;
78101
79- beforeAll ( ( ) => {
80- urlStub = stub ( _internals , "getBaseUrl " , ( ) => BASE_URL ) ;
81- } ) ;
102+ beforeAll ( ( ) => {
103+ urlStub = stub ( _internals , "getHostnameAndPort " , ( ) => BASE_OPTIONS ) ;
104+ } ) ;
82105
83- afterAll ( ( ) => {
84- urlStub . restore ( ) ;
85- } ) ;
106+ afterAll ( ( ) => {
107+ urlStub . restore ( ) ;
108+ } ) ;
86109
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