Skip to content

Commit

Permalink
adds tests on response's writer and recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
rvflash committed Mar 2, 2019
1 parent fc04a9f commit ca30ea6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
9 changes: 1 addition & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ type conn struct {
}

func (c *conn) bySegment(segment string, body io.Reader) {
w := c.newResponseWriter()
w := newWriter(c.rwc)
req := c.newRequest(segment, body)
c.srv.ServeTCP(w, req)
}

func (c *conn) newResponseWriter() *responseWriter {
return &responseWriter{
ResponseWriter: c.rwc,
size: noWritten,
}
}

func (c *conn) newRequest(segment string, body io.Reader) *Request {
req := NewRequest(segment, body)
req.RemoteAddr = c.addr
Expand Down
7 changes: 7 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ type ResponseWriter interface {
io.WriteCloser
}

func newWriter(wc io.WriteCloser) *responseWriter {
return &responseWriter{
ResponseWriter: wc,
size: noWritten,
}
}

type responseWriter struct {
ResponseWriter io.WriteCloser
size int
Expand Down
61 changes: 61 additions & 0 deletions response_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tcp

import (
"testing"

"github.com/matryer/is"
)

func TestResponseWriter_Write(t *testing.T) {
var (
are = is.New(t)
dt = []struct {
msg string
len, size int
}{
{msg: "hi", len: 2, size: 2},
{msg: " ", len: 1, size: 3},
{msg: "world!", len: 6, size: 9},
{msg: "\n", len: 1, size: 10},
}
w = newWriter(NewRecorder())
n int
err error
)
for _, tt := range dt {
t.Run(tt.msg, func(t *testing.T) {
n, err = w.Write([]byte(tt.msg))
are.NoErr(err)
are.Equal(n, tt.len) // len mismatch
are.Equal(w.Size(), tt.size) // size mismatch
})
}
are.NoErr(w.Close())
}

func TestResponseWriter_WriteString(t *testing.T) {
var (
are = is.New(t)
dt = []struct {
msg string
len, size int
}{
{msg: "hi", len: 2, size: 2},
{msg: " ", len: 1, size: 3},
{msg: "world!", len: 6, size: 9},
{msg: "\n", len: 1, size: 10},
}
w = newWriter(NewRecorder())
n int
err error
)
for _, tt := range dt {
t.Run(tt.msg, func(t *testing.T) {
n, err = w.WriteString(tt.msg)
are.NoErr(err)
are.Equal(n, tt.len) // len mismatch
are.Equal(w.Size(), tt.size) // size mismatch
})
}
are.NoErr(w.Close())
}
46 changes: 46 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tcp_test

import (
"testing"

"github.com/matryer/is"
"github.com/rvflash/tcp"
)

const (
msg = "hello world\n"
msgSize = 12
)

func TestResponseRecorder_Close(t *testing.T) {
is.New(t).True(tcp.NewRecorder().Close() == nil)
}

func TestResponseRecorder_Write(t *testing.T) {
w := tcp.NewRecorder()
n, err := w.Write([]byte(msg))
are := is.New(t)
are.NoErr(err)
are.Equal(n, msgSize)
are.Equal(w.Size(), msgSize)
}

func TestResponseRecorder_WriteString(t *testing.T) {
w := tcp.NewRecorder()
n, err := w.WriteString(msg)
are := is.New(t)
are.NoErr(err)
are.Equal(n, msgSize)
are.Equal(w.Size(), msgSize)
}

func TestResponseRecorder_Size(t *testing.T) {
are := is.New(t)
// no body
w := &tcp.ResponseRecorder{}
are.Equal(w.Size(), -1)
// with a valid body
w = tcp.NewRecorder()
are.Equal(w.Size(), 0)

}

0 comments on commit ca30ea6

Please sign in to comment.