58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"testing"
|
|
)
|
|
|
|
func TestCartTcpHelpers(t *testing.T) {
|
|
|
|
server, err := CartListen("localhost:51337")
|
|
if err != nil {
|
|
t.Errorf("Error listening: %v\n", err)
|
|
}
|
|
client, err := CartDial("localhost:51337")
|
|
if err != nil {
|
|
t.Errorf("Error dialing: %v\n", err)
|
|
}
|
|
var messageData string
|
|
server.ListenFor(1, func(id CartId, data []byte) error {
|
|
log.Printf("Received message: %s\n", string(data))
|
|
messageData = string(data)
|
|
return nil
|
|
})
|
|
server.HandleCall(666, func(id CartId, data []byte) (CartMessage, []byte, error) {
|
|
log.Printf("Received 666 call: %s\n", string(data))
|
|
return 3, []byte("Hello, client!"), fmt.Errorf("Det blev fel")
|
|
})
|
|
server.HandleCall(2, func(id CartId, data []byte) (CartMessage, []byte, error) {
|
|
log.Printf("Received 2 call: %s\n", string(data))
|
|
return 4, []byte("Hello, client!"), nil
|
|
})
|
|
// server.HandleCall(Ping, func(id CartId, data []byte) (CartMessage, []byte, error) {
|
|
// return Pong, nil, nil
|
|
// })
|
|
id := ToCartId("kalle")
|
|
client.SendPacket(1, id, []byte("Hello, world!"))
|
|
answer, err := client.Call(2, id, 4, []byte("Hello, server!"))
|
|
if err != nil {
|
|
t.Errorf("Error calling: %v\n", err)
|
|
}
|
|
s, err := client.Call(666, id, 3, []byte("Hello, server!"))
|
|
client.Close()
|
|
if err != nil {
|
|
t.Errorf("Error calling: %v\n", err)
|
|
}
|
|
if s.StatusCode != 500 {
|
|
t.Errorf("Expected 500, got %d\n", s.StatusCode)
|
|
}
|
|
|
|
if string(answer.Data) != "Hello, client!" {
|
|
t.Errorf("Expected answer 'Hello, client!', got %s\n", string(answer.Data))
|
|
}
|
|
if messageData != "Hello, world!" {
|
|
t.Errorf("Expected message 'Hello, world!', got %s\n", messageData)
|
|
}
|
|
}
|