diff --git a/logger_test.go b/logger_test.go new file mode 100644 index 0000000..4ae52b7 --- /dev/null +++ b/logger_test.go @@ -0,0 +1 @@ +package tcp diff --git a/request.go b/request.go index 6c80709..4d216a7 100644 --- a/request.go +++ b/request.go @@ -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. diff --git a/request_test.go b/request_test.go index a6b3318..3248e49 100644 --- a/request_test.go +++ b/request_test.go @@ -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) +}