-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_entry_test.go
97 lines (93 loc) · 2.04 KB
/
log_entry_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
91
92
93
94
95
96
97
package spectest
import (
"bytes"
"io"
"net/http"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewHTTPRequestLogEntry(t *testing.T) {
type args struct {
req *http.Request
}
tests := []struct {
name string
args args
want LogEntry
wantErr bool
}{
{
name: "test",
args: args{
req: &http.Request{
Method: http.MethodGet,
URL: &url.URL{Path: "/path"},
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Host: "example.com",
Body: io.NopCloser(bytes.NewBufferString("request body")),
},
},
want: LogEntry{
Header: "GET /path HTTP/1.1\r\nHost: example.com\r\n\r\n",
Body: "request body",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewHTTPRequestLogEntry(tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("NewHTTPRequestLogEntry() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestNewHTTPResponseLogEntry(t *testing.T) {
type args struct {
res *http.Response
}
tests := []struct {
name string
args args
want LogEntry
wantErr bool
}{
{
name: "test",
args: args{
res: &http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: http.StatusOK,
ContentLength: 21,
Body: io.NopCloser(bytes.NewBufferString("response body")),
},
},
want: LogEntry{
Header: "HTTP/1.1 200 OK\r\nContent-Length: 21\r\n\r\n",
Body: "response body",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewHTTPResponseLogEntry(tt.args.res)
if (err != nil) != tt.wantErr {
t.Errorf("NewHTTPResponseLogEntry() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
}