httprequest_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package splunk
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "go.opentelemetry.io/collector/consumer/consumererror"
  12. "go.opentelemetry.io/collector/exporter/exporterhelper"
  13. )
  14. func TestConsumeMetrics(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. httpResponseCode int
  18. retryAfter int
  19. wantErr bool
  20. wantPermanentErr bool
  21. wantThrottleErr bool
  22. }{
  23. {
  24. name: "response_forbidden",
  25. httpResponseCode: http.StatusForbidden,
  26. wantErr: true,
  27. },
  28. {
  29. name: "response_bad_request",
  30. httpResponseCode: http.StatusBadRequest,
  31. wantPermanentErr: true,
  32. },
  33. {
  34. name: "response_throttle",
  35. httpResponseCode: http.StatusTooManyRequests,
  36. wantThrottleErr: true,
  37. },
  38. {
  39. name: "response_throttle_with_header",
  40. retryAfter: 123,
  41. httpResponseCode: http.StatusServiceUnavailable,
  42. wantThrottleErr: true,
  43. },
  44. {
  45. name: "large_batch",
  46. httpResponseCode: http.StatusAccepted,
  47. },
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. resp := &http.Response{
  52. StatusCode: tt.httpResponseCode,
  53. }
  54. if tt.retryAfter != 0 {
  55. resp.Header = map[string][]string{
  56. HeaderRetryAfter: {strconv.Itoa(tt.retryAfter)},
  57. }
  58. }
  59. err := HandleHTTPCode(resp)
  60. if tt.wantErr {
  61. assert.Error(t, err)
  62. return
  63. }
  64. if tt.wantPermanentErr {
  65. assert.Error(t, err)
  66. assert.True(t, consumererror.IsPermanent(err))
  67. return
  68. }
  69. if tt.wantThrottleErr {
  70. expected := fmt.Errorf("HTTP %d %q", tt.httpResponseCode, http.StatusText(tt.httpResponseCode))
  71. expected = exporterhelper.NewThrottleRetry(expected, time.Duration(tt.retryAfter)*time.Second)
  72. assert.EqualValues(t, expected, err)
  73. return
  74. }
  75. assert.NoError(t, err)
  76. })
  77. }
  78. }