-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_test.go
86 lines (73 loc) · 2.14 KB
/
get_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
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"testing"
)
func TestGet(t *testing.T) {
// Start testing webserver in a goroutine
go func() {
// Normal test
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, `{"string":"Testing String","int":69420,"bool":true}`)
if err != nil {
log.Fatal(err)
}
})
// Header test
http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, r.Header.Get("test-header"))
if err != nil {
log.Fatal(err)
}
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}()
testFlags := Flags{
Url: "http://localhost:8080/",
Headers: nil,
}
res, err := Get(&testFlags)
if err != nil {
t.Fatal("Test failed with unexpected error: ", err)
}
if len(res) < 100 {
t.Error("Test failed: Response too short (<100 char)")
}
resStr := string(res)
matchHttps, err := regexp.MatchString(`[h,t,p,s]{4,5}\:\/\/`, resStr)
if err != nil {
t.Fatal("Regexp crashed with unexpected error: ", err)
} else if matchHttps {
t.Error("Test failed: Response contains HTTPS URL")
}
// Check for protocol version ex: HTTP/1.1
matchProtVer, err := regexp.MatchString(`HTTP\/\d\.\d \d{3} [A-Z]*`, resStr)
if err != nil {
t.Fatal("Regexp crashed with unexpected error: ", err)
} else if !matchProtVer {
t.Error("Test failed: Response does not contain protocol version")
}
matchResponse, err := regexp.MatchString(`\{"string":"Testing String","int":69420,"bool":true\}`, resStr)
if err != nil {
t.Fatal("Regexp crashed with unexpected error: ", err)
} else if !matchResponse {
t.Error("Test failed: Different response than expected (", resStr, ")")
}
headerTestFlags := Flags{
Url: "http://localhost:8080/headers",
Headers: []string{"test-header: gurl-test-header"},
}
headerCheck, err := Get(&headerTestFlags)
matchHeader, err := regexp.MatchString(`gurl-test-header`, string(headerCheck))
if err != nil {
t.Fatal("Regexp crashed with unexpected error: ", err)
} else if !matchHeader {
t.Error("Test failed: Different header than expected (", resStr, ")")
}
}