transporter_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. /*
  3. import (
  4. "crypto/tls"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. func TestTransporterTimeout(t *testing.T) {
  12. http.HandleFunc("/timeout", func(w http.ResponseWriter, r *http.Request) {
  13. fmt.Fprintf(w, "timeout")
  14. w.(http.Flusher).Flush() // send headers and some body
  15. time.Sleep(time.Second * 100)
  16. })
  17. go http.ListenAndServe(":8080", nil)
  18. conf := tls.Config{}
  19. ts := newTransporter("http", conf, nil)
  20. ts.Get("http://google.com")
  21. _, _, err := ts.Get("http://google.com:9999")
  22. if err == nil {
  23. t.Fatal("timeout error")
  24. }
  25. res, req, err := ts.Get("http://localhost:8080/timeout")
  26. if err != nil {
  27. t.Fatal("should not timeout")
  28. }
  29. ts.CancelWhenTimeout(req)
  30. body, err := ioutil.ReadAll(res.Body)
  31. if err == nil {
  32. fmt.Println(string(body))
  33. t.Fatal("expected an error reading the body")
  34. }
  35. _, _, err = ts.Post("http://google.com:9999", nil)
  36. if err == nil {
  37. t.Fatal("timeout error")
  38. }
  39. _, _, err = ts.Get("http://www.google.com")
  40. if err != nil {
  41. t.Fatal("get error: ", err.Error())
  42. }
  43. _, _, err = ts.Post("http://www.google.com", nil)
  44. if err != nil {
  45. t.Fatal("post error")
  46. }
  47. }
  48. */