2
0

server_test.go 873 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package manager
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "net/http"
  8. "testing"
  9. "time"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestHTTPServer(t *testing.T) {
  13. Convey("HTTP server should work", t, func() {
  14. s := makeHTTPServer(false)
  15. So(s, ShouldNotBeNil)
  16. port := rand.Intn(10000) + 20000
  17. go func() {
  18. s.Run(fmt.Sprintf("127.0.0.1:%d", port))
  19. }()
  20. time.Sleep(50 * time.Microsecond)
  21. resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/ping", port))
  22. So(err, ShouldBeNil)
  23. So(resp.StatusCode, ShouldEqual, http.StatusOK)
  24. So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json; charset=utf-8")
  25. defer resp.Body.Close()
  26. body, err := ioutil.ReadAll(resp.Body)
  27. So(err, ShouldBeNil)
  28. var p map[string]string
  29. err = json.Unmarshal(body, &p)
  30. So(err, ShouldBeNil)
  31. So(p["msg"], ShouldEqual, "pong")
  32. })
  33. }