-
Notifications
You must be signed in to change notification settings - Fork 7
/
minecraft_test.go
126 lines (92 loc) · 3.28 KB
/
minecraft_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
// minecraft_test.go
package minecraft
import (
"net/http"
"os"
"regexp"
"testing"
"github.com/minotar/minecraft/mockminecraft"
. "github.com/smartystreets/goconvey/convey"
)
var (
mcTest *Minecraft
mcProd *Minecraft
)
func TestMain(m *testing.M) {
mux := mockminecraft.ReturnMux()
rt, shutdown := mockminecraft.Setup(mux)
mcTest = NewMinecraft()
mcTest.Client = &http.Client{Transport: rt}
mcTest.UsernameAPI.SkinURL = "http://skins.example.net/skins/"
mcTest.UsernameAPI.CapeURL = "http://skins.example.net/capes/"
mcProd = NewMinecraft()
code := m.Run()
shutdown()
os.Exit(code)
}
func TestRegexs(t *testing.T) {
Convey("Regexs compile", t, func() {
var err error
_, err = regexp.Compile("^" + ValidUsernameRegex + "$")
So(err, ShouldBeNil)
_, err = regexp.Compile("^" + ValidUUIDRegex + "$")
So(err, ShouldBeNil)
_, err = regexp.Compile("^" + ValidUsernameOrUUIDRegex + "$")
So(err, ShouldBeNil)
})
Convey("Regexs work", t, func() {
invalidUsernames := []string{"d9135e082f2244c89cb0bee234155292", "_-proscope-_", "PeriScopeButTooLong"}
validUsernames := []string{"clone1018", "lukegb", "Wooxye"}
invalidUUIDs := []string{"clone1018", "d9135e082f2244c8-9cb0-bee234155292"}
validUUIDs := []string{"d9135e082f2244c89cb0bee234155292", "d9135e08-2f22-44c8-9cb0-bee234155292"}
validUsernamesOrUUIDs := append(validUsernames, validUUIDs...)
possiblyInvalidUsernamesOrUUIDs := append(invalidUsernames, invalidUUIDs...)
Convey("Username regex works", func() {
for _, validUsername := range validUsernames {
So(RegexUsername.MatchString(validUsername), ShouldBeTrue)
}
for _, invalidUsername := range invalidUsernames {
So(RegexUsername.MatchString(invalidUsername), ShouldBeFalse)
}
})
Convey("UUID regex works", func() {
for _, validUUID := range validUUIDs {
So(RegexUUID.MatchString(validUUID), ShouldBeTrue)
}
for _, invalidUUID := range invalidUUIDs {
So(RegexUUID.MatchString(invalidUUID), ShouldBeFalse)
}
})
Convey("Username-or-UUID regex works", func() {
for _, validThing := range validUsernamesOrUUIDs {
So(RegexUsernameOrUUID.MatchString(validThing), ShouldBeTrue)
}
for _, possiblyInvalidThing := range possiblyInvalidUsernamesOrUUIDs {
resultOne := RegexUsername.MatchString(possiblyInvalidThing)
resultTwo := RegexUUID.MatchString(possiblyInvalidThing)
expectedResult := resultOne || resultTwo
So(RegexUsernameOrUUID.MatchString(possiblyInvalidThing), ShouldEqual, expectedResult)
}
})
})
}
func TestExtra(t *testing.T) {
Convey("Test bad GET requests", t, func() {
Convey("apiRequest Bad Request", func() {
_, err := mcProd.apiRequest("::")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "unable to create request: parse ::: missing protocol scheme")
})
Convey("apiRequest Bad GET", func() {
_, err := mcProd.apiRequest("//dummy_url")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "unable to GET URL: Get //dummy_url: unsupported protocol scheme \"\"")
})
Convey("t.Fetch Bad GET", func() {
texture := &Texture{URL: "//dummy_url", Mc: mcProd}
err := texture.Fetch()
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "unable to Fetch Texture: unable to GET URL: Get //dummy_url: unsupported protocol scheme \"\"")
})
})
}