-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathcrumb_test.go
43 lines (40 loc) · 2.97 KB
/
crumb_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
package proute
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectCrumb_Value(t *testing.T) {
type fields struct {
Name string
Elements []Crumb
}
type args struct {
opts []CrumbOption
}
tests := []struct {
name string
fields fields
args args
want string
}{
{"json empty", fields{"", []Crumb{}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeJSON)}}, "{}"},
{"json simple int", fields{"", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeJSON)}}, `{"key":123}`},
{"json nested array", fields{"", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}, ArrayCrumb{Name: "array", Element: IntCrumb{Val: 123, Fixed: true}}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeJSON)}}, `{"key":123,"array":[123]}`},
{"json dupe keys", fields{"", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}, ArrayCrumb{Name: "key", Element: IntCrumb{Val: 123, Fixed: true}}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeJSON)}}, `{"key":123,"key":[123]}`},
{"xml simple int", fields{"root", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeXML)}}, `<?xml version="1.0" encoding="UTF-8"?><root><key>123</key></root>`},
{"xml nestedArray", fields{"root", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}, ArrayCrumb{Name: "array", Element: IntCrumb{Name: "", Val: 123, Fixed: true}}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeXML)}}, `<?xml version="1.0" encoding="UTF-8"?><root><key>123</key><array><array>123</array></array></root>`},
{"xml nestedArrayObject", fields{"root", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}, ArrayCrumb{Name: "array", Element: ObjectCrumb{Name: "innerobj", Elements:[]Crumb{StaticCrumb{K: "innerkey", V:"innerv"}}}}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeXML)}}, `<?xml version="1.0" encoding="UTF-8"?><root><key>123</key><array><innerobj><innerkey>innerv</innerkey></innerobj></array></root>`},
{"formdata simple int", fields{"", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeFormData)}}, "--hahahahahformboundaryhahahaha\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\n123\r\n--hahahahahformboundaryhahahaha--\r\n"},
{"formdata nested array", fields{"", []Crumb{IntCrumb{Name: "key", Val: 123, Fixed: true}, ArrayCrumb{Name: "array", Element: IntCrumb{Val: 123, Fixed: true}}}}, args{[]CrumbOption{CrumbOptContentType(ContentTypeFormData)}}, "--hahahahahformboundaryhahahaha\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\n123\r\n--hahahahahformboundaryhahahaha\r\nContent-Disposition: form-data; name=\"array\"\r\n\r\narray=123\r\n--hahahahahformboundaryhahahaha--\r\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := ObjectCrumb{
Name: tt.fields.Name,
Elements: tt.fields.Elements,
}
got := o.Value(tt.args.opts...)
assert.Equal(t, tt.want, got)
})
}
}