types_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package acme
  5. import (
  6. "errors"
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. func TestRateLimit(t *testing.T) {
  12. now := time.Date(2017, 04, 27, 10, 0, 0, 0, time.UTC)
  13. f := timeNow
  14. defer func() { timeNow = f }()
  15. timeNow = func() time.Time { return now }
  16. h120, hTime := http.Header{}, http.Header{}
  17. h120.Set("Retry-After", "120")
  18. hTime.Set("Retry-After", "Tue Apr 27 11:00:00 2017")
  19. err1 := &Error{
  20. ProblemType: "urn:ietf:params:acme:error:nolimit",
  21. Header: h120,
  22. }
  23. err2 := &Error{
  24. ProblemType: "urn:ietf:params:acme:error:rateLimited",
  25. Header: h120,
  26. }
  27. err3 := &Error{
  28. ProblemType: "urn:ietf:params:acme:error:rateLimited",
  29. Header: nil,
  30. }
  31. err4 := &Error{
  32. ProblemType: "urn:ietf:params:acme:error:rateLimited",
  33. Header: hTime,
  34. }
  35. tt := []struct {
  36. err error
  37. res time.Duration
  38. ok bool
  39. }{
  40. {nil, 0, false},
  41. {errors.New("dummy"), 0, false},
  42. {err1, 0, false},
  43. {err2, 2 * time.Minute, true},
  44. {err3, 0, true},
  45. {err4, time.Hour, true},
  46. }
  47. for i, test := range tt {
  48. res, ok := RateLimit(test.err)
  49. if ok != test.ok {
  50. t.Errorf("%d: RateLimit(%+v): ok = %v; want %v", i, test.err, ok, test.ok)
  51. continue
  52. }
  53. if res != test.res {
  54. t.Errorf("%d: RateLimit(%+v) = %v; want %v", i, test.err, res, test.res)
  55. }
  56. }
  57. }