mirror of https://github.com/libp2p/go-libp2p.git
Browse Source
Also, use the now standardized transport tests (and fix bugs around closing the listener).pull/1422/head
Steven Allen
7 years ago
5 changed files with 182 additions and 135 deletions
@ -1,42 +0,0 @@ |
|||
package websocket |
|||
|
|||
import ( |
|||
"context" |
|||
|
|||
ws "github.com/gorilla/websocket" |
|||
tpt "github.com/libp2p/go-libp2p-transport" |
|||
ma "github.com/multiformats/go-multiaddr" |
|||
manet "github.com/multiformats/go-multiaddr-net" |
|||
) |
|||
|
|||
type dialer struct{} |
|||
|
|||
func (d *dialer) Dial(raddr ma.Multiaddr) (tpt.Conn, error) { |
|||
return d.DialContext(context.Background(), raddr) |
|||
} |
|||
|
|||
func (d *dialer) DialContext(ctx context.Context, raddr ma.Multiaddr) (tpt.Conn, error) { |
|||
wsurl, err := parseMultiaddr(raddr) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
wscon, _, err := ws.DefaultDialer.Dial(wsurl, nil) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
mnc, err := manet.WrapNetConn(NewConn(wscon, nil)) |
|||
if err != nil { |
|||
wscon.Close() |
|||
return nil, err |
|||
} |
|||
|
|||
return &wsConn{ |
|||
Conn: mnc, |
|||
}, nil |
|||
} |
|||
|
|||
func (d *dialer) Matches(a ma.Multiaddr) bool { |
|||
return WsFmt.Matches(a) |
|||
} |
@ -1,31 +0,0 @@ |
|||
package websocket |
|||
|
|||
import ( |
|||
"testing" |
|||
|
|||
ma "github.com/multiformats/go-multiaddr" |
|||
) |
|||
|
|||
func TestDialerMatches(t *testing.T) { |
|||
addrWs, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws") |
|||
if err != nil { |
|||
t.Fatal(err) |
|||
} |
|||
|
|||
addrTcp, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555") |
|||
if err != nil { |
|||
t.Fatal(err) |
|||
} |
|||
|
|||
d := &dialer{} |
|||
matchTrue := d.Matches(addrWs) |
|||
matchFalse := d.Matches(addrTcp) |
|||
|
|||
if !matchTrue { |
|||
t.Fatal("expected to match websocket maddr, but did not") |
|||
} |
|||
|
|||
if matchFalse { |
|||
t.Fatal("expected to not match tcp maddr, but did") |
|||
} |
|||
} |
Loading…
Reference in new issue