/
acceptance.js
160 lines (132 loc) · 3.53 KB
/
acceptance.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
/**
* Push multiple files at a time.
*/
var PassThrough = require('readable-stream/passthrough')
var assert = require('assert')
var spdy = require('spdy')
var http = require('http')
var path = require('path')
var koa = require('koa')
var serve = require('..')
var port = 4401
var server = spdy.createServer({
ssl: false,
plain: true,
}).on('request', function (req, res) {
fn(req, res)
}).listen(port)
var agent = spdy.createAgent({
host: '127.0.0.1',
port: port,
spdy: {
ssl: false,
plain: true,
}
})
describe('this.body=', function () {
it('should support custom bodies', function (done) {
var app = koa()
app.use(serve(fixture()))
app.use(function* (next) {
this.type = 'css'
this.body = '@import "index.css";'
this.etag = 'asdf'
})
fn = app.callback()
var streams = []
agent.on('push', function onpush(stream) {
var length = streams.push(stream)
assert(length <= 2)
if (length === 1) return
agent.removeListener('push', onpush)
var urls = streams.map(function (stream) {
return stream.url
})
assert(~urls.indexOf('/index.css'))
assert(~urls.indexOf('/a.css'))
done()
})
http.request({
path: '/',
agent: agent
}).on('response', function (response) {
assert.equal(response.statusCode, 200)
assert(/text\/css/.test(response.headers['content-type']))
response.resume()
response.on('error', done)
}).on('error', done).end()
})
it('it should not push when there is no body', function (done) {
var app = koa()
app.use(serve(fixture()))
app.use(function* (next) {
this.type = 'css'
this.body = '@import "index.css";'
this.etag = 'asdf'
if (this.fresh) this.status = 304
})
fn = app.callback()
var streams = []
agent.on('push', throwError)
http.request({
path: '/',
agent: agent,
headers: {
'if-none-match': '"asdf"'
}
}).on('response', function (response) {
assert.equal(response.statusCode, 304)
response.resume()
response.on('error', done)
response.on('end', function () {
agent.removeListener('push', throwError)
done()
})
}).on('error', done).end()
function throwError() {
throw new Error('boom')
}
})
it('should support stream bodies', function (done) {
var app = koa()
app.use(serve(fixture()))
app.use(function* (next) {
this.type = 'css'
var stream = this.body = new PassThrough()
stream.end('@import "index.css";')
})
fn = app.callback()
var streams = []
agent.on('push', function onpush(stream) {
var length = streams.push(stream)
assert(length <= 2)
if (length === 1) return
agent.removeListener('push', onpush)
var urls = streams.map(function (stream) {
return stream.url
})
assert(~urls.indexOf('/index.css'))
assert(~urls.indexOf('/a.css'))
done()
})
http.request({
path: '/',
agent: agent
}).on('response', function (response) {
assert.equal(response.statusCode, 200)
assert(/text\/css/.test(response.headers['content-type']))
response.resume()
response.on('error', done)
}).on('error', done).end()
})
})
agent.on('error', function (err) {
console.error(err.stack)
process.exit(1)
})
agent.on('push', function (stream) {
console.log('pushed: %s', stream.url)
})
function fixture(file) {
return path.join(__dirname, 'fixtures-acceptance', file || '')
}