/
format_test.go
110 lines (95 loc) · 2.32 KB
/
format_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
package contenttype
import (
"regexp"
"testing"
"github.com/RainInFall/assert"
)
func TestFormat(t *testing.T) {
assert.Init(t)
func() {
str, err := Format(&ContentType{Type: "text/html"})
assert.Ok(nil == err)
assert.Ok("text/html" == str)
}()
func() {
str, err := Format(&ContentType{Type: "image/svg+xml"})
assert.Ok(nil == err)
assert.Ok("image/svg+xml" == str)
}()
func() {
str, err := Format(&ContentType{
Type: "text/html",
Parameters: map[string]string{
"charset": "utf-8",
},
})
assert.Ok(nil == err)
assert.Ok("text/html; charset=utf-8" == str)
}()
func() {
str, err := Format(&ContentType{
Type: "text/html",
Parameters: map[string]string{
"foo": "bar or \"baz\"",
},
})
assert.Ok(nil == err)
assert.Ok("text/html; foo=\"bar or \\\"baz\\\"\"" == str)
}()
func() {
str, err := Format(&ContentType{
Type: "text/html",
Parameters: map[string]string{
"foo": "",
},
})
assert.Ok(nil == err)
assert.Ok("text/html; foo=\"\"" == str)
}()
func() {
str, err := Format(&ContentType{
Type: "text/html",
Parameters: map[string]string{
"charset": "utf-8",
"foo": "bar",
"bar": "baz",
},
})
assert.Ok(nil == err)
assert.Ok("text/html; bar=baz; charset=utf-8; foo=bar" == str)
}()
func() {
_, err := Format(nil)
assert.Ok(nil != err && regexp.MustCompile("argument ContentType is required").MatchString(err.Error()))
}()
func() {
_, err := Format(&ContentType{})
assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error()))
}()
func() {
_, err := Format(&ContentType{Type: "text/"})
assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error()))
}()
func() {
_, err := Format(&ContentType{Type: " text/html"})
assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error()))
}()
func() {
_, err := Format(&ContentType{
Type: "image/svg",
Parameters: map[string]string{
"foo/": "bar",
},
})
assert.Ok(nil != err && regexp.MustCompile("invalid parameter name").MatchString(err.Error()))
}()
func() {
_, err := Format(&ContentType{
Type: "image/svg",
Parameters: map[string]string{
"foo": "bar\u0000",
},
})
assert.Ok(nil != err && regexp.MustCompile("invalid parameter value").MatchString(err.Error()))
}()
}