/
helpers_test.go
128 lines (99 loc) · 2.82 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package utils
import (
"net/http"
"net/http/httptest"
"sync"
"testing"
"github.com/tonyvugithub/GoURLsCheckerCLI/models"
)
func TestCheckLink200Response(t *testing.T) {
// Mock a server here
srv := mockServer(200, "OK")
// Close the server when done
defer srv.Close()
// Set up parameters for CheckLink function
c := make(chan models.LinkStatus)
userAgent := "Go-http-client/1.1" //Default User-Agent
// Waitgroup
var wg sync.WaitGroup
wg.Add(1)
// Make call to the mock server with the server's URL
go func() {
defer wg.Done()
CheckLink(srv.URL, c, userAgent)
}()
// Receive the result from the channel
result := <-c
wg.Wait()
resultStatus := result.GetLiveStatus()
// If the live status is true meaning it returns a 200, else 400 or 404
if resultStatus != true {
t.Errorf("CheckLink was incorrect, expect %t, received %t", true, resultStatus)
}
}
func TestCheckLink403Response(t *testing.T) {
// Mock a server here
srv := mockServer(403, "Forbidden")
// Close the server when done
defer srv.Close()
// Set up parameters for CheckLink function
c := make(chan models.LinkStatus)
userAgent := "Go-http-client/1.1" //Default User-Agent
// Waitgroup
var wg sync.WaitGroup
wg.Add(1)
// Make call to the mock server with the server's URL
go func() {
defer wg.Done()
CheckLink(srv.URL, c, userAgent)
}()
// Receive the result from the channel
result := <-c
wg.Wait()
resultStatus := result.GetLiveStatus()
// If the live status is true meaning it returns a 200, else 400 or 404
if resultStatus != true {
t.Errorf("CheckLink was incorrect, expect %t, received %t", true, resultStatus)
}
}
func TestCheckLinkWith404Response(t *testing.T) {
// Mock a server here
srv := mockServer(404, "Not Found")
// Close the server when done
defer srv.Close()
// Set up parameters for CheckLink function
c := make(chan models.LinkStatus)
userAgent := "Go-http-client/1.1"
// Waitgroup
var wg sync.WaitGroup
wg.Add(1)
// Make call to the mock server with the server's URL
go func() {
CheckLink(srv.URL, c, userAgent)
defer wg.Done()
}()
// Receive the result from the channel
result := <-c
wg.Wait()
resultStatus := result.GetLiveStatus()
// If the live status is true meaning it returns a 200, else 400 or 404
if resultStatus != true {
t.Errorf("CheckLink was incorrect, expect %t, received %t", true, resultStatus)
}
}
func mockServer(statusCode int, msg string) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
w.Write([]byte(msg))
}))
return srv
}
/* func handler404(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Not Found"))
}
func handler200(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 - OK"))
}
*/