-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstatus_test.go
More file actions
85 lines (74 loc) · 2.17 KB
/
Copy pathstatus_test.go
File metadata and controls
85 lines (74 loc) · 2.17 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
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
package goose
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
type jsonErr struct{}
func (jsonErr) Error() string { return "json error" }
func (jsonErr) MarshalJSON() ([]byte, error) {
return []byte(`{"msg":"json error"}`), nil
}
type headerErr struct{}
func (headerErr) Error() string { return "header error" }
func (headerErr) Headers() http.Header {
h := http.Header{}
h.Set("X-Test", "1")
return h
}
type statusErr struct{}
func (statusErr) Error() string { return "status error" }
func (statusErr) StatusCode() int {
return 418
}
func TestDefaultEncodeError_plain(t *testing.T) {
rr := httptest.NewRecorder()
DefaultEncodeError(context.Background(), errors.New("plain error"), rr)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusInternalServerError)
}
if ct := resp.Header.Get(ContentTypeKey); ct != "text/plain; charset=utf-8" {
t.Errorf("Content-Type = %q, want text/plain", ct)
}
if !bytes.Contains(body, []byte("plain error")) {
t.Errorf("body = %q, want contains 'plain error'", body)
}
}
func TestDefaultEncodeError_json(t *testing.T) {
rr := httptest.NewRecorder()
DefaultEncodeError(context.Background(), jsonErr{}, rr)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if ct := resp.Header.Get(ContentTypeKey); ct != JsonContentType {
t.Errorf("Content-Type = %q, want %q", ct, JsonContentType)
}
if !bytes.Contains(body, []byte(`"msg":"json error"`)) {
t.Errorf("body = %q, want contains json error", body)
}
}
func TestDefaultEncodeError_header(t *testing.T) {
rr := httptest.NewRecorder()
DefaultEncodeError(context.Background(), headerErr{}, rr)
resp := rr.Result()
defer resp.Body.Close()
if resp.Header.Get("X-Test") != "1" {
t.Errorf("X-Test header not set")
}
}
func TestDefaultEncodeError_status(t *testing.T) {
rr := httptest.NewRecorder()
DefaultEncodeError(context.Background(), statusErr{}, rr)
resp := rr.Result()
defer resp.Body.Close()
if resp.StatusCode != 418 {
t.Errorf("status = %d, want 418", resp.StatusCode)
}
}