-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_test.go
131 lines (125 loc) · 2.89 KB
/
middleware_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package soajsgo
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRegistry_Middleware(t *testing.T) {
tt := []struct {
name string
headerInfo string
reg Registry
expectedSoaData ContextData
}{
{
name: "bad header",
headerInfo: "nil",
reg: Registry{},
expectedSoaData: ContextData{},
},
{
name: "empty header",
headerInfo: "",
reg: Registry{},
expectedSoaData: ContextData{},
},
{
name: "all ok",
headerInfo: `{"device":"iPhone"}`,
reg: Registry{Name: "ok"},
expectedSoaData: ContextData{Device: "iPhone", Reg: &Registry{Name: "ok"}},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
soa := r.Context().Value(SoajsKey)
if soa != nil {
assert.Equal(t, tc.expectedSoaData, soa.(ContextData))
} else {
assert.Nil(t, soa)
}
_, _ = w.Write([]byte("ok"))
})
req := httptest.NewRequest("", "http://localhost:8080/", nil)
req.Header.Set(headerDataName, tc.headerInfo)
rec := httptest.NewRecorder()
middleware := tc.reg.Middleware(handler)
middleware.ServeHTTP(rec, req)
})
}
}
func TestHeaderData(t *testing.T) {
tt := []struct {
name string
data string
expectedInfo *headerInfo
expectedErr error
}{
{
name: "bad header",
data: "nil",
expectedInfo: nil,
expectedErr: errors.New("unable to parse SOAJS header: invalid character 'i' in literal null (expecting 'u')"),
},
{
name: "all ok",
data: `{"device":"iPhone"}`,
expectedInfo: &headerInfo{Device: "iPhone"},
expectedErr: nil,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest("", "http://localhost:8080/", nil)
req.Header.Set(headerDataName, tc.data)
info, err := headerData(req)
assert.Equal(t, tc.expectedErr, err)
assert.Equal(t, tc.expectedInfo, info)
})
}
}
func TestHost_Path(t *testing.T) {
tt := []struct {
name string
host Host
args []string
expectedPath string
}{
{
name: "1",
host: Host{
Host: "localhost",
Port: 8080,
},
args: []string{"test"},
expectedPath: "localhost:8080/",
},
{
name: "2",
host: Host{
Host: "localhost",
Port: 8080,
},
args: []string{"CONTROLLER", "v"},
expectedPath: "localhost:8080/CONTROLLER/",
},
{
name: "3",
host: Host{
Host: "localhost",
Port: 8080,
},
args: []string{"CONTROLLER", "1", "-"},
expectedPath: "localhost:8080/CONTROLLER/v1/",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
p := tc.host.Path(tc.args...)
assert.Equal(t, tc.expectedPath, p)
})
}
}