4
0

errors_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package acme
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func mustJSON(t *testing.T, m map[string]any) string {
  9. t.Helper()
  10. b, err := json.Marshal(m)
  11. require.NoError(t, err)
  12. return string(b)
  13. }
  14. func TestError_WithAdditionalErrorDetail(t *testing.T) {
  15. internalJSON := mustJSON(t, map[string]any{
  16. "detail": "The server experienced an internal error",
  17. "type": "urn:ietf:params:acme:error:serverInternal",
  18. })
  19. malformedErr := NewError(ErrorMalformedType, "malformed error") // will result in Err == nil behavior
  20. malformedJSON := mustJSON(t, map[string]any{
  21. "detail": "The request message was malformed",
  22. "type": "urn:ietf:params:acme:error:malformed",
  23. })
  24. withDetailJSON := mustJSON(t, map[string]any{
  25. "detail": "Attestation statement cannot be verified: invalid property",
  26. "type": "urn:ietf:params:acme:error:badAttestationStatement",
  27. })
  28. tests := []struct {
  29. name string
  30. err *Error
  31. want string
  32. }{
  33. {"internal", NewDetailedError(ErrorServerInternalType, ""), internalJSON},
  34. {"nil err", malformedErr, malformedJSON},
  35. {"detailed", NewDetailedError(ErrorBadAttestationStatementType, "invalid property"), withDetailJSON},
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. b, err := json.Marshal(tt.err)
  40. require.NoError(t, err)
  41. // tests if the additional error detail is included in the JSON representation
  42. // of the ACME error. This is what is returned to ACME clients and being logged
  43. // by the CA.
  44. assert.JSONEq(t, tt.want, string(b))
  45. })
  46. }
  47. }