Skip to content

Commit 0606295

Browse files
committed
refactor(libp2phttp): don't require specific port for the HTTP host example (#3047)
1 parent ae0d78a commit 0606295

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

p2p/http/example_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net"
88
"net/http"
9+
"regexp"
910
"strings"
1011

1112
"github.com/libp2p/go-libp2p"
@@ -125,18 +126,24 @@ func ExampleHost_overLibp2pStreams() {
125126
// Output: Hello HTTP
126127
}
127128

129+
var tcpPortRE = regexp.MustCompile(`/tcp/(\d+)`)
130+
128131
func ExampleHost_Serve() {
129132
server := libp2phttp.Host{
130133
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
131-
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
134+
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
132135
}
133136

134137
go server.Serve()
135138
defer server.Close()
136139

137-
fmt.Println(server.Addrs())
140+
for _, a := range server.Addrs() {
141+
s := a.String()
142+
addrWithoutSpecificPort := tcpPortRE.ReplaceAllString(s, "/tcp/<runtime-port>")
143+
fmt.Println(addrWithoutSpecificPort)
144+
}
138145

139-
// Output: [/ip4/127.0.0.1/tcp/50221/http]
146+
// Output: /ip4/127.0.0.1/tcp/<runtime-port>/http
140147
}
141148

142149
func ExampleHost_SetHTTPHandler() {

0 commit comments

Comments
 (0)