This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_test.go
95 lines (80 loc) · 2.51 KB
/
callback_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
package workwave
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
qt "github.com/frankban/quicktest"
)
func TestCallbackGet(t *testing.T) {
setup()
defer teardown()
c := qt.New(t)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"url": "https://my.server.com/callback"}`)
})
client, _ := New("api-key")
client.baseURL, _ = url.Parse(server.URL)
cb, err := client.Callback.Get(ctx, Callback{
URL: "https://my.server.com/callback",
})
c.Assert(err, qt.IsNil)
c.Assert(cb.URL, qt.Equals, "https://my.server.com/callback")
}
func TestCallbackSet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var rBody Callback
if err := json.NewDecoder(r.Body).Decode(&rBody); err != nil {
t.Fatal("failed to parse request body")
}
switch {
case rBody.Test: // setting Test returns an error to check for it
fmt.Fprintf(w, `{"errorCode": 2000, "errorMessage": "Server at URL [https://my.server.com/callback] failed to respond to the test message."}`)
return
default:
fmt.Fprintf(w, `{"url":"https://my.server.com/new-callback", "previousUrl":"https://my.server.com/callback"}`)
return
}
})
client, _ := New("api-key")
client.baseURL, _ = url.Parse(server.URL)
t.Run("simple", func(t *testing.T) {
c := qt.New(t)
cb, err := client.Callback.Set(ctx, Callback{
URL: "https://my.server.com/new-callback",
})
c.Assert(err, qt.IsNil)
c.Assert(cb.URL, qt.Equals, "https://my.server.com/new-callback")
c.Assert(cb.PreviousURL, qt.Equals, "https://my.server.com/callback")
})
t.Run("with test and error response", func(t *testing.T) {
c := qt.New(t)
cb, err := client.Callback.Set(ctx, Callback{
URL: "https://my.server.com/new-callback",
Test: true,
})
c.Assert(err, qt.ErrorMatches, "failed to set callback.*")
c.Assert(cb, qt.DeepEquals, Callback{
ErrorCode: 2000,
ErrorMessage: "Server at URL [https://my.server.com/callback] failed to respond to the test message.",
})
})
}
func TestCallbackDelete(t *testing.T) {
setup()
defer teardown()
c := qt.New(t)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"previousUrl": "https://my.server.com/callback"}`)
})
client, _ := New("api-key")
client.baseURL, _ = url.Parse(server.URL)
cb, err := client.Callback.Delete(ctx, Callback{
URL: "https://my.server.com/callback",
})
c.Assert(err, qt.IsNil)
c.Assert(cb.PreviousURL, qt.Equals, "https://my.server.com/callback")
}