52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package tests
|
|
|
|
import (
|
|
"bufio"
|
|
"kerma/helpers"
|
|
"kerma/utils"
|
|
"net"
|
|
"testing"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
)
|
|
|
|
func TestHandler(t *testing.T) {
|
|
host := "127.0.0.1:1337"
|
|
net.Listen("tcp", host)
|
|
conn, _ := net.Dial("tcp", host)
|
|
scanner := bufio.NewScanner(conn)
|
|
|
|
var config helpers.Config
|
|
config.Construct()
|
|
|
|
Convey("Given a new handler", t, func() {
|
|
var handler utils.TCPHandler
|
|
handler.Construct(conn)
|
|
|
|
Convey("When Construct is called, a new object is returned", func() {
|
|
So(handler.Conn, ShouldResemble, conn)
|
|
So(handler.Config, ShouldResemble, config)
|
|
})
|
|
|
|
Convey("When Output is called, a JSON string should be returned on the connection", func() {
|
|
jsonString := `{"foo": "bar"}`
|
|
handler.Output([]byte(jsonString))
|
|
resp := string(scanner.Bytes())
|
|
|
|
So(resp, ShouldEqual, resp)
|
|
})
|
|
|
|
Convey("When Input is called, a JSON string and no error should be obtained from the connection", func() {
|
|
jsonString := `{"foo": "bar"}`
|
|
resp, err := handler.Input([]byte(jsonString))
|
|
|
|
So(resp, ShouldEqual, resp)
|
|
So(err, ShouldBeNil)
|
|
})
|
|
|
|
Convey("When GetRemotePeer is called, the local connection should be returned", func() {
|
|
So(handler.GetRemotePeer(), ShouldEqual, host)
|
|
})
|
|
})
|
|
}
|