-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdk_test.go
67 lines (58 loc) · 1.71 KB
/
sdk_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
package fdk_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
fdk "github.com/CrowdStrike/foundry-fn-go"
)
func TestAPIError(t *testing.T) {
errs := []fdk.APIError{
{Code: http.StatusInternalServerError, Message: "some internal error"},
{Code: http.StatusBadRequest, Message: "user dorked it up"},
{Message: "missing code will print a zero"},
}
for _, err := range errs {
want := fmt.Sprintf("[%d] %s", err.Code, err.Message)
fdk.EqualVals(t, want, err.Error())
}
}
func TestHandlerFnOfOK(t *testing.T) {
h := fdk.HandlerFnOfOK(func(ctx context.Context, r fdk.RequestOf[okReq]) fdk.Response {
return fdk.Response{Code: http.StatusOK}
})
b, err := json.Marshal(okReq{
Shoulds: []int{
http.StatusOK, // should not cause an error
http.StatusBadRequest, // should return a 400 error
http.StatusInternalServerError, // should return a 500 error
},
})
mustNoErr(t, err)
resp := h.Handle(context.TODO(), fdk.Request{Body: bytes.NewReader(b)})
fdk.EqualVals(t, http.StatusInternalServerError, resp.Code)
wantErrs := []fdk.APIError{
{Code: http.StatusBadRequest, Message: http.StatusText(http.StatusBadRequest)},
{Code: http.StatusInternalServerError, Message: http.StatusText(http.StatusInternalServerError)},
}
if !fdk.EqualVals(t, len(wantErrs), len(resp.Errors)) {
return
}
fdk.EqualVals(t, wantErrs[0], resp.Errors[0])
fdk.EqualVals(t, wantErrs[1], resp.Errors[1])
}
type okReq struct {
Shoulds []int `json:"shoulds"`
}
func (r okReq) OK() []fdk.APIError {
var out []fdk.APIError
for _, sh := range r.Shoulds {
if sh < 400 {
continue
}
out = append(out, fdk.APIError{Code: sh, Message: http.StatusText(sh)})
}
return out
}