-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebhook_service_test.go
More file actions
95 lines (77 loc) · 2.75 KB
/
webhook_service_test.go
File metadata and controls
95 lines (77 loc) · 2.75 KB
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
package httpsms
import (
"context"
"encoding/json"
"net/http"
"testing"
"github.com/NdoleStudio/httpsms-go/internal/helpers"
"github.com/NdoleStudio/httpsms-go/internal/stubs"
"github.com/stretchr/testify/assert"
)
func TestWebhookService_Store(t *testing.T) {
// Setup
t.Parallel()
// Arrange
apiKey := "test-api-key"
server := helpers.MakeTestServer(http.StatusOK, stubs.WebhookStoreResponse())
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
// Act
webhook, response, err := client.Webhooks.Store(context.Background(), &WebhookStoreParams{
SigningKey: "whsec_test_signing_key",
URL: "https://example.com/webhook",
PhoneNumbers: []string{"+18005550199"},
Events: []string{EventTypeMessagePhoneReceived, EventTypeMessagePhoneSent},
})
// Assert
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
jsonContent, _ := json.Marshal(webhook)
assert.JSONEq(t, string(stubs.WebhookStoreResponse()), string(jsonContent))
// Teardown
server.Close()
}
func TestWebhookService_StoreWithError(t *testing.T) {
// Setup
t.Parallel()
// Arrange
apiKey := "test-api-key"
server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse())
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
// Act
_, response, err := client.Webhooks.Store(context.Background(), &WebhookStoreParams{
SigningKey: "whsec_test_signing_key",
URL: "https://example.com/webhook",
PhoneNumbers: []string{"+18005550199"},
Events: []string{EventTypeMessagePhoneReceived},
})
// Assert
assert.NotNil(t, err)
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body))
// Teardown
server.Close()
}
func TestWebhookService_StoreRequest(t *testing.T) {
// Setup
t.Parallel()
// Arrange
apiKey := "test-api-key"
var capturedRequest http.Request
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.WebhookStoreResponse(), &capturedRequest)
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
// Act
_, _, err := client.Webhooks.Store(context.Background(), &WebhookStoreParams{
SigningKey: "whsec_test_signing_key",
URL: "https://example.com/webhook",
PhoneNumbers: []string{"+18005550199"},
Events: []string{EventTypeMessagePhoneReceived, EventTypeMessagePhoneSent},
})
// Assert
assert.Nil(t, err)
assert.Equal(t, http.MethodPost, capturedRequest.Method)
assert.Equal(t, "/v1/webhooks", capturedRequest.URL.Path)
assert.Equal(t, "application/json", capturedRequest.Header.Get("Content-Type"))
assert.Equal(t, apiKey, capturedRequest.Header.Get("x-api-key"))
// Teardown
server.Close()
}