-
Notifications
You must be signed in to change notification settings - Fork 802
/
petstore_test.go
140 lines (112 loc) · 3.92 KB
/
petstore_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/deepmap/oapi-codegen/examples/petstore-expanded/gin/api"
"github.com/deepmap/oapi-codegen/pkg/testutil"
"github.com/stretchr/testify/assert"
)
func doGet(t *testing.T, handler http.Handler, url string) *httptest.ResponseRecorder {
response := testutil.NewRequest().Get(url).WithAcceptJson().GoWithHTTPHandler(t, handler)
return response.Recorder
}
func TestPetStore(t *testing.T) {
var err error
store := api.NewPetStore()
ginPetServer := NewGinPetServer(store, 8080)
r := ginPetServer.Handler
t.Run("Add pet", func(t *testing.T) {
tag := "TagOfSpot"
newPet := api.NewPet{
Name: "Spot",
Tag: &tag,
}
rr := testutil.NewRequest().Post("/pets").WithJsonBody(newPet).GoWithHTTPHandler(t, r).Recorder
assert.Equal(t, http.StatusCreated, rr.Code)
var resultPet api.Pet
err = json.NewDecoder(rr.Body).Decode(&resultPet)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, newPet.Name, resultPet.Name)
assert.Equal(t, *newPet.Tag, *resultPet.Tag)
})
t.Run("Find pet by ID", func(t *testing.T) {
pet := api.Pet{
Id: 100,
}
store.Pets[pet.Id] = pet
rr := doGet(t, r, fmt.Sprintf("/pets/%d", pet.Id))
var resultPet api.Pet
err = json.NewDecoder(rr.Body).Decode(&resultPet)
assert.NoError(t, err, "error getting pet")
assert.Equal(t, pet, resultPet)
})
t.Run("Pet not found", func(t *testing.T) {
rr := doGet(t, r, "/pets/27179095781")
assert.Equal(t, http.StatusNotFound, rr.Code)
var petError api.Error
err = json.NewDecoder(rr.Body).Decode(&petError)
assert.NoError(t, err, "error getting response", err)
assert.Equal(t, int32(http.StatusNotFound), petError.Code)
})
t.Run("List all pets", func(t *testing.T) {
store.Pets = map[int64]api.Pet{1: {}, 2: {}}
// Now, list all pets, we should have two
rr := doGet(t, r, "/pets")
assert.Equal(t, http.StatusOK, rr.Code)
var petList []api.Pet
err = json.NewDecoder(rr.Body).Decode(&petList)
assert.NoError(t, err, "error getting response", err)
assert.Equal(t, 2, len(petList))
})
t.Run("Filter pets by tag", func(t *testing.T) {
tag := "TagOfFido"
store.Pets = map[int64]api.Pet{
1: {
Tag: &tag,
},
2: {},
}
// Filter pets by tag, we should have 1
rr := doGet(t, r, "/pets?tags=TagOfFido")
assert.Equal(t, http.StatusOK, rr.Code)
var petList []api.Pet
err = json.NewDecoder(rr.Body).Decode(&petList)
assert.NoError(t, err, "error getting response", err)
assert.Equal(t, 1, len(petList))
})
t.Run("Filter pets by tag", func(t *testing.T) {
store.Pets = map[int64]api.Pet{1: {}, 2: {}}
// Filter pets by non existent tag, we should have 0
rr := doGet(t, r, "/pets?tags=NotExists")
assert.Equal(t, http.StatusOK, rr.Code)
var petList []api.Pet
err = json.NewDecoder(rr.Body).Decode(&petList)
assert.NoError(t, err, "error getting response", err)
assert.Equal(t, 0, len(petList))
})
t.Run("Delete pets", func(t *testing.T) {
store.Pets = map[int64]api.Pet{1: {}, 2: {}}
// Let's delete non-existent pet
rr := testutil.NewRequest().Delete("/pets/7").GoWithHTTPHandler(t, r).Recorder
assert.Equal(t, http.StatusNotFound, rr.Code)
var petError api.Error
err = json.NewDecoder(rr.Body).Decode(&petError)
assert.NoError(t, err, "error unmarshaling PetError")
assert.Equal(t, int32(http.StatusNotFound), petError.Code)
// Now, delete both real pets
rr = testutil.NewRequest().Delete("/pets/1").GoWithHTTPHandler(t, r).Recorder
assert.Equal(t, http.StatusNoContent, rr.Code)
rr = testutil.NewRequest().Delete("/pets/2").GoWithHTTPHandler(t, r).Recorder
assert.Equal(t, http.StatusNoContent, rr.Code)
// Should have no pets left.
var petList []api.Pet
rr = doGet(t, r, "/pets")
assert.Equal(t, http.StatusOK, rr.Code)
err = json.NewDecoder(rr.Body).Decode(&petList)
assert.NoError(t, err, "error getting response", err)
assert.Equal(t, 0, len(petList))
})
}