forked from xmidt-org/scytale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WRPHandler_test.go
118 lines (100 loc) · 2.96 KB
/
WRPHandler_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
// SPDX-FileCopyrightText: 2019 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/wrp-go/v3/wrphttp"
)
func TestNewFanoutHandler(t *testing.T) {
assert := assert.New(t)
assert.Panics(func() {
newWRPFanoutHandler(nil)
})
assert.NotPanics(func() {
assert.NotNil(newWRPFanoutHandler(http.NotFoundHandler()))
})
}
func TestNewWRPFanoutHandlerWithPIDCheck(t *testing.T) {
assert := assert.New(t)
assert.Panics(func() {
newWRPFanoutHandlerWithPIDCheck(http.NotFoundHandler(), nil)
})
assert.Panics(func() {
newWRPFanoutHandlerWithPIDCheck(nil, &wrpPartnersAccess{})
})
}
func TestFanoutRequest(t *testing.T) {
testCases := []struct {
Name string
Recorder *httptest.ResponseRecorder
Entity *wrphttp.Entity
Err error
ExpectedCode int
Modify bool
}{
{
Name: "wrp check errors",
Recorder: httptest.NewRecorder(),
Entity: new(wrphttp.Entity),
Err: ErrTokenMissing,
ExpectedCode: 500,
},
{
Name: "wrp gets modified - happy path",
Modify: true,
Recorder: httptest.NewRecorder(),
Entity: &wrphttp.Entity{
Format: wrp.Msgpack,
Message: wrp.Message{
Destination: "mac:1122334455/service",
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
assert := assert.New(t)
mockWRPAccessAuthority := new(mockWRPAccessAuthority)
wrpFanoutHandler := newWRPFanoutHandlerWithPIDCheck(
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}), mockWRPAccessAuthority)
wrpResponseWriter := newTestWRPResponseWriter(testCase.Recorder)
r := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
wrpRequest := &wrphttp.Request{
Original: r,
Entity: testCase.Entity,
}
testEntity := testCase.Entity.Message
mockWRPAccessAuthority.On("authorizeWRP", r.Context(), &testEntity).Return(testCase.Modify, testCase.Err)
wrpFanoutHandler.ServeWRP(wrpResponseWriter, wrpRequest)
if testCase.Err != nil {
assert.Equal(testCase.ExpectedCode, testCase.Recorder.Code)
} else {
outgoingBody, err := io.ReadAll(r.Body)
assert.Nil(err)
assert.Equal(int64(len(outgoingBody)), r.ContentLength)
assert.Equal(testCase.Entity.Format.ContentType(), r.Header.Get("Content-Type"))
assert.Equal(testCase.Entity.Message.Destination, r.Header.Get("X-Webpa-Device-Name"))
}
})
}
}
func TestNonWRPResponseWriterFactory(t *testing.T) {
assert := assert.New(t)
w, err := nonWRPResponseWriterFactory(nil, nil)
assert.NoError(err)
require.NotNil(t, w)
a, err := w.WriteWRP(nil)
assert.NoError(err)
assert.Zero(a)
b, err := w.WriteWRPBytes(wrp.Msgpack, nil)
assert.NoError(err)
assert.Zero(b)
c := w.WRPFormat()
assert.Equal(wrp.Msgpack, c)
}