debian-forge-composer/internal/test/request.go
Ondřej Budai ad5a135b56 internal/test: add small library for tests
Yeah, we have TestRoute. It has one issue though: It doesn't have support
for passing a custom context. One option is to extend the method with yet
argument but since it already has 9 (!!!), this seems like a huge mess.

Therefore, I decided to invent a new small library for writing API tests.
It uses structs heavily which means that adding features to it doesn't
mean changing 100 lines of code (like adding another arg to TestRoute does).

I hope that we can start using this library more in our tests as it was
designed to be very flexible and powerfule.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2022-03-08 12:07:00 +01:00

23 lines
624 B
Go

package test
// RequestBody is an abstract interface for defining request bodies for APICall
type RequestBody interface {
// Body returns the intended request body as a slice of bytes
Body() []byte
// ContentType returns value for Content-Type request header
ContentType() string
}
// JSONRequestBody is just a simple wrapper over plain string.
//
// Body is just the string converted to a slice of bytes and content type is set to application/json
type JSONRequestBody string
func (b JSONRequestBody) Body() []byte {
return []byte(b)
}
func (b JSONRequestBody) ContentType() string {
return "application/json"
}