ooni-probe-cli/internal/netxlite/errorsx/errwrapper_test.go
Simone Basso 18b2eb37ff
fix(netxlite/errorsx): serialize directly to JSON (#491)
This simplifies serializing errors to `*string`. It did not
occur to me before. It seems quite a nice improvement.

Part of https://github.com/ooni/probe/issues/1591
2021-09-08 17:42:36 +02:00

43 lines
800 B
Go

package errorsx
import (
"encoding/json"
"errors"
"io"
"testing"
)
func TestErrWrapper(t *testing.T) {
t.Run("Error", func(t *testing.T) {
err := &ErrWrapper{Failure: FailureDNSNXDOMAINError}
if err.Error() != FailureDNSNXDOMAINError {
t.Fatal("invalid return value")
}
})
t.Run("Unwrap", func(t *testing.T) {
err := &ErrWrapper{
Failure: FailureEOFError,
WrappedErr: io.EOF,
}
if !errors.Is(err, io.EOF) {
t.Fatal("cannot unwrap error")
}
})
t.Run("MarshalJSON", func(t *testing.T) {
wrappedErr := &ErrWrapper{
Failure: FailureEOFError,
WrappedErr: io.EOF,
}
data, err := json.Marshal(wrappedErr)
if err != nil {
t.Fatal(err)
}
s := string(data)
if s != "\""+FailureEOFError+"\"" {
t.Fatal("invalid serialization", s)
}
})
}