-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_test.go
48 lines (43 loc) · 983 Bytes
/
session_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
46
47
48
package requests_test
import (
"context"
"fmt"
"github.com/golang-io/requests"
"io"
"net"
"net/http"
"net/url"
"os"
"testing"
)
func TestSession_Do(t *testing.T) {
sock := "unix:///tmp/requests.sock"
u, err := url.Parse(sock)
if err != nil {
t.Error(err)
}
t.Log(u.String(), u.Host, u.Path)
os.Remove("/tmp/requests.sock")
l, err := net.Listen("unix", "/tmp/requests.sock")
if err != nil {
t.Error(err)
return
}
s := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "url=%s, path=%s\n", r.URL.String(), r.URL.Path)
io.Copy(w, r.Body)
}),
}
defer s.Shutdown(context.Background())
go func() {
s.Serve(l)
}()
sess := requests.New(requests.URL(sock))
sess.DoRequest(context.Background(),
requests.URL("http://path?k=v"),
requests.Body("12345"), requests.MethodPost,
requests.Logf(func(ctx context.Context, stat *requests.Stat) {
_, _ = fmt.Printf("%s\n", stat)
}))
}