示例#1
0
func TestWebsockPath(t *testing.T) {
	tran := NewTransport()
	l, e := tran.NewListener("ws://127.0.0.1:3335/mysock", req.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Listener: %v", e)
		return
	}
	d, e := tran.NewDialer("ws://127.0.0.1:3335/boguspath", rep.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Dialer: %v", e)
		return
	}

	if e = l.Listen(); e != nil {
		t.Errorf("Listen failed")
		return
	}
	defer l.Close()

	p, e := d.Dial()
	if p != nil {
		defer p.Close()
	}
	if e == nil {
		t.Errorf("Dial passed, when should not have!")
		return
	}
	t.Logf("Got expected error %v", e)
}
示例#2
0
func TestWebsockMux(t *testing.T) {
	tran := NewTransport()
	l, e := tran.NewListener("ws://127.0.0.1:3336/mysock", req.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Listener: %v", e)
		return
	}
	muxi, e := l.GetOption(OptionWebSocketMux)
	if e != nil {
		t.Errorf("Failed get mux: %v", e)
	}
	mux := muxi.(*http.ServeMux)
	mux.HandleFunc("/bogus", bogusHandler)
	d, e := tran.NewDialer("ws://127.0.0.1:3336/bogus", rep.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Dialer: %v", e)
		return
	}

	if e = l.Listen(); e != nil {
		t.Errorf("Listen failed")
		return
	}
	defer l.Close()

	p, e := d.Dial()
	if p != nil {
		defer p.Close()
	}
	if e == nil {
		t.Errorf("Dial passed, when should not have!")
		return
	}
	t.Logf("Got expected error %v", e)

	// Now let's try to use http client.
	resp, err := http.Get("http://127.0.0.1:3336/bogus")

	if err != nil {
		t.Errorf("Get of boguspath failed: %v", err)
		return
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response code wrong: %d", resp.StatusCode)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("ReadAll Failed: %v", err)
		return
	}
	if string(body) != bogusstr {
		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
	}
	t.Logf("Got body: %s", string(body))
}
示例#3
0
func NewTranTest(tran mangos.Transport, addr string) *TranTest {
	tt := &TranTest{addr: addr, tran: tran}
	if strings.HasPrefix(tt.addr, "tls+tcp://") || strings.HasPrefix(tt.addr, "wss://") {
		tt.cliCfg, _ = GetTlsConfig(false)
		tt.srvCfg, _ = GetTlsConfig(true)
	}
	tt.protoRep = rep.NewProtocol()
	tt.protoReq = req.NewProtocol()
	return tt
}
示例#4
0
package tcp

import (
	"bytes"
	"testing"
	"time"

	"github.com/gdamore/mangos"
	"github.com/gdamore/mangos/protocol/rep"
	"github.com/gdamore/mangos/protocol/req"
)

var tran = NewTransport()
var protoRep = rep.NewProtocol()
var protoReq = req.NewProtocol()

func TestTCPListenAndAccept(t *testing.T) {
	addr := "tcp://127.0.0.1:3333"
	t.Logf("Establishing accepter")
	l, err := tran.NewListener(addr, protoRep)
	if err != nil {
		t.Errorf("NewListener failed: %v", err)
		return
	}
	defer l.Close()
	if err = l.Listen(); err != nil {
		t.Errorf("Listen failed: %v", err)
		return
	}
示例#5
0
// This test verifies that we can use stock http server instances with
// our own websocket handler.
func TestWebsockHandler(t *testing.T) {
	tran := NewTransport()
	l, e := tran.NewListener("ws://127.0.0.1:3337/mysock", req.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Listener: %v", e)
		return
	}
	hi, e := l.GetOption(OptionWebSocketHandler)
	if e != nil {
		t.Errorf("Failed get WebSocketHandler: %v", e)
	}
	handler := hi.(http.Handler)

	mux := http.NewServeMux()
	mux.HandleFunc("/bogus", bogusHandler)
	mux.Handle("/mysock", handler)

	// Note that we are *counting* on this to die gracefully when our
	// program exits. There appears to be no way to shutdown http
	// instances gracefully.
	go http.ListenAndServe("127.0.0.1:3337", mux)

	d, e := tran.NewDialer("ws://127.0.0.1:3337/bogus", rep.NewProtocol())
	if e != nil {
		t.Errorf("Failed new Dialer: %v", e)
		return
	}

	defer l.Close()

	p, e := d.Dial()
	if p != nil {
		defer p.Close()
	}
	if e == nil {
		t.Errorf("Dial passed, when should not have!")
		return
	}
	t.Logf("Got expected error %v", e)

	// Now let's try to use http client.
	resp, err := http.Get("http://127.0.0.1:3337/bogus")

	if err != nil {
		t.Errorf("Get of boguspath failed: %v", err)
		return
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response code wrong: %d", resp.StatusCode)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("ReadAll Failed: %v", err)
		return
	}
	if string(body) != bogusstr {
		t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
	}
	t.Logf("Got body: %s", string(body))
}