41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package apierrors
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type failedExport struct{ err error }
|
|
|
|
func (w failedExport) Write([]byte) (int, error) { return 0, w.err }
|
|
func TestCatalogExport(t *testing.T) {
|
|
catalog := Catalog{DefaultStatus: 200, Definitions: map[string]Definition{"INVALID": {Code: int64(9007199254740993), Message: "invalid"}}}
|
|
var out bytes.Buffer
|
|
if err := ExportCatalog(&out, catalog); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(out.String(), "9007199254740993") {
|
|
t.Fatal(out.String())
|
|
}
|
|
var decoded Catalog
|
|
if err := DecodeCatalogJSON(out.Bytes(), &decoded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
registry, err := NewRegistry(decoded)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if registry.Resolve(ByCode("INVALID")).Status != 200 {
|
|
t.Fatal("status lost")
|
|
}
|
|
cause := errors.New("write failed")
|
|
if err = ExportCatalog(failedExport{cause}, catalog); !errors.Is(err, cause) {
|
|
t.Fatal("writer failure lost", err)
|
|
}
|
|
if err = ExportCatalog(nil, catalog); err == nil {
|
|
t.Fatal("nil writer accepted")
|
|
}
|
|
}
|