Skip to content

Commit

Permalink
Add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Aug 18, 2021
1 parent ffdb2c3 commit d6a96c9
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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())
})
}

0 comments on commit d6a96c9

Please sign in to comment.