test: simplify request body

This commit is contained in:
Tine 2024-02-06 23:42:14 +01:00
parent 93650d7d5a
commit 00ed214513
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw

View file

@ -59,6 +59,12 @@ func setup(t *testing.T) (func(), *openapi.Server) {
}, server }, server
} }
func toJson(t *testing.T, request interface{ MarshalJSON() ([]byte, error) }) *bytes.Buffer {
json, err := request.MarshalJSON()
assert.Nil(t, err)
return bytes.NewBuffer(json)
}
func TestHealth(t *testing.T) { func TestHealth(t *testing.T) {
teardown, srv := setup(t) teardown, srv := setup(t)
defer teardown() defer teardown()
@ -78,11 +84,8 @@ func TestCreatingGroup(t *testing.T) {
teardown, srv := setup(t) teardown, srv := setup(t)
defer teardown() defer teardown()
groupUpdate := openapi.GroupUpdate{Name: "test"} // TODO: Maybe we can create a "newRequest" method to wrap this 6 lines?
json, err := groupUpdate.MarshalJSON() req, err := http.NewRequest("POST", "/groups", toJson(t, &openapi.GroupUpdate{Name: "test"}))
assert.Nil(t, err)
req, err := http.NewRequest("POST", "/groups", bytes.NewBuffer(json))
assert.Nil(t, err) assert.Nil(t, err)
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")