Skip to content

Commit

Permalink
adds some tests on Request
Browse files Browse the repository at this point in the history
  • Loading branch information
rvflash committed Mar 3, 2019
1 parent 5bf1502 commit 7ead310
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tcp
1 change: 0 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (r *Request) WithCancel(ctx context.Context) *Request {

// NewRequest returns a new instance of request.
// A segment is mandatory as input. If empty, a SYN segment is used.
// If the body is missing, a no-op reader with closing is used.
func NewRequest(segment string, body io.Reader) *Request {
if segment == "" {
// by default, we use the SYN segment.
Expand Down
50 changes: 50 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package tcp_test

import (
"io"
"io/ioutil"
"strconv"
"strings"
"testing"

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

func TestNewRequest(t *testing.T) {
var (
are = is.New(t)
dt = []struct {
seg string
body io.Reader
segment string
resp []byte
}{
{segment: tcp.SYN},
{seg: tcp.ACK, segment: tcp.ACK},
{seg: tcp.SYN, segment: tcp.SYN},
{seg: tcp.FIN, segment: tcp.FIN},
{seg: "NOP", segment: "NOP"},
{seg: tcp.ACK, segment: tcp.ACK, body: strings.NewReader(msg), resp: []byte(msg)},
}
req *tcp.Request
b []byte
err error
)
for i, tt := range dt {
t.Run("#"+strconv.Itoa(i), func(t *testing.T) {
req = tcp.NewRequest(tt.seg, tt.body)
are.Equal(req.Segment, tt.segment)
if tt.body == nil {
are.True(req.Body == nil)
} else {
b, err = ioutil.ReadAll(req.Body)
are.NoErr(err)
are.Equal(b, tt.resp)
}
})
}
}

func TestRequest_Context(t *testing.T) {
is.New(t).True(tcp.NewRequest(tcp.SYN, nil).Context() != nil)
}

0 comments on commit 7ead310

Please sign in to comment.