Files
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.

Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:47:46 -07:00

104 lines
2.0 KiB
Go

package http
import (
"bufio"
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testStruct struct {
Metadata *testStruct `json:"metadata,omitempty"`
Spec *testStruct `json:"spec,omitempty"`
Source *testStruct `json:"source,omitempty"`
Status *testStruct `json:"status,omitempty"`
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
Message string `json:"message,omitempty"`
}
var testVal = testStruct{
Metadata: &testStruct{Name: "test"},
Spec: &testStruct{
Source: &testStruct{
Path: "test_path",
},
},
Status: &testStruct{
Message: "Failed",
},
}
func TestMarshalerIncludeFields(t *testing.T) {
m := messageMarshaler{fields: map[string]any{
"metadata.name": true,
"spec.source": true,
}, exclude: false}
out, err := m.Marshal(testVal)
require.NoError(t, err)
assert.JSONEq(t, `{"metadata":{"name":"test"},"spec":{"source":{"path":"test_path"}}}`, string(out))
}
func TestMarshalerExcludeFields(t *testing.T) {
m := messageMarshaler{fields: map[string]any{
"metadata.name": true,
}, exclude: true}
out, err := m.Marshal(testVal)
require.NoError(t, err)
assert.JSONEq(t, `{"metadata":{},"spec":{"source":{"path":"test_path"}},"status":{"message":"Failed"}}`, string(out))
}
func TestMarshalerSSE(t *testing.T) {
m := messageMarshaler{isSSE: true}
out, err := m.Marshal(testVal)
require.NoError(t, err)
assert.Equal(t, `data: {"metadata":{"name":"test"},"spec":{"source":{"path":"test_path"}},"status":{"message":"Failed"}}
`, string(out))
}
var flushed bool
type flusher struct {
w *bufio.Writer
}
func (f *flusher) Flush() {
err := f.w.Flush()
if err != nil {
panic(err)
}
flushed = true
}
func TestFlushSuccess(t *testing.T) {
flushed = false
var buf bytes.Buffer
_, _ = fmt.Fprintf(&buf, "Size: %d MB.", 85)
f := flusher{w: bufio.NewWriter(&buf)}
flush(&f)
assert.True(t, flushed)
}
func TestFlushFailed(t *testing.T) {
flushed = false
f := flusher{}
flush(&f)
assert.False(t, flushed)
}