github.com/sandwich-go/boost@v1.3.29/httputil/client_test.go (about)

     1  package httputil
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  )
     9  
    10  func TestClient(t *testing.T) {
    11  	Convey("client", t, func() {
    12  		const raw = `{ "foo": "bar" }`
    13  
    14  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  			_, _ = w.Write([]byte(raw))
    16  		}))
    17  		defer ts.Close()
    18  		res, err := Get(ts.URL)
    19  		So(err, ShouldBeNil)
    20  		So(res, ShouldNotBeNil)
    21  
    22  		res, err = Post(ts.URL, "", nil)
    23  		So(err, ShouldBeNil)
    24  		So(res, ShouldNotBeNil)
    25  
    26  		bs, err0 := Bytes(ts.URL)
    27  		So(err0, ShouldBeNil)
    28  		So(bs, ShouldNotBeNil)
    29  		So(string(bs), ShouldEqual, raw)
    30  
    31  		str, err1 := String(ts.URL)
    32  		So(err1, ShouldBeNil)
    33  		So(str, ShouldEqual, raw)
    34  
    35  		var m map[string]string
    36  		err2 := JSON(ts.URL, &m)
    37  		So(err2, ShouldBeNil)
    38  		So(len(m), ShouldEqual, 1)
    39  		So(m["foo"], ShouldEqual, "bar")
    40  	})
    41  }