(t *testing.T)
| 24 | ) |
| 25 | |
| 26 | func TestWebhook(t *testing.T) { |
| 27 | t.Parallel() |
| 28 | |
| 29 | const ( |
| 30 | titlePlaintext = "this is the title" |
| 31 | titleMarkdown = "this *is* _the_ title" |
| 32 | bodyPlaintext = "this is the body" |
| 33 | bodyMarkdown = "~this~ is the `body`" |
| 34 | ) |
| 35 | |
| 36 | msgPayload := types.MessagePayload{ |
| 37 | Version: "1.0", |
| 38 | NotificationName: "test", |
| 39 | } |
| 40 | |
| 41 | tests := []struct { |
| 42 | name string |
| 43 | serverURL string |
| 44 | serverDeadline time.Time |
| 45 | serverFn func(uuid.UUID, http.ResponseWriter, *http.Request) |
| 46 | |
| 47 | expectSuccess bool |
| 48 | expectRetryable bool |
| 49 | expectErr string |
| 50 | }{ |
| 51 | { |
| 52 | name: "successful", |
| 53 | serverFn: func(msgID uuid.UUID, w http.ResponseWriter, r *http.Request) { |
| 54 | var payload dispatch.WebhookPayload |
| 55 | err := json.NewDecoder(r.Body).Decode(&payload) |
| 56 | assert.NoError(t, err) |
| 57 | assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 58 | assert.Equal(t, msgID, payload.MsgID) |
| 59 | assert.Equal(t, msgID.String(), r.Header.Get("X-Message-Id")) |
| 60 | |
| 61 | assert.Equal(t, titlePlaintext, payload.Title) |
| 62 | assert.Equal(t, titleMarkdown, payload.TitleMarkdown) |
| 63 | assert.Equal(t, bodyPlaintext, payload.Body) |
| 64 | assert.Equal(t, bodyMarkdown, payload.BodyMarkdown) |
| 65 | |
| 66 | w.WriteHeader(http.StatusOK) |
| 67 | _, err = w.Write([]byte(fmt.Sprintf("received %s", payload.MsgID))) |
| 68 | assert.NoError(t, err) |
| 69 | }, |
| 70 | expectSuccess: true, |
| 71 | }, |
| 72 | { |
| 73 | name: "invalid endpoint", |
| 74 | // Build a deliberately invalid URL to fail validation. |
| 75 | serverURL: "invalid .com", |
| 76 | expectSuccess: false, |
| 77 | expectErr: "invalid URL escape", |
| 78 | expectRetryable: false, |
| 79 | }, |
| 80 | { |
| 81 | name: "timeout", |
| 82 | serverDeadline: time.Now().Add(-time.Hour), |
| 83 | expectSuccess: false, |
nothing calls this directly
no test coverage detected