github.com/btccom/go-micro/v2@v2.9.3/api/server/http/http_test.go (about) 1 package http 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "testing" 8 ) 9 10 func TestHTTPServer(t *testing.T) { 11 testResponse := "hello world" 12 13 s := NewServer("localhost:0") 14 15 s.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 fmt.Fprint(w, testResponse) 17 })) 18 19 if err := s.Start(); err != nil { 20 t.Fatal(err) 21 } 22 23 rsp, err := http.Get(fmt.Sprintf("http://%s/", s.Address())) 24 if err != nil { 25 t.Fatal(err) 26 } 27 defer rsp.Body.Close() 28 29 b, err := ioutil.ReadAll(rsp.Body) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 if string(b) != testResponse { 35 t.Fatalf("Unexpected response, got %s, expected %s", string(b), testResponse) 36 } 37 38 if err := s.Stop(); err != nil { 39 t.Fatal(err) 40 } 41 }