-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
45 lines (40 loc) · 1.14 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package uhttp_test
import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/dnesting/uhttp"
)
func ExampleClient_sSDP() {
// This example performs an SSDP M-SEARCH to the local Multicast SSDP address.
// It uses the uhttp.Client so as to receive multiple responses, but this same
// transport could be used with a stock http.Client is you're OK only getting
// the first response.
client := uhttp.Client{
Transport: &uhttp.Transport{
// SSDP requires upper-case header names
HeaderCanon: func(n string) string { return strings.ToUpper(n) },
Repeat: uhttp.RepeatAfter(50*time.Millisecond, 1),
},
}
// Build M-SEARCH request
req, _ := http.NewRequest("M-SEARCH", "", nil)
req.URL.Host = "239.255.255.250:1900"
req.URL.Path = "*"
req.Header.Add("MAN", `"ssdp:discover"`)
req.Header.Add("MX", "1")
req.Header.Add("ST", "upnp:rootdevice")
req.Header.Add("CPFN.UPNP.ORG", "Test")
err := client.Do(req, 3*time.Second, func(sender net.Addr, resp *http.Response) error {
fmt.Printf("From %s:\n", sender)
resp.Write(os.Stdout)
fmt.Println("---")
return nil
})
if err != nil {
fmt.Printf("error: %s\n", err)
}
}