forked from watson/http-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
163 lines (144 loc) · 5 KB
/
test.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict'
var test = require('tape')
var http = require('http')
var Buffer = require('safe-buffer').Buffer
var httpHeaders = require('./')
var requestLine = 'GET /foo HTTP/1.1\r\n'
var statusLine = 'HTTP/1.1 200 OK\r\n'
var msgHeaders = 'Date: Tue, 10 Jun 2014 07:29:20 GMT\r\n' +
'Connection: keep-alive\r\n' +
'Transfer-Encoding: chunked\r\n' +
'Age: foo\r\n' +
'Age: bar\r\n' +
'Set-Cookie: cookie\r\n' +
'X-List: A\r\n' +
'X-Multi-Line-Header: Foo\r\n' +
' Bar\r\n' +
'X-List: B\r\n' +
'\r\n'
var requestMsg = requestLine + msgHeaders + 'Hello: World'
var responseMsg = statusLine + msgHeaders + 'Hello: World'
var headerResult = {
date: 'Tue, 10 Jun 2014 07:29:20 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked',
age: 'foo',
'set-cookie': ['cookie'],
'x-list': 'A, B',
'x-multi-line-header': 'Foo Bar'
}
var responseResult = {
version: { major: 1, minor: 1 },
statusCode: 200,
statusMessage: 'OK',
headers: headerResult
}
var requestResult = {
method: 'GET',
url: '/foo',
version: { major: 1, minor: 1 },
headers: headerResult
}
test('no argument', function (t) {
t.deepEqual(httpHeaders(), {})
t.deepEqual(httpHeaders(undefined, true), {})
t.end()
})
test('empty string', function (t) {
t.deepEqual(httpHeaders(''), {})
t.deepEqual(httpHeaders('', true), {})
t.end()
})
test('empty object', function (t) {
t.deepEqual(httpHeaders({}), {})
t.deepEqual(httpHeaders({}, true), {})
t.end()
})
test('empty buffer', function (t) {
t.deepEqual(httpHeaders(new Buffer('')), {})
t.deepEqual(httpHeaders(new Buffer(''), true), {})
t.end()
})
test('start-line + header', function (t) {
t.deepEqual(httpHeaders(requestLine + msgHeaders), requestResult)
t.deepEqual(httpHeaders(statusLine + msgHeaders), responseResult)
t.deepEqual(httpHeaders(new Buffer(requestLine + msgHeaders)), requestResult)
t.deepEqual(httpHeaders(new Buffer(statusLine + msgHeaders)), responseResult)
t.deepEqual(httpHeaders(requestLine + msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(statusLine + msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(new Buffer(requestLine + msgHeaders), true), headerResult)
t.deepEqual(httpHeaders(new Buffer(statusLine + msgHeaders), true), headerResult)
t.end()
})
test('request-line only', function (t) {
var requestResult = {
method: 'GET',
url: '/foo',
version: { major: 1, minor: 1 },
headers: {}
}
t.deepEqual(httpHeaders(requestLine + '\r\n'), requestResult)
t.deepEqual(httpHeaders(new Buffer(requestLine + '\r\n')), requestResult)
t.deepEqual(httpHeaders(requestLine + '\r\n', true), {})
t.deepEqual(httpHeaders(new Buffer(requestLine + '\r\n'), true), {})
t.end()
})
test('status-line only', function (t) {
var responseResult = {
version: { major: 1, minor: 1 },
statusCode: 200,
statusMessage: 'OK',
headers: {}
}
t.deepEqual(httpHeaders(statusLine + '\r\n'), responseResult)
t.deepEqual(httpHeaders(new Buffer(statusLine + '\r\n')), responseResult)
t.deepEqual(httpHeaders(statusLine + '\r\n', true), {})
t.deepEqual(httpHeaders(new Buffer(statusLine + '\r\n'), true), {})
t.end()
})
test('headers only', function (t) {
t.deepEqual(httpHeaders(msgHeaders), headerResult)
t.deepEqual(httpHeaders(new Buffer(msgHeaders)), headerResult)
t.deepEqual(httpHeaders(msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(new Buffer(msgHeaders), true), headerResult)
t.end()
})
test('full http response', function (t) {
t.deepEqual(httpHeaders(requestMsg), requestResult)
t.deepEqual(httpHeaders(responseMsg), responseResult)
t.deepEqual(httpHeaders(new Buffer(requestMsg)), requestResult)
t.deepEqual(httpHeaders(new Buffer(responseMsg)), responseResult)
t.deepEqual(httpHeaders(requestMsg, true), headerResult)
t.deepEqual(httpHeaders(responseMsg, true), headerResult)
t.deepEqual(httpHeaders(new Buffer(requestMsg), true), headerResult)
t.deepEqual(httpHeaders(new Buffer(responseMsg), true), headerResult)
t.end()
})
test('http.ServerResponse', function (t) {
t.test('real http.ServerResponse object', function (t) {
var res = new http.ServerResponse({})
t.deepEqual(httpHeaders(res), {})
t.deepEqual(httpHeaders(res, true), {})
t.end()
})
t.test('no _header property', function (t) {
t.deepEqual(httpHeaders({ _header: undefined }), {})
t.deepEqual(httpHeaders({ _header: undefined }, true), {})
t.end()
})
t.test('empty string as _header', function (t) {
t.deepEqual(httpHeaders({ _header: '' }), {})
t.deepEqual(httpHeaders({ _header: '' }, true), {})
t.end()
})
t.test('normal _header property', function (t) {
t.deepEqual(httpHeaders({ _header: statusLine + msgHeaders }), responseResult)
t.deepEqual(httpHeaders({ _header: statusLine + msgHeaders }, true), headerResult)
t.end()
})
})
test('set-cookie', function (t) {
t.deepEqual(httpHeaders('Set-Cookie: foo'), { 'set-cookie': ['foo'] })
t.deepEqual(httpHeaders('Set-Cookie: foo\r\nSet-Cookie: bar'), { 'set-cookie': ['foo', 'bar'] })
t.end()
})