MCPcopy
hub / github.com/kopia/kopia / TestWebhook

Function TestWebhook

notification/sender/webhook/webhook_sender_test.go:18–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestWebhook(t *testing.T) {
19 ctx := testlogging.Context(t)
20
21 mux := http.NewServeMux()
22
23 var requests []*http.Request
24
25 var requestBodies []bytes.Buffer
26
27 mux.HandleFunc("/some-path", func(w http.ResponseWriter, r *http.Request) {
28 var b bytes.Buffer
29
30 io.Copy(&b, r.Body)
31
32 requestBodies = append(requestBodies, b)
33 requests = append(requests, r)
34 })
35
36 server := httptest.NewServer(mux)
37 defer server.Close()
38
39 p, err := sender.GetSender(ctx, "my-profile", "webhook", &webhook.Options{
40 Endpoint: server.URL + "/some-path",
41 Method: "POST",
42 Headers: "X-Some: thing\nX-Another-Header: z",
43 })
44 require.NoError(t, err)
45
46 require.NoError(t, p.Send(ctx, &sender.Message{
47 Subject: "Test",
48 Body: "This is a test.\n\n* one\n* two\n* three\n\n# Header\n## Subheader\n\n- a\n- b\n- c",
49 Headers: map[string]string{
50 "X-Some-Header": "x",
51 "Content-Type": "some/content-type",
52 },
53 }))
54
55 p2, err := sender.GetSender(ctx, "my-profile", "webhook", &webhook.Options{
56 Endpoint: server.URL + "/some-path",
57 Method: "PUT",
58 Headers: "X-Another-Header: y",
59 Format: "html",
60 })
61 require.NoError(t, err)
62
63 require.NoError(t, p2.Send(ctx, &sender.Message{
64 Subject: "Test 2",
65 Body: "This is a test.\n\n* one\n* two\n* three",
66 Headers: map[string]string{
67 "Content-Type": "text/html",
68 },
69 }))
70
71 require.Len(t, requests, 2)
72
73 // first request - POST in md format
74 require.Equal(t, "some/content-type", requests[0].Header.Get("Content-Type"))
75 require.Equal(t, "x", requests[0].Header.Get("X-Some-Header"))

Callers

nothing calls this directly

Calls 10

ContextFunction · 0.92
GetSenderFunction · 0.92
EqualMethod · 0.80
CloseMethod · 0.65
SendMethod · 0.65
GetMethod · 0.65
SummaryMethod · 0.65
LenMethod · 0.45
StringMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected