-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
dump_test.go
66 lines (53 loc) · 1.52 KB
/
dump_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
package httplab
import (
"bytes"
"fmt"
"net/http"
"testing"
"sort"
"strings"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDumpRequestWithJSON(t *testing.T) {
t.Run("should be indented", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/withJSON", bytes.NewBuffer(
[]byte(`{"foo": "bar", "a": [1,2,3]}`),
))
req.Header.Set("Content-Type", "application/json")
buf, err := DumpRequest(req)
require.NoError(t, err)
fmt.Printf("%s\n", buf)
})
t.Run("should be displayed as is", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/invalidJSON", bytes.NewBuffer(
[]byte(`invalid json`),
))
req.Header.Set("Content-Type", "application/json")
buf, err := DumpRequest(req)
require.NoError(t, err)
fmt.Printf("%s\n", buf)
})
}
func TestDumpRequestHeaders(t *testing.T) {
t.Run("request headers should be dumped in sorted order", func(t *testing.T) {
keys := []string{"B", "A", "C", "D", "E", "F", "H", "G", "I"}
req, _ := http.NewRequest("GET", "/", bytes.NewBuffer(nil))
for _, k := range keys {
req.Header.Set(k, "")
}
buf, err := DumpRequest(req)
require.NoError(t, err)
sort.Strings(keys)
startLine := "GET / HTTP/1.1\n"
response := startLine + strings.Join(keys, ": \n") + ": \n"
assert.Contains(t, response, string(Decolorize(buf)))
})
}
func TestDecolorization(t *testing.T) {
for i := range [107]struct{}{} {
text := "Some Text"
nocolor := Decolorize([]byte(withColor(i, text)))
assert.Equal(t, text, string(nocolor))
}
}