-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_test.go
71 lines (49 loc) · 1.28 KB
/
receiver_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
package github
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"strconv"
"testing"
"github.com/whosonfirst/go-webhookd/v3/receiver"
)
func TestGitHubReceiver(t *testing.T) {
secret := "s33kret"
receiver_uri := fmt.Sprintf("github://?secret=%s", secret)
ctx := context.Background()
r, err := receiver.NewReceiver(ctx, receiver_uri)
if err != nil {
t.Fatalf("Failed to create new receiver, %v", err)
}
msg := "fixtures/events/push.json"
fh, err := os.Open(msg)
if err != nil {
t.Fatalf("Failed to open %s, %v", msg, err)
}
defer fh.Close()
body, err := io.ReadAll(fh)
if err != nil {
t.Fatalf("Failed to read %s, %v", msg, err)
}
sig, err := GenerateSignature(string(body), secret)
if err != nil {
t.Fatalf("Failed to generate signature, %v", err)
}
req, err := http.NewRequest("POST", "http://localhost:8080/github", bytes.NewReader(body))
if err != nil {
t.Fatalf("Failed to create new request, %v", err)
}
req.Header.Set("X-GitHub-Event", "debug")
req.Header.Set("X-Hub-Signature", sig)
req.Header.Add("Content-Length", strconv.Itoa(len(body)))
body2, err2 := r.Receive(ctx, req)
if err2 != nil {
t.Fatalf("Failed to receive message, %v", err)
}
if !bytes.Equal(body2, body) {
t.Fatalf("Unexpected output '%s'", string(body2))
}
}