-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproblem_test.go
114 lines (91 loc) · 2.36 KB
/
problem_test.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package problems
import (
"errors"
"net/http"
"net/url"
"testing"
)
var unAuthDetails = "you are unauthorized to access this resource"
func TestDefaultProblem(t *testing.T) {
problem := NewDetailedProblem(http.StatusUnauthorized, unAuthDetails)
typ, err := problem.ProblemType()
if err != nil {
t.Errorf("Unable to read problem type")
}
if typ != nil && typ.String() != problem.Type {
t.Errorf("Problem Types did not match")
}
if problem.ProblemTitle() != problem.Title {
t.Errorf("Problem Titles did not match")
}
err = ValidateProblem(problem)
if err != nil {
t.Errorf("problem is not valid")
}
}
type badProblemType struct{}
func (p badProblemType) ProblemType() (*url.URL, error) {
return nil, errors.New("i am a bad problem type")
}
func (p badProblemType) ProblemTitle() string {
return "something valid"
}
type badProblemTitle struct{}
func (p badProblemTitle) ProblemType() (*url.URL, error) {
return &url.URL{}, nil
}
func (p badProblemTitle) ProblemTitle() string {
return ""
}
func TestValidateProblem(t *testing.T) {
var err error
err = ValidateProblem(badProblemType{})
if err == nil {
t.Error("Only valid URI's should be allowed as problem types")
}
err = ValidateProblem(badProblemTitle{})
if err == nil {
t.Errorf("Empty strings should not be allowed as problem titles")
}
badURI := "::/"
err = ValidateProblem(&DefaultProblem{Type: badURI})
if err == nil {
t.Errorf("%q was allowed as a valid URI", badURI)
}
}
type creditProblem struct {
DefaultProblem
Balance float64 `json:"balance"`
Accounts []string `json:"accounts"`
}
func (cp *creditProblem) ProblemType() (*url.URL, error) {
u, err := url.Parse(cp.Type)
if err != nil {
return nil, err
}
return u, nil
}
func (cp *creditProblem) ProblemTitle() string {
return cp.Title
}
func TestCreditProblem(t *testing.T) {
problem := &creditProblem{
DefaultProblem: *NewDetailedProblem(http.StatusUnauthorized, unAuthDetails),
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}
typ, err := problem.ProblemType()
if err != nil {
t.Errorf("Unable to read problem type")
}
if typ != nil && typ.String() != problem.Type {
t.Errorf("Problem Types did not match")
}
if problem.ProblemTitle() != problem.Title {
t.Errorf("Problem Titles did not match")
}
err = ValidateProblem(problem)
if err != nil {
t.Errorf("problem is not valid")
}
}