Skip to content

Commit

Permalink
Response: test CommonWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Jul 16, 2024
1 parent a62ae30 commit d83a434
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Flusher interface {
// CommonWriter is a component meant to be used with composition
// to avoid having to implement the base behavior of the common interfaces
// a chained writer has to implement (`PreWrite()`, `Write()`, `Close()`, `Flush()`)
type CommonWriter struct { // TODO test CommonWriter
type CommonWriter struct {
Component
wr io.Writer
}
Expand Down
102 changes: 101 additions & 1 deletion response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,34 @@ func (r *hijackableRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {

type testChainedWriter struct {
*httptest.ResponseRecorder
flushErr error
prewritten []byte
closed bool
flushed bool
}

func (r *testChainedWriter) PreWrite(b []byte) {
r.prewritten = b
}

func (r *testChainedWriter) Flush() error {
r.flushed = true
return r.flushErr
}

func (r *testChainedWriter) Close() error {
r.closed = true
return nil
}

type testChainedWriterHTTPFlusher struct {
*testChainedWriter
}

func (r *testChainedWriterHTTPFlusher) Flush() {
r.flushed = true
}

func TestResponse(t *testing.T) {
t.Run("NewResponse", func(t *testing.T) {
resp, _ := newTestReponse()
Expand Down Expand Up @@ -530,5 +545,90 @@ func TestResponse(t *testing.T) {
})
})

// TODO flush test
t.Run("Flush", func(t *testing.T) {
resp, recorder := newTestReponse()
newWriter := &testChainedWriter{
ResponseRecorder: recorder,
}
resp.SetWriter(newWriter)
resp.Flush()
assert.True(t, newWriter.flushed)
assert.True(t, resp.wroteHeader)
assert.Equal(t, []byte{}, newWriter.prewritten) // PreWrite called

// PreWrite called once
newWriter.prewritten = nil
resp.Flush()
assert.Nil(t, newWriter.prewritten)

t.Run("error", func(t *testing.T) {
resp, recorder := newTestReponse()
logBuffer := &bytes.Buffer{}
resp.server.Logger = slog.New(slog.NewHandler(false, logBuffer))
newWriter := &testChainedWriter{
ResponseRecorder: recorder,
flushErr: fmt.Errorf("test error"),
}
resp.SetWriter(newWriter)
resp.Flush()
assert.NotEmpty(t, logBuffer.String())
})

t.Run("http.Flusher", func(t *testing.T) {
resp, recorder := newTestReponse()
logBuffer := &bytes.Buffer{}
resp.server.Logger = slog.New(slog.NewHandler(false, logBuffer))
newWriter := &testChainedWriterHTTPFlusher{
testChainedWriter: &testChainedWriter{
ResponseRecorder: recorder,
},
}
resp.SetWriter(newWriter)
resp.Flush()
assert.True(t, newWriter.flushed)
assert.Empty(t, logBuffer.String())
})
})
}

type httpFlusher struct {
io.Writer
flushed bool
}

func (f *httpFlusher) Flush() {
f.flushed = true
}

func TestCommonWriter(t *testing.T) {
recorder := httptest.NewRecorder()
chainedWriter := &testChainedWriter{
ResponseRecorder: recorder,
}
wr := NewCommonWriter(chainedWriter)
assert.Equal(t, chainedWriter, wr.wr)

wr.PreWrite([]byte("hello"))
assert.Equal(t, []byte("hello"), chainedWriter.prewritten)

_, err := wr.Write([]byte("hello"))
assert.NoError(t, err)
assert.NoError(t, wr.Flush())
assert.True(t, chainedWriter.flushed)
assert.NoError(t, wr.Close())
assert.True(t, chainedWriter.closed)

res := chainedWriter.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, []byte("hello"), body)

t.Run("http.flusher_and_nocloser", func(t *testing.T) {
chainedWriter := &httpFlusher{}
wr := NewCommonWriter(chainedWriter)
assert.NoError(t, wr.Flush())
assert.True(t, chainedWriter.flushed)
assert.NoError(t, wr.Close())
})
}

0 comments on commit d83a434

Please sign in to comment.