forked from cortezaproject/corteza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
90 lines (71 loc) · 2.25 KB
/
http_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
package errors
import (
"bytes"
"context"
"fmt"
"os"
"syscall"
"testing"
"github.com/stretchr/testify/require"
)
func Example_writeHttpPlain() {
writeHttpPlain(os.Stdout, fmt.Errorf("dummy error"), true)
// Output:
// Error: dummy error
}
func Example_writeHttpJSON() {
writeHttpJSON(context.Background(), os.Stdout, fmt.Errorf("dummy error"), true)
// Output:
// {"error":{"message":"dummy error"}}
}
func Example_writeHttpJSON_clientAbortedConnectionReset() {
writeHttpJSON(context.Background(), os.Stdout, syscall.ECONNRESET, true)
// Output:
}
func Example_writeHttpPlain_clientAbortedConnectionReset() {
writeHttpPlain(os.Stdout, syscall.ECONNRESET, true)
// Output:
}
func Example_writeHttpJSON_clientAbortedConnectionPipe() {
writeHttpJSON(context.Background(), os.Stdout, syscall.EPIPE, true)
// Output:
}
func Example_writeHttpPlain_clientAbortedConnectionPipe() {
writeHttpPlain(os.Stdout, syscall.EPIPE, true)
// Output:
}
func Example_writeHttpPlain_masked() {
err := New(0, "dummy error", Meta("a", "b"), Meta(&Error{}, "nope"))
err.stack = nil // will not test the stack as file path & line numbers might change
writeHttpPlain(os.Stdout, err, true)
// Output:
// Error: dummy error
}
func Example_writeHttpPlain_unmasked() {
err := New(0, "dummy error", Meta("a", "b"), Meta(&Error{}, "nope"))
err.stack = nil // will not test the stack as file path & line numbers might change
writeHttpPlain(os.Stdout, err, false)
// Output:
// Error: dummy error
// --------------------------------------------------------------------------------
// a: b
// --------------------------------------------------------------------------------
}
func Test_writeHttpJSON(t *testing.T) {
var (
err = New(0, "dummy error", Meta("meta", "meta"))
buf = bytes.NewBuffer(nil)
req = require.New(t)
)
buf.Truncate(0)
writeHttpJSON(context.Background(), buf, err, false)
req.Contains(buf.String(), "dummy error")
req.Contains(buf.String(), "meta")
req.Contains(buf.String(), "stack")
// when errors are masked (production env) we do not add meta or stack
buf.Truncate(0)
writeHttpJSON(context.Background(), buf, err, true)
req.Contains(buf.String(), "dummy error")
req.NotContains(buf.String(), "meta")
req.NotContains(buf.String(), "stack")
}