package echo
import (
"bytes"
"io"
"testing"
)
type ioBuffer struct {
// I need a separate place to read from and write to
in bytes.Buffer
out bytes.Buffer
isClosed bool
}
func (b *ioBuffer) Read(p []byte) (n int, err error) {
if b.isClosed {
return 0, io.ErrClosedPipe
}
return b.in.Read(p)
}
func (b *ioBuffer) Write(p []byte) (n int, err error) {
if b.isClosed {
return 0, io.ErrClosedPipe
}
return b.out.Write(p)
}
func (b *ioBuffer) Close() error {
b.isClosed = true
return nil
}
func TestHandler(t *testing.T) {
connection := &ioBuffer{in: *bytes.NewBufferString("testing")}
want := "testing"
err := Handler(connection)
if err != nil {
t.Fatalf("Handler failed: %s", err.Error())
}
if connection.out.String() != want {
t.Errorf(`got %q, want %q`, connection.out.String(), want)
}
if !connection.isClosed {
t.Error("Did not close the connection")
}
}
func TestHandlerErr(t *testing.T) {
connection := &ioBuffer{in: *bytes.NewBufferString("testing")}
connection.Close()
err := Handler(connection)
if err != io.ErrClosedPipe {
t.Fatalf("Handler should fail with closed pipe. got: %v\n", err)
}
}
package daytime
import (
"bytes"
"io"
"testing"
"time"
)
type ioBuffer struct {
// I need a separate place to read from and write to
in bytes.Buffer
out bytes.Buffer
isClosed bool
}
func (b *ioBuffer) Read(p []byte) (n int, err error) {
if b.isClosed {
return 0, io.ErrClosedPipe
}
return b.in.Read(p)
}
func (b *ioBuffer) Write(p []byte) (n int, err error) {
if b.isClosed {
return 0, io.ErrClosedPipe
}
return b.out.Write(p)
}
func (b *ioBuffer) Close() error {
b.isClosed = true
return nil
}
func TestHandler(t *testing.T) {
const format = "Monday, January 2, 2006 15:04:05-MST\r\n"
var connection ioBuffer
err := Handler(&connection)
if err != nil {
t.Fatalf("Handler failed: %s", err.Error())
}
// Check that we write a correctly formatted date string to the outputt
_, err = time.Parse(format, connection.out.String())
if err != nil {
t.Errorf("Error: %s\n", err.Error())
}
// Check that we closed the output.
if !connection.isClosed {
t.Error("Did not close the connection")
}
}
func TestHandlerErr(t *testing.T) {
var connection ioBuffer
connection.Close()
err := Handler(&connection)
if err != io.ErrClosedPipe {
t.Fatalf("Handler should fail with closed pipe. got: %v\n", err)
}
}
func Test_formatResponse(t *testing.T) {
const format = "Monday, January 2, 2006 15:04:05-MST\r\n"
denver, err := time.LoadLocation("America/Denver")
if err != nil {
t.Fatalf("Can not find location info for America/Denver: %v", err)
}
type args struct {
t time.Time
}
tests := []struct {
name string
args args
want []byte
}{
{
// result should match the format string.
name: "format-pattern",
args: args{
t: time.Date(2006, 1, 2, 15, 4, 5, 0, denver),
},
want: []byte(format),
},
{
// we don't need a lot of tests but a couple ensures that we are
// formatting as expected.
name: "unix-epoch",
args: args{
t: time.UnixMilli(0).In(denver),
},
want: []byte("Wednesday, December 31, 1969 17:00:00-MST\r\n"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatResponse(tt.args.t); !bytes.Equal(got, tt.want) {
t.Errorf("formatResponse() = %v, want %v", got, tt.want)
}
})
}
}