-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
75 lines (56 loc) · 1.35 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"strings"
"testing"
"github.com/danielgtaylor/huma/v2/humatest"
)
func TestGetHealth(t *testing.T) {
_, api := humatest.New(t)
addRoutes(api)
resp := api.Get("/")
if !strings.Contains(resp.Body.String(), `"status":"ok"`) {
t.Fatalf("Unexpected response: %s", resp.Body.String())
}
}
func TestGetGreeting(t *testing.T) {
_, api := humatest.New(t)
addRoutes(api)
resp := api.Get("/greeting/itman")
if !strings.Contains(resp.Body.String(), "Hello, itman!") {
t.Fatalf("Unexpected response: %s", resp.Body.String())
}
}
func TestPutReview(t *testing.T) {
_, api := humatest.New(t)
addRoutes(api)
resp := api.Post("/reviews", map[string]any{
"author": "dunghd",
"rating": 5,
})
if resp.Code != 201 {
t.Fatalf("Unexpected status code: %d", resp.Code)
}
}
func TestPutReviewError(t *testing.T) {
_, api := humatest.New(t)
addRoutes(api)
resp := api.Post("/reviews", map[string]any{
"rating": 10,
})
if resp.Code != 422 {
t.Fatalf("Unexpected status code: %d", resp.Code)
}
}
func TestGetReviews(t *testing.T) {
_, api := humatest.New(t)
addRoutes(api)
// Setup a review
api.Post("/reviews", map[string]any{
"author": "dunghd",
"rating": 5,
})
resp := api.Get("/reviews")
if !strings.Contains(resp.Body.String(), `"author":"dunghd"`) {
t.Fatalf("Unexpected response: %s", resp.Body.String())
}
}