-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
57 lines (52 loc) · 1.47 KB
/
main_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
package main
import (
"net/http"
"os"
"testing"
"github.com/calvinmclean/babyapi"
babytest "github.com/calvinmclean/babyapi/test"
)
func TestAPI(t *testing.T) {
defer os.RemoveAll("storage.json")
os.Setenv("STORAGE_FILE", "storage.json")
api := createAPI()
babytest.RunTableTest(t, api, []babytest.TestCase[*babyapi.AnyResource]{
{
Name: "CreateSNACK",
Test: babytest.RequestTest[*babyapi.AnyResource]{
Method: http.MethodPost,
Body: `{"Title": "New SNACK"}`,
},
ExpectedResponse: babytest.ExpectedResponse{
Status: http.StatusCreated,
BodyRegexp: `{"id":"[0-9a-v]{20}","Title":"New SNACK","Description":"","Eaten":null}`,
},
},
{
Name: "GetSNACK",
Test: babytest.RequestTest[*babyapi.AnyResource]{
Method: http.MethodGet,
IDFunc: func(getResponse babytest.PreviousResponseGetter) string {
return getResponse("CreateSNACK").Data.GetID()
},
},
ExpectedResponse: babytest.ExpectedResponse{
Status: http.StatusOK,
BodyRegexp: `{"id":"[0-9a-v]{20}","Title":"New SNACK","Description":"","Eaten":null}`,
},
},
{
Name: "DeleteSNACK",
Test: babytest.RequestTest[*babyapi.AnyResource]{
Method: http.MethodDelete,
IDFunc: func(getResponse babytest.PreviousResponseGetter) string {
return getResponse("CreateSNACK").Data.GetID()
},
},
ExpectedResponse: babytest.ExpectedResponse{
Status: http.StatusForbidden, // TEMP: should be http.StatusOK
NoBody: true,
},
},
})
}