forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
155 lines (130 loc) · 3.75 KB
/
utils_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"crypto/rand"
"io/ioutil"
"net/http"
"regexp"
"testing"
"github.com/facebookgo/ensure"
"github.com/facebookgo/jsonpipe"
"github.com/facebookgo/parse"
)
func TestBothEmpty(t *testing.T) {
t.Parallel()
ensure.False(t, numericLessThan("", ""))
}
func TestOnlyAEmpty(t *testing.T) {
t.Parallel()
ensure.True(t, numericLessThan("", "b"))
}
func TestOnlyBEmpty(t *testing.T) {
t.Parallel()
ensure.False(t, numericLessThan("a", ""))
}
func TestBothEqual(t *testing.T) {
t.Parallel()
ensure.False(t, numericLessThan("abc", "abc"))
}
func TestAPrefixOfB(t *testing.T) {
t.Parallel()
ensure.True(t, numericLessThan("abc", "abcd"))
}
func TestBPrefixOfA(t *testing.T) {
t.Parallel()
ensure.False(t, numericLessThan("abcd", "abc"))
}
func TestABSansNumbers(t *testing.T) {
t.Parallel()
const max = 10
dictionary := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var rb [max]byte
randomString := func(length int) string {
bytes := rb[:length]
_, err := rand.Read(bytes)
ensure.Nil(t, err)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
for i := 1; i < max; i++ {
for j := 1; j < max; j++ {
a, b := randomString(i), randomString(j)
ensure.DeepEqual(t, numericLessThan(a, b), a < b)
}
}
}
func TestABWithNumbers(t *testing.T) {
ensure.True(t, numericLessThan("abc9", "abc12"))
ensure.False(t, numericLessThan("abc12", "abc9"))
ensure.DeepEqual(t, numericLessThan("abc001", "abc1"), "abc001" < "abc1")
}
func TestGetHostFromURL(t *testing.T) {
urlStr := "https://api.parse.com/1/"
host, err := getHostFromURL(urlStr, "")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.parse.com")
urlStr = "https://api.parse.com/1/"
host, err = getHostFromURL(urlStr, "yolo@earth.com")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.parse.com#yolo@earth.com")
urlStr = "https://api.example.com:8080/1/"
host, err = getHostFromURL(urlStr, "")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.example.com")
urlStr = "https://api.example.com:8080/1/"
host, err = getHostFromURL(urlStr, "yolo@earth.com")
ensure.Nil(t, err)
ensure.DeepEqual(t, host, "api.example.com#yolo@earth.com")
urlStr = "api.example.com:8080:90"
host, err = getHostFromURL(urlStr, "")
ensure.Err(t, err, regexp.MustCompile("not a valid url"))
}
func TestIsSupportedWarning(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
ht := transportFunc(func(r *http.Request) (*http.Response, error) {
ensure.DeepEqual(t, r.URL.Path, "/1/supported")
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(
jsonpipe.Encode(
map[string]string{"warning": "please update"},
),
),
}, nil
})
h.env.ParseAPIClient = &ParseAPIClient{apiClient: &parse.Client{Transport: ht}}
message, err := checkIfSupported(h.env, "2.0.2")
ensure.Nil(t, err)
ensure.DeepEqual(t, message, "please update")
}
func TestIsSupportedError(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
ht := transportFunc(func(r *http.Request) (*http.Response, error) {
ensure.DeepEqual(t, r.URL.Path, "/1/supported")
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(
jsonpipe.Encode(
map[string]string{"error": "not supported"},
),
),
}, nil
})
h.env.ParseAPIClient = &ParseAPIClient{apiClient: &parse.Client{Transport: ht}}
_, err := checkIfSupported(h.env, "2.0.2")
ensure.Err(t, err, regexp.MustCompile("not supported"))
}
func TestLastFour(t *testing.T) {
t.Parallel()
ensure.DeepEqual(t, last4(""), "")
ensure.DeepEqual(t, last4("a"), "a")
ensure.DeepEqual(t, last4("ab"), "ab")
ensure.DeepEqual(t, last4("abc"), "abc")
ensure.DeepEqual(t, last4("abcd"), "abcd")
ensure.DeepEqual(t, last4("abcdefg"), "***defg")
}