main_test.go 1.6 KB

1234567891011121314151617181920212223242526272829
  1. package main
  2. import "testing"
  3. func TestHistoricalIngressRejectReason(t *testing.T) {
  4. tests := []struct {
  5. name string
  6. item candidate
  7. reason string
  8. match bool
  9. }{
  10. {name: "standard invalid key", item: candidate{body: `{"code":"INVALID_API_KEY","message":"Invalid API key"}`}, reason: "invalid_key", match: true},
  11. {name: "google missing key", item: candidate{body: `{"error":{"code":401,"message":"API key is required","status":"UNAUTHENTICATED"}}`}, reason: "missing_key", match: true},
  12. {name: "google group deleted", item: candidate{body: `{"error":{"code":403,"message":"API Key 所属分组已删除","status":"PERMISSION_DENIED"}}`}, reason: "group_deleted", match: true},
  13. {name: "ip acl", item: candidate{body: `{"code":"ACCESS_DENIED","message":"Access denied. Your IP is 192.0.2.1"}`}, reason: "ip_acl_denied", match: true},
  14. {name: "user not found remains", item: candidate{body: `{"code":"USER_NOT_FOUND","message":"User associated with API key not found"}`}, match: false},
  15. {name: "quota remains", item: candidate{body: `{"code":"API_KEY_QUOTA_EXHAUSTED","message":"quota"}`}, match: false},
  16. {name: "database failure remains", item: candidate{statusCode: 500, message: "Failed to validate API key", body: `{"code":"INTERNAL_ERROR","message":"Failed to validate API key"}`}, match: false},
  17. }
  18. for _, tt := range tests {
  19. t.Run(tt.name, func(t *testing.T) {
  20. reason, ok := historicalIngressRejectReason(tt.item)
  21. if ok != tt.match || reason != tt.reason {
  22. t.Fatalf("got (%q, %v), want (%q, %v)", reason, ok, tt.reason, tt.match)
  23. }
  24. })
  25. }
  26. }