This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
imgix_test.go
155 lines (130 loc) · 5.04 KB
/
imgix_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 imgix
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func testClient() Client {
return NewClient("prod.imgix.net", "stag.imgix.net", "dev.imgix.net")
}
func testClientWithToken() Client {
return NewClientWithToken("my-social-network.imgix.net", "FOO123bar")
}
func TestBasicClientPath(t *testing.T) {
c := testClient()
assert.Equal(t, "https://prod.imgix.net/1/users.jpg", c.Path("/1/users.jpg"))
}
func TestClientPathWithParams(t *testing.T) {
c := testClient()
params := url.Values{"w": []string{"200"}, "h": []string{"400"}}
assert.Equal(t, "https://prod.imgix.net/1/users.jpg?h=400&w=200", c.PathWithParams("/1/users.jpg", params))
}
func TestClientScheme(t *testing.T) {
c := testClient()
c.secure = false
assert.Equal(t, "http", c.Scheme())
c.secure = true
assert.Equal(t, "https", c.Scheme())
}
func TestClientPath(t *testing.T) {
c := testClient()
u := c.Path("/jax.jpg")
assert.Equal(t, "https://prod.imgix.net/jax.jpg", u)
}
func TestClientPathWithOneParam(t *testing.T) {
c := testClient()
params := url.Values{"w": []string{"400"}}
u := c.PathWithParams("/jax.jpg", params)
assert.Equal(t, "https://prod.imgix.net/jax.jpg?w=400", u)
}
func TestClientPathWithTwoParams(t *testing.T) {
c := testClient()
params := url.Values{"w": []string{"400"}, "h": []string{"300"}}
u := c.PathWithParams("/jax.jpg", params)
assert.Equal(t, "https://prod.imgix.net/jax.jpg?h=300&w=400", u)
}
func TestClientPathWithParamsEncodesParamKeys(t *testing.T) {
c := testClient()
params := url.Values{"hello world": []string{"interesting"}}
u := c.PathWithParams("/demo.png", params)
assert.Equal(t, "https://prod.imgix.net/demo.png?hello%%20world=interesting", u)
}
func TestClientPathWithParamsEncodesParamValues(t *testing.T) {
c := testClient()
params := url.Values{"hello_world": []string{"/foo\"> <script>alert(\"hacked\")</script><"}}
u := c.PathWithParams("/demo.png", params)
assert.Equal(t, "https://prod.imgix.net/demo.png?hello_world=%2Ffoo%22%3E%%20%3Cscript%3Ealert%28%22hacked%22%29%3C%2Fscript%3E%3C", u)
}
func TestClientPathWithParamsEncodesBase64ParamVariants(t *testing.T) {
c := testClient()
params := url.Values{"txt64": []string{"I cannøt belîév∑ it wors! 😱"}}
u := c.PathWithParams("~text", params)
assert.Equal(t, "https://prod.imgix.net/~text?txt64=SSBjYW5uw7h0IGJlbMOuw6l24oiRIGl0IHdvcu-jv3MhIPCfmLE", u)
}
func TestClientPathWithSignature(t *testing.T) {
c := testClientWithToken()
u := c.Path("/users/1.png")
assert.Equal(t, "https://my-social-network.imgix.net/users/1.png?s=6797c24146142d5b40bde3141fd3600c", u)
}
func TestClientPathWithSignatureAndParams(t *testing.T) {
c := testClientWithToken()
params := url.Values{"w": []string{"400"}, "h": []string{"300"}}
assert.Equal(t, "https://my-social-network.imgix.net/users/1.png?h=300&w=400&s=1a4e48641614d1109c6a7af51be23d18", c.PathWithParams("/users/1.png", params))
}
func TestClientPathWithSignatureAndEmptyParams(t *testing.T) {
c := testClientWithToken()
params := url.Values{}
assert.Equal(t, "https://my-social-network.imgix.net/users/1.png?s=6797c24146142d5b40bde3141fd3600c", c.PathWithParams("/users/1.png", params))
}
func TestClientFullyQualifiedUrlPath(t *testing.T) {
c := testClientWithToken()
assert.Equal(t, "https://my-social-network.imgix.net/http%3A%2F%2Favatars.com%2Fjohn-smith.png?s=493a52f008c91416351f8b33d4883135", c.Path("http://avatars.com/john-smith.png"))
}
func TestClientFullyQualifiedUrlPathWithParams(t *testing.T) {
c := testClientWithToken()
params := url.Values{"w": []string{"400"}, "h": []string{"300"}}
assert.Equal(t, "https://my-social-network.imgix.net/http%3A%2F%2Favatars.com%2Fjohn-smith.png?h=300&w=400&s=a201fe1a3caef4944dcb40f6ce99e746", c.PathWithParams("http://avatars.com/john-smith.png", params))
}
func TestClientFallbackShardStrategy(t *testing.T) {
c := testClient()
assert.Equal(t, ShardStrategy(""), c.shardStrategy)
assert.Equal(t, ShardStrategyCycle, c.ShardStrategy())
}
func TestClientHostUsingCRC(t *testing.T) {
c := testClient()
c.shardStrategy = ShardStrategyCRC
assert.Equal(t, "prod.imgix.net", c.Host("/1/users.jpg"))
assert.Equal(t, "dev.imgix.net", c.Host("/2/ellothere.png"))
}
func TestClientHostUsingCycle(t *testing.T) {
c := testClient()
c.shardStrategy = ShardStrategyCycle
assert.Equal(t, "prod.imgix.net", c.Host("/1/users.jpg"))
assert.Equal(t, "stag.imgix.net", c.Host("/1/users.jpg"))
assert.Equal(t, "dev.imgix.net", c.Host("/1/users.jpg"))
assert.Equal(t, "prod.imgix.net", c.Host("/1/users.jpg"))
}
func TestClientShardStrategyValidation(t *testing.T) {
defer func() {
if r := recover(); r != nil {
e, ok := r.(error)
assert.True(t, ok)
assert.EqualError(t, e, "shard strategy 'hellothere' is not supported")
}
}()
c := testClient()
c.shardStrategy = ShardStrategy("hellothere")
c.ShardStrategy()
}
func TestClientHostsCountValidation(t *testing.T) {
defer func() {
if r := recover(); r != nil {
e, ok := r.(error)
assert.True(t, ok)
assert.EqualError(t, e, "hosts must be provided")
}
}()
c := testClient()
c.hosts = []string{}
c.Hosts(1)
}