-
Notifications
You must be signed in to change notification settings - Fork 406
/
bebmock.go
233 lines (208 loc) · 6.47 KB
/
bebmock.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package testing
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"path"
"strings"
"golang.org/x/oauth2"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo" // nolint
. "github.com/onsi/gomega" // nolint
logf "sigs.k8s.io/controller-runtime/pkg/log"
// gcp auth etc.
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/kyma-project/kyma/components/eventing-controller/pkg/ems/api/events/config"
bebtypes "github.com/kyma-project/kyma/components/eventing-controller/pkg/ems/api/events/types"
)
const (
TokenURLPath = "/auth"
MessagingURLPath = "/messaging"
)
// BEBMock implements a programmable mock for BEB
type BEBMock struct {
Requests *SafeRequests
Subscriptions *SafeSubscriptions
TokenURL string
MessagingURL string
BEBConfig *config.Config
log logr.Logger
AuthResponse response
GetResponse responseWithName
ListResponse response
CreateResponse response
DeleteResponse response
}
func NewBEBMock(bebConfig *config.Config) *BEBMock {
logger := logf.Log.WithName("beb mock")
return &BEBMock{
NewSafeRequests(), NewSafeSubscriptions(), "", "", bebConfig,
logger,
nil, nil, nil, nil, nil,
}
}
type responseWithName func(w http.ResponseWriter, subscriptionName string)
type response func(w http.ResponseWriter)
func (m *BEBMock) Reset() {
m.log.Info("Initializing requests")
//TODO clean when done
//m.Requests = make(map[*http.Request]interface{})
m.Requests = NewSafeRequests()
m.Subscriptions = NewSafeSubscriptions()
m.AuthResponse = nil
m.GetResponse = nil
m.ListResponse = nil
m.CreateResponse = nil
m.DeleteResponse = nil
}
func (m *BEBMock) Start() string {
m.Reset()
// implementation based on https://pages.github.tools.sap/KernelServices/APIDefinitions/?urls.primaryName=Business%20Event%20Bus%20-%20CloudEvents
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// store request
//TODO clean when done
//m.Requests[r] = nil
m.Requests.StoreRequest(r)
description := ""
reqBytes, err := httputil.DumpRequest(r, true)
if err == nil {
description = string(reqBytes)
}
m.log.V(1).Info("received request",
"uri", r.RequestURI,
"method", r.Method,
"description", description,
)
w.Header().Set("Content-Type", "application/json")
// oauth2 request
if r.Method == http.MethodPost && strings.HasPrefix(r.RequestURI, TokenURLPath) {
if m.AuthResponse != nil {
m.AuthResponse(w)
} else {
BEBAuthResponseSuccess(w)
}
return
}
// messaging API request
if strings.HasPrefix(r.RequestURI, MessagingURLPath) {
switch r.Method {
case http.MethodDelete:
key := r.URL.Path
m.Subscriptions.DeleteSubscription(key)
if m.DeleteResponse != nil {
m.DeleteResponse(w)
} else {
BEBDeleteResponseSuccess(w)
}
case http.MethodPost:
var subscription bebtypes.Subscription
_ = json.NewDecoder(r.Body).Decode(&subscription)
//TODO clean
//m.Requests[r] = subscription
m.Requests.PutSubscription(r, subscription)
key := r.URL.Path + "/" + subscription.Name
m.Subscriptions.PutSubscription(key, &subscription)
if m.CreateResponse != nil {
m.CreateResponse(w)
} else {
BEBCreateSuccess(w)
}
case http.MethodGet:
switch r.RequestURI {
case m.BEBConfig.ListURL:
if m.ListResponse != nil {
m.ListResponse(w)
} else {
BEBListSuccess(w)
}
// get on a single subscription
default:
key := r.URL.Path
if m.GetResponse != nil {
m.GetResponse(w, key)
} else {
subscriptionSaved := m.Subscriptions.GetSubscription(key)
if subscriptionSaved != nil {
subscriptionSaved.SubscriptionStatus = bebtypes.SubscriptionStatusActive
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(*subscriptionSaved)
Expect(err).ShouldNot(HaveOccurred())
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}
return
default:
w.WriteHeader(http.StatusNotImplemented)
}
return
}
}))
uri := ts.URL
return uri
}
// BEBAuthResponseSuccess writes a oauth2 authentication response to the writer for the happy-path.
func BEBAuthResponseSuccess(w http.ResponseWriter) {
token := oauth2.Token{
AccessToken: "some-token",
TokenType: "",
RefreshToken: "",
}
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(token)
Expect(err).ShouldNot(HaveOccurred())
}
// BEBCreateSuccess writes a response to the writer for the happy-path of creating a BEB subscription.
func BEBCreateSuccess(w http.ResponseWriter) {
w.WriteHeader(http.StatusAccepted)
response := bebtypes.CreateResponse{
Response: bebtypes.Response{
StatusCode: http.StatusAccepted,
Message: "",
},
Href: "",
}
err := json.NewEncoder(w).Encode(response)
Expect(err).ShouldNot(HaveOccurred())
}
// BEBGetSuccess writes a response to the writer for the happy-path of getting a BEB subscription.
func BEBGetSuccess(w http.ResponseWriter, name string) {
w.WriteHeader(http.StatusOK)
s := bebtypes.Subscription{
Name: name,
SubscriptionStatus: bebtypes.SubscriptionStatusActive,
}
err := json.NewEncoder(w).Encode(s)
Expect(err).ShouldNot(HaveOccurred())
}
// BEBListSuccess writes a response to the writer for the happy-path of listing a BEB subscription.
func BEBListSuccess(w http.ResponseWriter) {
w.WriteHeader(http.StatusAccepted)
response := bebtypes.Response{
StatusCode: http.StatusOK,
Message: "",
}
err := json.NewEncoder(w).Encode(response)
Expect(err).ShouldNot(HaveOccurred())
}
// BEBDeleteResponseSuccess writes a response to the writer for the happy-path of deleting a BEB subscription.
func BEBDeleteResponseSuccess(w http.ResponseWriter) {
w.WriteHeader(http.StatusNoContent)
}
// IsBEBSubscriptionCreate determines if the http request is creating a BEB subscription.
func IsBEBSubscriptionCreate(r *http.Request, bebConfig config.Config) bool {
return r.Method == http.MethodPost && strings.Contains(bebConfig.CreateURL, r.RequestURI)
}
// IsBEBSubscriptionDelete determines if the http request is deleting a BEB subscription.
func IsBEBSubscriptionDelete(r *http.Request) bool {
return r.Method == http.MethodDelete && strings.Contains(r.RequestURI, MessagingURLPath)
}
// GetRestAPIObject gets the name of the involved object in a REST url
// e.g. "/messaging/events/subscriptions/{subscriptionName}" => "{subscriptionName}"
func GetRestAPIObject(u *url.URL) string {
return path.Base(u.Path)
}