generated from xmidt-org/.go-template
/
example_test.go
168 lines (140 loc) · 3.3 KB
/
example_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
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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package listener_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"time"
"github.com/xmidt-org/webhook-schema"
listener "github.com/xmidt-org/wrp-listener"
)
func startFakeListener() *httptest.Server {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
r.Body.Close()
var reg webhook.Registration
_ = json.Unmarshal(body, ®)
w.WriteHeader(http.StatusOK)
},
),
)
return server
}
type eventListener struct {
l *listener.Listener
}
func (el *eventListener) ServeHTTP(w http.ResponseWriter, r *http.Request) {
token, err := el.l.Tokenize(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
err = el.l.Authorize(r, token)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println(string(body))
w.WriteHeader(http.StatusOK)
}
func ExampleBasicAuth() { // nolint: govet
server := startFakeListener()
defer server.Close()
// Create the listener.
r := webhook.Registration{
Config: webhook.DeliveryConfig{
ContentType: "application/json",
},
Duration: webhook.CustomDuration(5 * time.Minute),
}
url := server.URL // replace with the URL of the webhook provider
whl, err := listener.New(url, &r,
listener.DecorateRequest(listener.DecoratorFunc(
func(r *http.Request) error {
// Add basic auth headers to the request.
r.SetBasicAuth("username", "password")
return nil
},
)),
listener.AcceptSHA1(),
listener.AcceptedSecrets("foobar", "carport"),
)
if err != nil {
panic(err)
}
el := eventListener{
l: whl,
}
go func() {
err := http.ListenAndServe(":8080", &el) // nolint: gosec
if err != nil {
panic(err)
}
}()
// Register for webhook events, using the secret "foobar" as the shared
// secret.
err = whl.Register(context.Background(), "foobar")
if err != nil {
panic(err)
}
// Output:
}
func ExampleBearerAuth() { // nolint: govet
server := startFakeListener()
defer server.Close()
// Create the listener.
r := webhook.Registration{
Config: webhook.DeliveryConfig{
ContentType: "application/json",
},
Duration: webhook.CustomDuration(5 * time.Minute),
}
sharedSecret := strings.Split(os.Getenv("SHARED_SECRET"), ",")
for i := range sharedSecret {
sharedSecret[i] = strings.TrimSpace(sharedSecret[i])
}
url := server.URL // replace with the URL of the webhook provider
whl, err := listener.New(url, &r,
listener.DecorateRequest(listener.DecoratorFunc(
func(r *http.Request) error {
r.Header.Set("Authorization", "Bearer "+os.Getenv("BEARER_TOKEN"))
return nil
},
)),
listener.AcceptSHA1(),
listener.AcceptedSecrets(sharedSecret...),
)
if err != nil {
panic(err)
}
el := eventListener{
l: whl,
}
go func() {
err := http.ListenAndServe(":8081", &el) // nolint: gosec
if err != nil {
panic(err)
}
}()
// Register for webhook events, using the secret "foobar" as the shared
// secret.
err = whl.Register(context.Background(), "foobar")
if err != nil {
panic(err)
}
// Output:
}