Skip to content

Commit ab35184

Browse files
committed
Add tests for HTTPError methods
1 parent 3c39bce commit ab35184

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

error_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package tempmail
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestHTTPError_Error(t *testing.T) {
12+
httpErr := &HTTPError{
13+
Response: &http.Response{StatusCode: http.StatusBadRequest},
14+
ErrorDetails: HTTPErrorError{
15+
Type: "request_error",
16+
Code: "not_found",
17+
Detail: "The requested resource was not found",
18+
},
19+
Meta: HTTPErrorMeta{
20+
RequestID: "req_123456789",
21+
},
22+
}
23+
24+
expected := "status 400, error type: request_error, code: not_found, detail: The requested resource was not found"
25+
assert.Equal(t, expected, httpErr.Error())
26+
}
27+
28+
func TestHTTPError_Format(t *testing.T) {
29+
httpErr := &HTTPError{
30+
Response: &http.Response{StatusCode: http.StatusBadRequest},
31+
ErrorDetails: HTTPErrorError{
32+
Type: "request_error",
33+
Code: "not_found",
34+
Detail: "The requested resource was not found",
35+
},
36+
Meta: HTTPErrorMeta{
37+
RequestID: "req_123456789",
38+
},
39+
}
40+
41+
t.Run("format with %s", func(t *testing.T) {
42+
result := fmt.Sprintf("%s", httpErr)
43+
expected := "status 400, error type: request_error, code: not_found, detail: The requested resource was not found"
44+
assert.Equal(t, expected, result)
45+
})
46+
47+
t.Run("format with %v", func(t *testing.T) {
48+
result := fmt.Sprintf("%v", httpErr)
49+
expected := "status 400, error type: request_error, code: not_found, detail: The requested resource was not found"
50+
assert.Equal(t, expected, result)
51+
})
52+
53+
t.Run("format with %+v", func(t *testing.T) {
54+
result := fmt.Sprintf("%+v", httpErr)
55+
expected := "status 400, error type: request_error, code: not_found, detail: The requested resource was not found, request_id: req_123456789"
56+
assert.Equal(t, expected, result)
57+
})
58+
59+
t.Run("format with %q", func(t *testing.T) {
60+
result := fmt.Sprintf("%q", httpErr)
61+
expected := "\"status 400, error type: request_error, code: not_found, detail: The requested resource was not found\""
62+
assert.Equal(t, expected, result)
63+
})
64+
}
65+
66+
func TestHTTPError_fullError(t *testing.T) {
67+
httpErr := &HTTPError{
68+
Response: &http.Response{StatusCode: http.StatusBadGateway},
69+
ErrorDetails: HTTPErrorError{
70+
Type: "api_error",
71+
Code: "internal_error",
72+
Detail: "Internal server error",
73+
},
74+
Meta: HTTPErrorMeta{
75+
RequestID: "req_987654321",
76+
},
77+
}
78+
79+
expected := "status 502, error type: api_error, code: internal_error, detail: Internal server error, request_id: req_987654321"
80+
assert.Equal(t, expected, httpErr.fullError())
81+
}

0 commit comments

Comments
 (0)