-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
58 lines (47 loc) · 1.31 KB
/
errors_test.go
File metadata and controls
58 lines (47 loc) · 1.31 KB
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
package openapi_validator
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestValidationError(t *testing.T) {
// Arrange
err := &ValidationError{
Message: "test error",
Errors: []string{"detail 1", "detail 2"},
}
// Act
got := err.Error()
// Assert
if got != "test error" {
t.Errorf("expected test error, got %s", got)
}
}
func TestDefaultErrorEncoder(t *testing.T) {
// Arrange
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
err := errors.New("test validation error")
// Act
DefaultErrorEncoder(w, r, err)
// Assert
if w.Code != http.StatusBadRequest {
t.Errorf("expected status 400, got %d", w.Code)
}
if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", w.Header().Get("Content-Type"))
}
var resp ValidationError
if errUnmarshal := json.Unmarshal(w.Body.Bytes(), &resp); errUnmarshal != nil {
t.Fatalf("failed to unmarshal response: %v", errUnmarshal)
}
if resp.Message != "Validation Failed" {
t.Errorf("expected Message Validation Failed, got %s", resp.Message)
}
if len(resp.Errors) == 0 || !strings.Contains(resp.Errors[0], "test validation error") {
t.Errorf("expected error details to contain 'test validation error', got %v", resp.Errors)
}
}