-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathweb_test.go
38 lines (34 loc) · 877 Bytes
/
web_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
package gopwn
import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHTTPGet(t *testing.T) {
t.Run("default setup", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "helloworld")
}))
defer ts.Close()
content, err := HTTPGet(ts.URL)
assert.NoError(t, err)
assert.Equal(t, []byte("helloworld"), content)
})
t.Run("timeout", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
}))
defer ts.Close()
_, err := HTTPGet(ts.URL, func(o *HTTPClientOptions) {
o.Timeout = 5 * time.Millisecond
})
assert.Error(t, err)
netErr, ok := err.(net.Error)
assert.True(t, ok)
assert.True(t, netErr.Timeout())
})
}