2016-07-18 17:48:35 -04:00
|
|
|
package jsonapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
|
|
|
|
err := &ErrorObject{Title: "Title test.", Detail: "Detail test."}
|
|
|
|
var input error = err
|
|
|
|
|
|
|
|
output := input.Error()
|
|
|
|
|
|
|
|
if output != fmt.Sprintf("Error: %s %s\n", err.Title, err.Detail) {
|
|
|
|
t.Fatal("Unexpected output.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
|
|
|
|
var marshalErrorsTableTasts = []struct {
|
2017-02-17 18:11:39 -05:00
|
|
|
Title string
|
|
|
|
In []*ErrorObject
|
|
|
|
Out map[string]interface{}
|
2016-07-18 17:48:35 -04:00
|
|
|
}{
|
2017-02-17 18:11:39 -05:00
|
|
|
{
|
|
|
|
Title: "TestFieldsAreSerializedAsNeeded",
|
|
|
|
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
|
2016-07-18 17:48:35 -04:00
|
|
|
Out: map[string]interface{}{"errors": []interface{}{
|
|
|
|
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
|
|
|
|
}},
|
|
|
|
},
|
2017-02-17 18:11:39 -05:00
|
|
|
{
|
|
|
|
Title: "TestMetaFieldIsSerializedProperly",
|
|
|
|
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
|
2016-07-18 17:48:35 -04:00
|
|
|
Out: map[string]interface{}{"errors": []interface{}{
|
|
|
|
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testRow := range marshalErrorsTableTasts {
|
2017-02-17 18:11:39 -05:00
|
|
|
t.Run(testRow.Title, func(t *testing.T) {
|
|
|
|
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
|
|
|
|
var writer io.Writer = buffer
|
2016-07-18 17:48:35 -04:00
|
|
|
|
2017-02-17 18:11:39 -05:00
|
|
|
_ = MarshalErrors(writer, testRow.In)
|
|
|
|
json.Unmarshal(buffer.Bytes(), &output)
|
2016-07-18 17:48:35 -04:00
|
|
|
|
2017-02-17 18:11:39 -05:00
|
|
|
if !reflect.DeepEqual(output, testRow.Out) {
|
|
|
|
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
|
|
|
|
}
|
|
|
|
})
|
2016-07-18 17:48:35 -04:00
|
|
|
}
|
|
|
|
}
|