You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 lines
2.5 KiB

  1. package metrics
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestClassifyError_RateLimitEmptyMsg(t *testing.T) {
  7. errorType, errorCode := ClassifyError(429, "")
  8. assert.Equal(t, "rate_limit", errorType)
  9. assert.Equal(t, "429", errorCode)
  10. }
  11. func TestClassifyError_UpstreamHTTP502(t *testing.T) {
  12. errorType, errorCode := ClassifyError(502, "")
  13. assert.Equal(t, "upstream", errorType)
  14. assert.Equal(t, "502", errorCode)
  15. }
  16. func TestClassifyError_UpstreamHTTP500(t *testing.T) {
  17. errorType, errorCode := ClassifyError(500, "")
  18. assert.Equal(t, "upstream", errorType)
  19. assert.Equal(t, "500", errorCode)
  20. }
  21. func TestClassifyError_Timeout(t *testing.T) {
  22. errorType, errorCode := ClassifyError(0, "context deadline exceeded")
  23. assert.Equal(t, "timeout", errorType)
  24. assert.Equal(t, "context_deadline_exceeded", errorCode)
  25. }
  26. func TestClassifyError_TimeoutCanceled(t *testing.T) {
  27. errorType, errorCode := ClassifyError(0, "context canceled")
  28. assert.Equal(t, "timeout", errorType)
  29. assert.Equal(t, "context_canceled", errorCode)
  30. }
  31. func TestClassifyError_AuthInvalidToken(t *testing.T) {
  32. errorType, errorCode := ClassifyError(401, "invalid api key")
  33. assert.Equal(t, "auth", errorType)
  34. assert.Equal(t, "401", errorCode)
  35. }
  36. func TestClassifyError_AuthForbidden(t *testing.T) {
  37. errorType, errorCode := ClassifyError(403, "")
  38. assert.Equal(t, "auth", errorType)
  39. assert.Equal(t, "403", errorCode)
  40. }
  41. func TestClassifyError_RateLimit(t *testing.T) {
  42. errorType, errorCode := ClassifyError(429, "rate limit exceeded")
  43. assert.Equal(t, "rate_limit", errorType)
  44. assert.Equal(t, "429", errorCode)
  45. }
  46. func TestClassifyError_BillingInsufficientQuota(t *testing.T) {
  47. errorType, errorCode := ClassifyError(402, "insufficient quota")
  48. assert.Equal(t, "billing", errorType)
  49. assert.Equal(t, "insufficient_quota", errorCode)
  50. }
  51. func TestClassifyError_BillingQuotaExceeded(t *testing.T) {
  52. errorType, errorCode := ClassifyError(403, "quota exceeded")
  53. assert.Equal(t, "billing", errorType)
  54. assert.Equal(t, "quota_exceeded", errorCode)
  55. }
  56. func TestClassifyError_Other(t *testing.T) {
  57. errorType, errorCode := ClassifyError(0, "something unexpected")
  58. assert.Equal(t, "other", errorType)
  59. assert.Equal(t, "unknown", errorCode)
  60. }
  61. func TestClassifyError_BadRequest(t *testing.T) {
  62. errorType, errorCode := ClassifyError(400, "")
  63. assert.Equal(t, "other", errorType)
  64. assert.Equal(t, "400", errorCode)
  65. }
  66. func TestClassifyError_EmptyBoth(t *testing.T) {
  67. errorType, errorCode := ClassifyError(0, "")
  68. assert.Equal(t, "other", errorType)
  69. assert.Equal(t, "unknown", errorCode)
  70. }