2020-05-20 07:49:40 +00:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialServer(t *testing.T) {
|
|
|
|
// TODO replace the websocket connection with a mock server
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
//ctx := context.Background()
|
|
|
|
wsconn := &WSTestConn{
|
|
|
|
rwt: &RWTest{},
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mux := NewRouteMux()
|
|
|
|
t.Fatal(ListenAndServe(wsconn, mux))
|
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
var ErrNoImpl error = errors.New("not implemented")
|
2020-05-21 05:30:24 +00:00
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
// WSTestConn is a fake websocket connection
|
|
|
|
type WSTestConn struct {
|
|
|
|
closed bool
|
|
|
|
rwt *RWTest
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) NextReader() (messageType int, r io.Reader, err error) {
|
|
|
|
return 0, nil, ErrNoImpl
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) NextWriter(messageType int) (io.WriteCloser, error) {
|
|
|
|
return nil, ErrNoImpl
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) WriteControl(messageType int, data []byte, deadline time.Time) error {
|
|
|
|
if wst.closed {
|
|
|
|
return io.EOF
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
return nil
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) WriteMessage(messageType int, data []byte) error {
|
|
|
|
if wst.closed {
|
|
|
|
return io.EOF
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
return nil
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return ErrNoImpl
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) Close() error {
|
|
|
|
wst.closed = true
|
|
|
|
return nil
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
func (wst *WSTestConn) RemoteAddr() net.Addr {
|
|
|
|
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8443")
|
|
|
|
return addr
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
// RWTest is a fake buffer
|
|
|
|
type RWTest struct {
|
|
|
|
closed bool
|
|
|
|
tmpr []byte
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
func (rwt *RWTest) Read(dst []byte) (int, error) {
|
|
|
|
if rwt.closed {
|
|
|
|
return 0, io.EOF
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
id := Addr{
|
|
|
|
scheme: "http",
|
|
|
|
addr: "192.168.1.108",
|
|
|
|
port: 6732,
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
tun := Addr{
|
|
|
|
scheme: "http",
|
|
|
|
termination: TLS,
|
|
|
|
addr: "abc.example.com",
|
|
|
|
port: 443,
|
2020-05-21 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
if 0 == len(rwt.tmpr) {
|
|
|
|
b := []byte("Hello, World!")
|
|
|
|
h, _, _ := Encode(b, id, tun)
|
|
|
|
rwt.tmpr = append(h, b...)
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
n := copy(dst, rwt.tmpr)
|
|
|
|
rwt.tmpr = rwt.tmpr[n:]
|
2020-05-20 07:49:40 +00:00
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
return n, nil
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
func (rwt *RWTest) Write(int, []byte) error {
|
|
|
|
if rwt.closed {
|
|
|
|
return io.EOF
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
return nil
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 10:29:05 +00:00
|
|
|
func (rwt *RWTest) Close() error {
|
|
|
|
rwt.closed = true
|
|
|
|
return nil
|
2020-05-20 07:49:40 +00:00
|
|
|
}
|