Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

122 linhas
2.3 KiB

  1. package aws_test
  2. import (
  3. "fmt"
  4. "github.com/goamz/goamz/aws"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. // Retrieve the response from handler using aws.RetryingClient
  13. func serveAndGet(handler http.HandlerFunc) (body string, err error) {
  14. ts := httptest.NewServer(handler)
  15. defer ts.Close()
  16. resp, err := aws.RetryingClient.Get(ts.URL)
  17. if err != nil {
  18. return
  19. }
  20. if resp.StatusCode != 200 {
  21. return "", fmt.Errorf("Bad status code: %d", resp.StatusCode)
  22. }
  23. greeting, err := ioutil.ReadAll(resp.Body)
  24. resp.Body.Close()
  25. if err != nil {
  26. return
  27. }
  28. return strings.TrimSpace(string(greeting)), nil
  29. }
  30. func TestClient_expected(t *testing.T) {
  31. body := "foo bar"
  32. resp, err := serveAndGet(func(w http.ResponseWriter, r *http.Request) {
  33. fmt.Fprintln(w, body)
  34. })
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if resp != body {
  39. t.Fatal("Body not as expected.")
  40. }
  41. }
  42. func TestClient_delay(t *testing.T) {
  43. body := "baz"
  44. wait := 4
  45. resp, err := serveAndGet(func(w http.ResponseWriter, r *http.Request) {
  46. if wait < 0 {
  47. // If we dipped to zero delay and still failed.
  48. t.Fatal("Never succeeded.")
  49. }
  50. wait -= 1
  51. time.Sleep(time.Second * time.Duration(wait))
  52. fmt.Fprintln(w, body)
  53. })
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if resp != body {
  58. t.Fatal("Body not as expected.", resp)
  59. }
  60. }
  61. func TestClient_no4xxRetry(t *testing.T) {
  62. tries := 0
  63. // Fail once before succeeding.
  64. _, err := serveAndGet(func(w http.ResponseWriter, r *http.Request) {
  65. tries += 1
  66. http.Error(w, "error", 404)
  67. })
  68. if err == nil {
  69. t.Fatal("should have error")
  70. }
  71. if tries != 1 {
  72. t.Fatalf("should only try once: %d", tries)
  73. }
  74. }
  75. func TestClient_retries(t *testing.T) {
  76. body := "biz"
  77. failed := false
  78. // Fail once before succeeding.
  79. resp, err := serveAndGet(func(w http.ResponseWriter, r *http.Request) {
  80. if !failed {
  81. http.Error(w, "error", 500)
  82. failed = true
  83. } else {
  84. fmt.Fprintln(w, body)
  85. }
  86. })
  87. if failed != true {
  88. t.Error("We didn't retry!")
  89. }
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if resp != body {
  94. t.Fatal("Body not as expected.")
  95. }
  96. }
  97. func TestClient_fails(t *testing.T) {
  98. tries := 0
  99. // Fail 3 times and return the last error.
  100. _, err := serveAndGet(func(w http.ResponseWriter, r *http.Request) {
  101. tries += 1
  102. http.Error(w, "error", 500)
  103. })
  104. if err == nil {
  105. t.Fatal(err)
  106. }
  107. if tries != 3 {
  108. t.Fatal("Didn't retry enough")
  109. }
  110. }