@@ -7,41 +7,164 @@ import (
77 "github.com/karust/openserp/core"
88)
99
10- var testQuery = core.Query {Text : "go" , Site : "tutorialspoint.com" , DateInterval : "20140101..20230101" , Limit : 10 }
11-
12- func TestUrlBuild (t * testing.T ) {
13- res , err := BuildURL (testQuery )
14- if err != nil {
15- t .Fatal (err )
10+ func TestBuildURL (t * testing.T ) {
11+ tests := []struct {
12+ name string
13+ query core.Query
14+ wantErr bool
15+ check func (* testing.T , url.Values , string )
16+ }{
17+ {
18+ name : "combined params with unicode and start zero" ,
19+ query : core.Query {
20+ Text : "go 搜索" ,
21+ Site : "tutorialspoint.com" ,
22+ Filetype : "pdf" ,
23+ DateInterval : "20140101..20230101" ,
24+ Limit : 10 ,
25+ Start : 0 ,
26+ },
27+ check : func (t * testing.T , params url.Values , host string ) {
28+ t .Helper ()
29+ if host != "www.baidu.com" {
30+ t .Fatalf ("unexpected host: %s" , host )
31+ }
32+ if got := params .Get ("wd" ); got != "go 搜索 site:tutorialspoint.com filetype:pdf" {
33+ t .Fatalf ("unexpected wd value: %q" , got )
34+ }
35+ if got := params .Get ("gpc" ); got != "stf=1388534400,1672531200|stftype=2" {
36+ t .Fatalf ("unexpected gpc value: %q" , got )
37+ }
38+ if got := params .Get ("rn" ); got != "10" {
39+ t .Fatalf ("unexpected rn value: %q" , got )
40+ }
41+ if got := params .Get ("pn" ); got != "" {
42+ t .Fatalf ("pn should be omitted when Start=0, got %q" , got )
43+ }
44+ if got := params .Get ("f" ); got != "8" {
45+ t .Fatalf ("unexpected f value: %q" , got )
46+ }
47+ if got := params .Get ("ie" ); got != "utf-8" {
48+ t .Fatalf ("unexpected ie value: %q" , got )
49+ }
50+ },
51+ },
52+ {
53+ name : "very large start" ,
54+ query : core.Query {
55+ Text : "golang" ,
56+ Start : 2147483647 ,
57+ },
58+ check : func (t * testing.T , params url.Values , _ string ) {
59+ t .Helper ()
60+ if got := params .Get ("pn" ); got != "2147483647" {
61+ t .Fatalf ("unexpected pn value: %q" , got )
62+ }
63+ },
64+ },
65+ {
66+ name : "negative start returns error" ,
67+ query : core.Query {
68+ Text : "golang" ,
69+ Start : - 1 ,
70+ },
71+ wantErr : true ,
72+ },
73+ {
74+ name : "empty fields return error" ,
75+ query : core.Query {},
76+ wantErr : true ,
77+ },
1678 }
1779
18- want := "https://www.baidu.com/s?f=8&gpc=stf%3D1388534400%2C1672531200%7Cstftype%3D2&ie=utf-8&rn=10&wd=go+site%3Atutorialspoint.com"
80+ for _ , tt := range tests {
81+ t .Run (tt .name , func (t * testing.T ) {
82+ got , err := BuildURL (tt .query )
83+ if (err != nil ) != tt .wantErr {
84+ t .Fatalf ("BuildURL() error = %v, wantErr %v" , err , tt .wantErr )
85+ }
86+ if tt .wantErr {
87+ return
88+ }
1989
20- if want != res {
21- t .Fatalf ("Wanted result `%s` doesn't match to resulted `%s`" , want , res )
90+ parsed , err := url .Parse (got )
91+ if err != nil {
92+ t .Fatalf ("invalid URL returned: %v" , err )
93+ }
94+ if tt .check != nil {
95+ tt .check (t , parsed .Query (), parsed .Host )
96+ }
97+ })
2298 }
2399}
24100
25- func TestImageUrlBuild (t * testing.T ) {
26- query := core.Query {Text : "金毛猎犬" }
27-
28- got , err := BuildImageURL (query , 0 )
29- if err != nil {
30- t .Fatal (err )
101+ func TestBuildImageURL (t * testing.T ) {
102+ tests := []struct {
103+ name string
104+ query core.Query
105+ pageNum int
106+ wantErr bool
107+ check func (* testing.T , url.Values , string )
108+ }{
109+ {
110+ name : "unicode query" ,
111+ query : core.Query {Text : "金毛猎犬" },
112+ pageNum : 0 ,
113+ check : func (t * testing.T , params url.Values , host string ) {
114+ t .Helper ()
115+ if host != "image.baidu.com" {
116+ t .Fatalf ("unexpected host: %s" , host )
117+ }
118+ if got := params .Get ("word" ); got != "金毛猎犬" {
119+ t .Fatalf ("expected unicode query, got %q" , got )
120+ }
121+ if got := params .Get ("tn" ); got != "resultjson_com" {
122+ t .Fatalf ("unexpected tn value: %q" , got )
123+ }
124+ },
125+ },
126+ {
127+ name : "combined params with pagination" ,
128+ query : core.Query {
129+ Text : "golang" ,
130+ Limit : 25 ,
131+ },
132+ pageNum : 3 ,
133+ check : func (t * testing.T , params url.Values , _ string ) {
134+ t .Helper ()
135+ if got := params .Get ("rn" ); got != "30" {
136+ t .Fatalf ("unexpected rn value: %q" , got )
137+ }
138+ if got := params .Get ("pn" ); got != "90" {
139+ t .Fatalf ("unexpected pn value: %q" , got )
140+ }
141+ },
142+ },
143+ {
144+ name : "empty fields return error" ,
145+ query : core.Query {},
146+ pageNum : 0 ,
147+ wantErr : true ,
148+ },
31149 }
32150
33- parsed , err := url .Parse (got )
34- if err != nil {
35- t .Fatalf ("invalid URL returned: %v" , err )
36- }
37- if parsed .Host != "image.baidu.com" {
38- t .Fatalf ("unexpected host: %s" , parsed .Host )
39- }
40- q := parsed .Query ()
41- if q .Get ("word" ) != "金毛猎犬" {
42- t .Fatalf ("expected word query to be preserved, got %q" , q .Get ("word" ))
43- }
44- if q .Get ("tn" ) != "resultjson_com" {
45- t .Fatalf ("expected tn=resultjson_com, got %q" , q .Get ("tn" ))
151+ for _ , tt := range tests {
152+ t .Run (tt .name , func (t * testing.T ) {
153+ got , err := BuildImageURL (tt .query , tt .pageNum )
154+ if (err != nil ) != tt .wantErr {
155+ t .Fatalf ("BuildImageURL() error = %v, wantErr %v" , err , tt .wantErr )
156+ }
157+ if tt .wantErr {
158+ return
159+ }
160+
161+ parsed , err := url .Parse (got )
162+ if err != nil {
163+ t .Fatalf ("invalid URL returned: %v" , err )
164+ }
165+ if tt .check != nil {
166+ tt .check (t , parsed .Query (), parsed .Host )
167+ }
168+ })
46169 }
47170}
0 commit comments