-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem_details_test.go
156 lines (143 loc) · 3.44 KB
/
problem_details_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package httpsuite
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_SetProblemBaseURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Set valid base URL",
input: "https://api.example.com",
expected: "https://api.example.com",
},
{
name: "Set base URL to blank",
input: BlankUrl,
expected: BlankUrl,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetProblemBaseURL(tt.input)
assert.Equal(t, tt.expected, problemBaseURL)
})
}
}
func Test_SetProblemErrorTypePath(t *testing.T) {
tests := []struct {
name string
errorKey string
path string
expected string
}{
{
name: "Set custom error path",
errorKey: "custom_error",
path: "/errors/custom-error",
expected: "/errors/custom-error",
},
{
name: "Override existing path",
errorKey: "validation_error",
path: "/errors/new-validation-error",
expected: "/errors/new-validation-error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetProblemErrorTypePath(tt.errorKey, tt.path)
assert.Equal(t, tt.expected, errorTypePaths[tt.errorKey])
})
}
}
func Test_GetProblemTypeURL(t *testing.T) {
// Setup initial state
SetProblemBaseURL("https://api.example.com")
SetProblemErrorTypePath("validation_error", "/errors/validation-error")
tests := []struct {
name string
errorType string
expectedURL string
}{
{
name: "Valid error type",
errorType: "validation_error",
expectedURL: "https://api.example.com/errors/validation-error",
},
{
name: "Unknown error type",
errorType: "unknown_error",
expectedURL: BlankUrl,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetProblemTypeURL(tt.errorType)
assert.Equal(t, tt.expectedURL, result)
})
}
}
func Test_getProblemBaseURL(t *testing.T) {
tests := []struct {
name string
baseURL string
expectedResult string
}{
{
name: "Base URL is set",
baseURL: "https://api.example.com",
expectedResult: "https://api.example.com",
},
{
name: "Base URL is about:blank",
baseURL: BlankUrl,
expectedResult: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
problemBaseURL = tt.baseURL
assert.Equal(t, tt.expectedResult, getProblemBaseURL())
})
}
}
func Test_NewProblemDetails(t *testing.T) {
tests := []struct {
name string
status int
problemType string
title string
detail string
expectedType string
}{
{
name: "All fields provided",
status: 400,
problemType: "https://api.example.com/errors/validation-error",
title: "Validation Error",
detail: "Invalid input",
expectedType: "https://api.example.com/errors/validation-error",
},
{
name: "Empty problem type",
status: 404,
problemType: "",
title: "Not Found",
detail: "The requested resource was not found",
expectedType: BlankUrl,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
details := NewProblemDetails(tt.status, tt.problemType, tt.title, tt.detail)
assert.Equal(t, tt.status, details.Status)
assert.Equal(t, tt.title, details.Title)
assert.Equal(t, tt.detail, details.Detail)
assert.Equal(t, tt.expectedType, details.Type)
})
}
}