-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
function_execute_test.go
80 lines (67 loc) · 1.59 KB
/
function_execute_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
package slack
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
)
func postHandler(t *testing.T) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
t.Error(err)
return
}
var req FunctionCompleteSuccessRequest
err = json.Unmarshal(body, &req)
if err != nil {
t.Error(err)
return
}
switch req.FunctionExecutionID {
case "function-success":
postSuccess(rw, r)
case "function-failure":
postFailure(rw, r)
}
}
}
func postSuccess(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": true
}`)
rw.Write(response)
}
func postFailure(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": false,
"error": "function_execution_not_found"
}`)
rw.Write(response)
rw.WriteHeader(500)
}
func TestFunctionComplete(t *testing.T) {
http.HandleFunc("/functions.completeSuccess", postHandler(t))
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
err := api.FunctionCompleteSuccess("function-success")
if err != nil {
t.Error(err)
}
err = api.FunctionCompleteSuccess("function-failure")
if err == nil {
t.Fail()
}
err = api.FunctionCompleteSuccessContext(context.Background(), "function-success")
if err != nil {
t.Error(err)
}
err = api.FunctionCompleteSuccessContext(context.Background(), "function-failure")
if err == nil {
t.Fail()
}
}