forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
110 lines (107 loc) · 3.97 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bytes"
"github.com/cyfdecyf/bufio"
"strings"
"testing"
"time"
)
func TestParseRequestURI(t *testing.T) {
var testData = []struct {
rawurl string
url *URL
}{
{"http://www.g.com", &URL{"www.g.com:80", "www.g.com", "80", "g.com", ""}},
{"http://plus.g.com/", &URL{"plus.g.com:80", "plus.g.com", "80", "g.com", "/"}},
{"https://g.com:80", &URL{"g.com:80", "g.com", "80", "g.com", ""}},
{"http://mail.g.com:80/", &URL{"mail.g.com:80", "mail.g.com", "80", "g.com", "/"}},
{"http://g.com:80/ncr", &URL{"g.com:80", "g.com", "80", "g.com", "/ncr"}},
{"https://g.com/ncr/tree", &URL{"g.com:443", "g.com", "443", "g.com", "/ncr/tree"}},
{"www.g.com.hk:80/", &URL{"www.g.com.hk:80", "www.g.com.hk", "80", "com.hk", "/"}},
{"g.com.jp:80", &URL{"g.com.jp:80", "g.com.jp", "80", "com.jp", ""}},
{"g.com", &URL{"g.com:80", "g.com", "80", "g.com", ""}},
{"g.com:8000/ncr", &URL{"g.com:8000", "g.com", "8000", "g.com", "/ncr"}},
{"g.com/ncr/tree", &URL{"g.com:80", "g.com", "80", "g.com", "/ncr/tree"}},
{"simplehost", &URL{"simplehost:80", "simplehost", "80", "", ""}},
{"simplehost:8080", &URL{"simplehost:8080", "simplehost", "8080", "", ""}},
{"192.168.1.1:8080/", &URL{"192.168.1.1:8080", "192.168.1.1", "8080", "", "/"}},
{"/helloworld", &URL{"", "", "", "", "/helloworld"}},
}
for _, td := range testData {
url, err := ParseRequestURI(td.rawurl)
if url == nil {
if err == nil {
t.Error("nil URL must report error")
}
if td.url != nil {
t.Error(td.rawurl, "should not report error")
}
continue
}
if err != nil {
t.Error(td.rawurl, "non nil URL should not report error")
}
if url.HostPort != td.url.HostPort {
t.Error(td.rawurl, "parsed hostPort wrong:", td.url.HostPort, "got", url.HostPort)
}
if url.Host != td.url.Host {
t.Error(td.rawurl, "parsed host wrong:", td.url.Host, "got", url.Host)
}
if url.Port != td.url.Port {
t.Error(td.rawurl, "parsed port wrong:", td.url.Port, "got", url.Port)
}
if url.Domain != td.url.Domain {
t.Error(td.rawurl, "parsed domain wrong:", td.url.Domain, "got", url.Domain)
}
if url.Path != td.url.Path {
t.Error(td.rawurl, "parsed path wrong:", td.url.Path, "got", url.Path)
}
}
}
func TestParseHeader(t *testing.T) {
var testData = []struct {
raw string
newraw string
header *Header
}{
{"content-length: 64\r\nConnection: keep-alive\r\n\r\n",
"content-length: 64\r\n",
&Header{ContLen: 64, Chunking: false, ConnectionKeepAlive: true}},
{"Connection: keep-alive\r\nKeep-Alive: timeout=10\r\nTransfer-Encoding: chunked\r\nTE: trailers\r\n\r\n",
"",
&Header{ContLen: -1, Chunking: true, ConnectionKeepAlive: true,
KeepAlive: 10 * time.Second}},
{"Connection:\r\n keep-alive\r\nKeep-Alive: max=5,\r\n timeout=10\r\n\r\n",
"",
&Header{ContLen: -1, Chunking: false, ConnectionKeepAlive: true,
KeepAlive: 10 * time.Second}},
{"Connection: \r\n close\r\nLong: line\r\n continued\r\n\tagain\r\n\r\n",
"Long: line continued again\r\n",
&Header{ContLen: -1, Chunking: false, ConnectionKeepAlive: false}},
}
for _, td := range testData {
var h Header
var newraw bytes.Buffer
h.parseHeader(bufio.NewReader(strings.NewReader(td.raw)), &newraw, nil)
if h.ContLen != td.header.ContLen {
t.Errorf("%q parsed content length wrong, should be %d, get %d\n",
td.raw, td.header.ContLen, h.ContLen)
}
if h.Chunking != td.header.Chunking {
t.Errorf("%q parsed chunking wrong, should be %t, get %t\n",
td.raw, td.header.Chunking, h.Chunking)
}
if h.ConnectionKeepAlive != td.header.ConnectionKeepAlive {
t.Errorf("%q parsed connection wrong, should be %v, get %v\n",
td.raw, td.header.ConnectionKeepAlive, h.ConnectionKeepAlive)
}
if h.KeepAlive != td.header.KeepAlive {
t.Errorf("%q parsed keep alive wrong, should be %v, get %v\n",
td.raw, td.header.KeepAlive, h.KeepAlive)
}
if newraw.String() != td.newraw {
t.Errorf("%q parsed raw wrong\nshould be: %q\ngot: %q\n",
td.raw, td.newraw, newraw.Bytes())
}
}
}