-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathclient_test.go
More file actions
91 lines (84 loc) · 2.16 KB
/
client_test.go
File metadata and controls
91 lines (84 loc) · 2.16 KB
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
package openapi
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParameterToString(t *testing.T) {
var testCases = []struct {
name string
obj interface{}
format string
para string
}{
{
name: "strings csv",
obj: []string{"red", "blue", "yellow"},
format: "csv",
para: "red,blue,yellow",
},
{
name: "strings pipes",
obj: []string{"red", "blue", "yellow"},
format: "pipes",
para: "red|blue|yellow",
},
{
name: "strings ssv",
obj: []string{"red", "blue", "yellow"},
format: "ssv",
para: "red blue yellow",
},
{
name: "strings json",
obj: []string{"red", "blue", "yellow"},
format: "application/json",
para: "[\"red\",\"blue\",\"yellow\"]",
},
{
name: "obj json",
obj: struct {
Name string `json:"name"`
Age int `json:"age"`
Active bool `json:"active"`
}{
Name: "free5GC",
Age: 3,
Active: true,
},
format: "application/json",
para: "{\"name\":\"free5GC\",\"age\":3,\"active\":true}",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
para := ParameterToString(tc.obj, tc.format)
require.Equal(t, tc.para, para)
})
}
}
// TestMultipartDeserialize_MalformedBody pins the fix for free5gc/free5gc#1026.
// Before the fix, a body whose Content-Type declared multipart/related but
// whose payload was not a valid MIME multipart stream would panic on
// part.Header.Get in MultipartDeserialize because mime/multipart.NextPart can
// return a nil *Part alongside a non-EOF error. The SMF then surfaced HTTP
// 500 on every malformed POST /nsmf-pdusession/v1/sm-contexts.
func TestMultipartDeserialize_MalformedBody(t *testing.T) {
type dummy struct{}
cases := []struct {
name string
body []byte
}{
{name: "plain text body", body: []byte("hello, not multipart")},
{name: "empty body", body: []byte("")},
{name: "json body", body: []byte(`{"foo":"bar"}`)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var out dummy
require.NotPanics(t, func() {
err := MultipartDeserialize(tc.body, &out, "boundaryX")
require.Error(t, err)
})
})
}
}