File: error_test.go

package info (click to toggle)
golang-github-manyminds-api2go 1.0-RC2%2Bgit20161229.31.dc368bb-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 484 kB
  • ctags: 488
  • sloc: sh: 23; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package api2go

import (
	"errors"
	"net/http"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

type ErrorMarshaler struct{}

func (e ErrorMarshaler) Marshal(i interface{}) ([]byte, error) {
	return []byte{}, errors.New("this will always fail")
}
func (e ErrorMarshaler) Unmarshal(data []byte, i interface{}) error {
	return nil
}

func (e ErrorMarshaler) MarshalError(error) string {
	return ""
}

var _ = Describe("Errors test", func() {
	Context("validate error logic", func() {
		It("can create array tree", func() {
			httpErr := NewHTTPError(errors.New("hi"), "hi", 0)
			for i := 0; i < 20; i++ {
				httpErr.Errors = append(httpErr.Errors, Error{})
			}

			Expect(len(httpErr.Errors)).To(Equal(20))
		})
	})

	Context("Marshalling", func() {
		It("will be marshalled correctly with default error", func() {
			httpErr := NewHTTPError(nil, "Invalid use case done", http.StatusInternalServerError)
			result := marshalHTTPError(httpErr)
			expected := `{"errors":[{"status":"500","title":"Invalid use case done"}]}`
			Expect(result).To(Equal(expected))
		})

		It("will be marshalled correctly without child errors", func() {
			httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 400)
			result := marshalHTTPError(httpErr)
			expected := `{"errors":[{"status":"400","title":"Bad Request"}]}`
			Expect(result).To(Equal(expected))
		})

		It("will be marshalled correctly with child errors", func() {
			httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 500)

			errorOne := Error{
				ID: "001",
				Links: &ErrorLinks{
					About: "http://bla/blub",
				},
				Status: "500",
				Code:   "001",
				Title:  "Title must not be empty",
				Detail: "Never occures in real life",
				Source: &ErrorSource{
					Pointer: "#titleField",
				},
				Meta: map[string]interface{}{
					"creator": "api2go",
				},
			}

			httpErr.Errors = append(httpErr.Errors, errorOne)

			result := marshalHTTPError(httpErr)
			expected := `{"errors":[{"id":"001","links":{"about":"http://bla/blub"},"status":"500","code":"001","title":"Title must not be empty","detail":"Never occures in real life","source":{"pointer":"#titleField"},"meta":{"creator":"api2go"}}]}`
			Expect(result).To(Equal(expected))
		})

		It("will be marshalled correctly with child errors without links or source", func() {
			httpErr := NewHTTPError(errors.New("Bad Request"), "Bad Request", 500)

			errorOne := Error{
				ID:     "001",
				Status: "500",
				Code:   "001",
				Title:  "Title must not be empty",
				Detail: "Never occures in real life",
				Meta: map[string]interface{}{
					"creator": "api2go",
				},
			}

			httpErr.Errors = append(httpErr.Errors, errorOne)

			result := marshalHTTPError(httpErr)
			expected := `{"errors":[{"id":"001","status":"500","code":"001","title":"Title must not be empty","detail":"Never occures in real life","meta":{"creator":"api2go"}}]}`
			Expect(result).To(Equal(expected))
		})
	})
})