Files
moby/internal/testutil/request/helpers_test.go
Sebastiaan van Stijn 3906199019 internal/testutil/request: add ReadJSONResponse utility
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-18 22:15:31 +02:00

78 lines
1.7 KiB
Go

package request_test
import (
"bytes"
"io"
"net/http"
"testing"
"github.com/moby/moby/v2/internal/testutil/request"
"gotest.tools/v3/assert"
)
func TestReadJSONResponse(t *testing.T) {
type someResponse struct {
Hello string `json:"Hello"`
}
tests := []struct {
name string
body string
contentType string
expected someResponse
expErr string
}{
{
name: "valid JSON",
body: `{"hello": "world"}`,
contentType: "application/json",
expected: someResponse{Hello: "world"},
},
{
name: "valid JSON, utf-8",
body: `{"hello": "world"}`,
contentType: "application/json; charset=utf-8",
expected: someResponse{Hello: "world"},
},
{
name: "malformed JSON",
body: `{"hello": "world"`,
contentType: "application/json",
expErr: "unexpected EOF",
},
{
name: "non-JSON",
body: `<html><head><title>Page not found</title></head></html>`,
contentType: "text/html",
expErr: "unexpected Content-Type: 'text/html'",
},
{
name: "nil response",
expErr: "nil *http.Response",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var v someResponse
var resp *http.Response
if tc.name != "nil response" {
resp = &http.Response{
Header: make(http.Header),
Body: io.NopCloser(bytes.NewBufferString(tc.body)),
}
resp.Header.Set("Content-Type", tc.contentType)
}
err := request.ReadJSONResponse(resp, &v)
if tc.expErr != "" {
assert.ErrorContains(t, err, tc.expErr)
} else {
assert.NilError(t, err)
assert.DeepEqual(t, tc.expected, v)
}
})
}
}