@@ -10,53 +10,6 @@ import (
1010 log "github.com/sirupsen/logrus"
1111)
1212
13- func ExampleIsOnline () {
14- google , _ := url .Parse ("https://google.com" )
15- err := IsOnline (* google )
16-
17- if err != nil {
18- fmt .Println ("Host is not accessible" )
19- } else {
20- fmt .Println ("Host is accessible" )
21- }
22- // Output: Host is accessible
23- }
24-
25- func setUpMock (scenario string ) * httptest.Server {
26- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
27- switch scenario {
28- case "OK" :
29- w .WriteHeader (http .StatusOK )
30- _ , _ = fmt .Fprintln (w , "OK" )
31- return
32- case "Accepted" :
33- w .WriteHeader (http .StatusAccepted )
34- _ , _ = fmt .Fprintln (w , "Accepted" )
35- return
36- case "Redirect" :
37- http .Redirect (w , r , "https://google.com" , http .StatusMovedPermanently )
38- return
39- case "BadRequest" :
40- http .Error (w , http .StatusText (http .StatusBadRequest ), http .StatusBadRequest )
41- return
42- case "NotFound" :
43- http .Error (w , http .StatusText (http .StatusNotFound ), http .StatusNotFound )
44- return
45- case "RateLimit" :
46- http .Error (w , http .StatusText (http .StatusTooManyRequests ), http .StatusTooManyRequests )
47- return
48- case "ServerError" :
49- http .Error (w , http .StatusText (http .StatusInternalServerError ), http .StatusNotFound )
50- return
51- default :
52- log .Fatalf ("Unimplemented scenario %q provided" , scenario )
53- return
54- }
55- }))
56-
57- return ts
58- }
59-
6013func Test_IsOnline (t * testing.T ) {
6114 tests := []struct {
6215 name string
@@ -114,11 +67,45 @@ func Test_IsOnline(t *testing.T) {
11467 }
11568}
11669
117- func Test_IsOnline_MissingHost (t * testing.T ) {
118- testURL , _ := url .Parse ("https://does-not-exist.github.com" )
70+ func setUpMock (scenario string ) * httptest.Server {
71+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
72+ // Handle redirect target path
73+ if scenario == "Redirect" && r .URL .Path == "/redirected" {
74+ w .WriteHeader (http .StatusOK )
75+ _ , _ = fmt .Fprintln (w , "Redirected successfully" )
76+ return
77+ }
11978
120- err := IsOnline (* testURL )
121- if (err != nil ) != true {
122- t .Errorf ("IsOnline(%q) error = %v, wantErr %v" , testURL .String (), err , true )
123- }
79+ switch scenario {
80+ case "OK" :
81+ w .WriteHeader (http .StatusOK )
82+ _ , _ = fmt .Fprintln (w , "OK" )
83+ return
84+ case "Accepted" :
85+ w .WriteHeader (http .StatusAccepted )
86+ _ , _ = fmt .Fprintln (w , "Accepted" )
87+ return
88+ case "Redirect" :
89+ // Redirect to the same mock server to avoid external calls
90+ http .Redirect (w , r , "/redirected" , http .StatusMovedPermanently )
91+ return
92+ case "BadRequest" :
93+ http .Error (w , http .StatusText (http .StatusBadRequest ), http .StatusBadRequest )
94+ return
95+ case "NotFound" :
96+ http .Error (w , http .StatusText (http .StatusNotFound ), http .StatusNotFound )
97+ return
98+ case "RateLimit" :
99+ http .Error (w , http .StatusText (http .StatusTooManyRequests ), http .StatusTooManyRequests )
100+ return
101+ case "ServerError" :
102+ http .Error (w , http .StatusText (http .StatusInternalServerError ), http .StatusNotFound )
103+ return
104+ default :
105+ log .Fatalf ("Unimplemented scenario %q provided" , scenario )
106+ return
107+ }
108+ }))
109+
110+ return ts
124111}
0 commit comments