@@ -40,29 +40,60 @@ func TestExtractURL(t *testing.T) {
40
40
}))
41
41
defer ts .Close ()
42
42
43
- lr := UReadability {TimeOut : 30 , SnippetSize : 200 }
44
- t .Log ("full url" )
45
- rb , err := lr .Extract (context .Background (), ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" )
46
- assert .NoError (t , err )
47
- assert .Equal (t , ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" , rb .URL , "not changed" )
48
- assert .Equal (t , "Всем миром для общей пользы • Umputun тут был" , rb .Title )
49
- assert .Equal (t , 9665 , len (rb .Content ))
43
+ lr := UReadability {TimeOut : 30 * time .Second , SnippetSize : 200 }
44
+
45
+ tests := []struct {
46
+ name string
47
+ url string
48
+ wantURL string
49
+ wantTitle string
50
+ wantContentLen int
51
+ wantErr bool
52
+ }{
53
+ {
54
+ name : "full url" ,
55
+ url : ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" ,
56
+ wantURL : ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" ,
57
+ wantTitle : "Всем миром для общей пользы • Umputun тут был" ,
58
+ wantContentLen : 9665 ,
59
+ wantErr : false ,
60
+ },
61
+ {
62
+ name : "short url" ,
63
+ url : ts .URL + "/IAvTHr" ,
64
+ wantURL : ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" ,
65
+ wantTitle : "Всем миром для общей пользы • Umputun тут был" ,
66
+ wantContentLen : 9665 ,
67
+ wantErr : false ,
68
+ },
69
+ {
70
+ name : "bad body" ,
71
+ url : ts .URL + "/bad_body" ,
72
+ wantErr : true ,
73
+ },
74
+ {
75
+ name : "bad url" ,
76
+ url : "http://bad_url" ,
77
+ wantErr : true ,
78
+ },
79
+ }
80
+
81
+ for _ , tt := range tests {
82
+ t .Run (tt .name , func (t * testing.T ) {
83
+ rb , err := lr .Extract (context .Background (), tt .url )
84
+
85
+ if tt .wantErr {
86
+ assert .Error (t , err )
87
+ assert .Nil (t , rb )
88
+ return
89
+ }
50
90
51
- t .Log ("short url" )
52
- rb , err = lr .Extract (context .Background (), ts .URL + "/IAvTHr" )
53
- assert .NoError (t , err )
54
- assert .Equal (t , ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" , rb .URL , "full url" )
55
- assert .Equal (t , 9665 , len (rb .Content ))
56
-
57
- t .Log ("bad body" )
58
- rb , err = lr .Extract (context .Background (), ts .URL + "/bad_body" )
59
- //assert.Error(t, err) // TODO: uncomment, wtf?! this should return error!
60
- assert .Nil (t , rb )
61
-
62
- t .Log ("bad url" )
63
- rb , err = lr .Extract (context .Background (), "http://bad_url" )
64
- assert .Error (t , err )
65
- assert .Nil (t , rb )
91
+ assert .NoError (t , err )
92
+ assert .Equal (t , tt .wantURL , rb .URL )
93
+ assert .Equal (t , tt .wantTitle , rb .Title )
94
+ assert .Equal (t , tt .wantContentLen , len (rb .Content ))
95
+ })
96
+ }
66
97
}
67
98
68
99
func TestExtractGeneral (t * testing.T ) {
@@ -92,7 +123,7 @@ func TestExtractGeneral(t *testing.T) {
92
123
}))
93
124
defer ts .Close ()
94
125
95
- lr := UReadability {TimeOut : 30 , SnippetSize : 200 }
126
+ lr := UReadability {TimeOut : 30 * time . Second , SnippetSize : 200 }
96
127
a , err := lr .Extract (context .Background (), ts .URL + "/2015/11/26/vsiem-mirom-dlia-obshchiei-polzy/" )
97
128
assert .NoError (t , err )
98
129
assert .Equal (t , "Всем миром для общей пользы • Umputun тут был" , a .Title )
@@ -114,7 +145,7 @@ func TestExtractGeneral(t *testing.T) {
114
145
}
115
146
116
147
func TestNormalizeLinks (t * testing.T ) {
117
- lr := UReadability {TimeOut : 30 , SnippetSize : 200 }
148
+ lr := UReadability {TimeOut : 30 * time . Second , SnippetSize : 200 }
118
149
inp := `blah <img src="/aaa.png"/> sdfasd <a href="/blah2/aa.link">something</a> blah33 <img src="//aaa.com/xyz.jpg">xx</img>`
119
150
u , _ := url .Parse ("http://ukeeper.com/blah" )
120
151
out , links := lr .normalizeLinks (inp , & http.Request {URL : u })
@@ -130,7 +161,7 @@ func TestNormalizeLinks(t *testing.T) {
130
161
}
131
162
132
163
func TestNormalizeLinksIssue (t * testing.T ) {
133
- lr := UReadability {TimeOut : 30 , SnippetSize : 200 }
164
+ lr := UReadability {TimeOut : 30 * time . Second , SnippetSize : 200 }
134
165
_ , err := lr .Extract (context .Background (), "https://git-scm.com/book/en/v2/Git-Tools-Submodules" )
135
166
assert .NoError (t , err )
136
167
}
@@ -150,8 +181,8 @@ func (m RulesMock) Disable(_ context.Context, _ primitive.ObjectID) error { retu
150
181
func (m RulesMock ) All (_ context.Context ) []datastore.Rule { return make ([]datastore.Rule , 0 ) }
151
182
152
183
func TestGetContentCustom (t * testing.T ) {
153
- lr := UReadability {TimeOut : 30 , SnippetSize : 200 , Rules : RulesMock {}}
154
- httpClient := & http.Client {Timeout : time . Second * 30 }
184
+ lr := UReadability {TimeOut : 30 * time . Second , SnippetSize : 200 , Rules : RulesMock {}}
185
+ httpClient := & http.Client {Timeout : 30 * time . Second }
155
186
ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
156
187
if r .URL .String () == "/2015/09/25/poiezdka-s-apple-maps/" {
157
188
fh , err := os .Open ("testdata/poiezdka-s-apple-maps.html" )
0 commit comments