-
Notifications
You must be signed in to change notification settings - Fork 718
/
util_test.go
107 lines (87 loc) · 2.61 KB
/
util_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
// Copyright (c) 2015-2024 Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package resty
import (
"bytes"
"errors"
"mime/multipart"
"testing"
)
func TestIsJSONType(t *testing.T) {
for _, test := range []struct {
input string
expect bool
}{
{"application/json", true},
{"application/xml+json", true},
{"application/vnd.foo+json", true},
{"application/json; charset=utf-8", true},
{"application/vnd.foo+json; charset=utf-8", true},
{"text/json", true},
{"text/vnd.foo+json", true},
{"application/foo-json", true},
{"application/foo.json", true},
{"application/vnd.foo-json", true},
{"application/vnd.foo.json", true},
{"application/x-amz-json-1.1", true},
{"text/foo-json", true},
{"text/foo.json", true},
{"text/vnd.foo-json", true},
{"text/vnd.foo.json", true},
} {
result := IsJSONType(test.input)
if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}
func TestIsXMLType(t *testing.T) {
for _, test := range []struct {
input string
expect bool
}{
{"application/xml", true},
{"application/vnd.foo+xml", true},
{"application/xml; charset=utf-8", true},
{"application/vnd.foo+xml; charset=utf-8", true},
{"text/xml", true},
{"text/vnd.foo+xml", true},
{"application/foo-xml", true},
{"application/foo.xml", true},
{"application/vnd.foo-xml", true},
{"application/vnd.foo.xml", true},
{"text/foo-xml", true},
{"text/foo.xml", true},
{"text/vnd.foo-xml", true},
{"text/vnd.foo.xml", true},
} {
result := IsXMLType(test.input)
if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}
func TestWriteMultipartFormFileReaderEmpty(t *testing.T) {
w := multipart.NewWriter(bytes.NewBuffer(nil))
defer func() { _ = w.Close() }()
if err := writeMultipartFormFile(w, "foo", "bar", bytes.NewReader(nil)); err != nil {
t.Errorf("Got unexpected error: %v", err)
}
}
func TestWriteMultipartFormFileReaderError(t *testing.T) {
err := writeMultipartFormFile(nil, "", "", &brokenReadCloser{})
assertNotNil(t, err)
assertEqual(t, "read error", err.Error())
}
func TestRestyErrorFuncs(t *testing.T) {
ne1 := errors.New("new error 1")
nie1 := errors.New("inner error 1")
e := wrapErrors(ne1, nie1)
assertEqual(t, "new error 1", e.Error())
assertEqual(t, "inner error 1", errors.Unwrap(e).Error())
e = wrapErrors(ne1, nil)
assertEqual(t, "new error 1", e.Error())
e = wrapErrors(nil, nie1)
assertEqual(t, "inner error 1", e.Error())
}