-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_test.go
40 lines (33 loc) · 1.04 KB
/
data_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
package webutil_test
import (
"bytes"
"testing"
"github.com/KarpelesLab/webutil"
)
func TestParseData(t *testing.T) {
var cases = []struct {
A string
R []byte
Mime string
}{
{"data:,", []byte{}, "application/octet-stream"},
{"data:text/plain,Hello world", []byte("Hello world"), "text/plain"},
{"data:text/plain,Hello+world", []byte("Hello world"), "text/plain"},
{"data:text/plain,Hello%20world", []byte("Hello world"), "text/plain"},
{"data:;base64,Zm9v", []byte("foo"), "application/octet-stream"},
{"data:;base64,Zm9vYg", []byte("foob"), "application/octet-stream"},
{"data://text/plain;base64,SSBsb3ZlIFBIUAo=", []byte("I love PHP\n"), "text/plain"}, // from php doc
}
for _, c := range cases {
b, mime, err := webutil.ParseDataUri(c.A)
if err != nil {
t.Errorf("test failed, error %s", err)
}
if bytes.Compare(c.R, b) != 0 {
t.Errorf("test failed, %s result %s, expected %v", c.A, b, c.R)
}
if c.Mime != mime {
t.Errorf("test failed, %s mime result %s, expected %s", c.A, mime, c.Mime)
}
}
}