Skip to content

Commit

Permalink
Convert more tests for core http2
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Aug 24, 2017
1 parent 6429159 commit dec3d57
Showing 1 changed file with 90 additions and 130 deletions.
220 changes: 90 additions & 130 deletions test/index.js
Expand Up @@ -67,8 +67,8 @@ describe('Underdog', () => {

const clients = server.connections.map((conn) => {

return Http2.connect(server.info.uri, { rejectUnauthorized: false }));
};
return Http2.connect(server.info.uri, { rejectUnauthorized: false });
});

return cb(err, server, clients);
});
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('Underdog', () => {
stream.on('error', cb);
};

it('pushes complete resource for an explicitly specified response.', { plan: 17 }, (done) => {
it('pushes complete resource for an explicitly specified response.', { plan: 8 }, (done) => {

makeServer([
{
Expand All @@ -145,57 +145,35 @@ describe('Underdog', () => {
}
}
],
(err, srv, ports) => {
(err, srv, clients) => {

expect(err).to.not.exist();

Items.parallel(ports, (port, nxt) => {

const next = callNTimes(4, nxt);

const request = Http2.get({ path: '/', port, agent });

request.on('response', (response) => {

expect(response.statusCode).to.equal(200);
Items.parallel(clients, (client, nxt) => {

response.on('data', (data) => {
const next = callNTimes(2, client.destroy, nxt);
const request = client.request({ ':path': '/' });

expect(data.toString()).to.equal('body');
next();
});
request.on('response', (headers) => {

response.on('end', next);
expect(headers[':status']).to.equal(200);
expectToStream(request, 'body', next);
});

request.on('push', (promise) => {

expect(promise).to.contain({
method: 'GET',
url: '/push-me',
scheme: 'https',
host: 'localhost'
});

expect(promise.headers).to.equal({
host: 'localhost',
'user-agent': 'shot'
});

promise.on('response', (pushStream) => {

expect(pushStream.statusCode).to.equal(201);
expect(pushStream.headers).to.contain({
'x-custom': 'winnie'
});
client.on('stream', (pushed, reqHeaders) => {

pushStream.on('data', (data) => {
pushed.on('push', (resHeaders) => {

expect(data.toString()).to.equal('pushed');
next();
expect(reqHeaders).to.contain({
':method': 'GET',
':path': '/push-me',
':scheme': 'https',
'user-agent': 'shot'
});

pushStream.on('end', next);
expect(resHeaders[':status']).to.equal(201);
expect(resHeaders['x-custom']).to.equal('winnie');
expectToStream(pushed, 'pushed', next);
});
});
}, (err1) => {
Expand All @@ -205,7 +183,7 @@ describe('Underdog', () => {
});
});

it('pushes resource for the request\'s current response.', { plan: 15 }, (done) => {
it('pushes resource for the request\'s current response.', { plan: 7 }, (done) => {

makeServer([
{
Expand Down Expand Up @@ -238,54 +216,34 @@ describe('Underdog', () => {
}
}
],
(err, srv, ports) => {
(err, srv, clients) => {

expect(err).to.not.exist();

Items.parallel(ports, (port, nxt) => {

const next = callNTimes(4, nxt);

const request = Http2.get({ path: '/', port, agent });

request.on('response', (response) => {

expect(response.statusCode).to.equal(200);
Items.parallel(clients, (client, nxt) => {

response.on('data', (data) => {
const next = callNTimes(2, client.destroy, nxt);
const request = client.request({ ':path': '/' });

expect(data.toString()).to.equal('body');
next();
});
request.on('response', (headers) => {

response.on('end', next);
expect(headers[':status']).to.equal(200);
expectToStream(request, 'body', next);
});

request.on('push', (promise) => {

expect(promise).to.contain({
method: 'GET',
url: '/push-me',
scheme: 'https',
host: 'localhost'
});

expect(promise.headers).to.equal({
host: 'localhost',
'user-agent': 'shot'
});

promise.on('response', (pushStream) => {

expect(pushStream.statusCode).to.equal(200);
client.on('stream', (pushed, reqHeaders) => {

pushStream.on('data', (data) => {
pushed.on('push', (resHeaders) => {

expect(data.toString()).to.equal('pushed');
next();
expect(reqHeaders).to.contain({
':method': 'GET',
':path': '/push-me',
':scheme': 'https',
'user-agent': 'shot'
});

pushStream.on('end', next);
expect(resHeaders[':status']).to.equal(200);
expectToStream(pushed, 'pushed', next);
});
});
}, (err1) => {
Expand All @@ -295,7 +253,7 @@ describe('Underdog', () => {
});
});

it.only('pushes resources from internal routes.', { plan: 6 }, (done) => {
it('pushes resources from internal routes.', { plan: 6 }, (done) => {

makeServer([
{
Expand Down Expand Up @@ -354,7 +312,7 @@ describe('Underdog', () => {
});
});

it('pushes multiple resources.', { plan: 25 }, (done) => {
it('pushes multiple resources.', { plan: 9 }, (done) => {

makeServer([
{
Expand Down Expand Up @@ -384,62 +342,55 @@ describe('Underdog', () => {
}
}
],
(err, srv, ports) => {
(err, srv, clients) => {

expect(err).to.not.exist();

Items.parallel(ports, (port, nxt) => {

const next = callNTimes(6, nxt);

const request = Http2.get({ path: '/', port, agent });

request.on('response', (response) => {

expect(response.statusCode).to.equal(200);
Items.parallel(clients, (client, nxt) => {

response.on('data', (data) => {
const next = callNTimes(3, client.destroy, nxt);
const request = client.request({ ':path': '/' });

expect(data.toString()).to.equal('body');
next();
});
request.on('response', (headers) => {

response.on('end', next);
expect(headers[':status']).to.equal(200);
expectToStream(request, 'body', next);
});

const allowed = ['/push-me', '/push-me-again'];

request.on('push', (promise) => {

const url = promise.url;
client.on('stream', (pushed, reqHeaders) => {

expect(allowed).to.contain(url);
pushed.on('push', (resHeaders) => {

// Do not allow same url twice
allowed.splice(allowed.indexOf(url), 1);
if (reqHeaders[':path'] !== '/push-me') {
return;
}

expect(promise).to.contain({
method: 'GET',
scheme: 'https',
host: 'localhost'
});
expect(reqHeaders).to.contain({
':method': 'GET',
':path': '/push-me',
':scheme': 'https',
'user-agent': 'shot'
});

expect(promise.headers).to.equal({
host: 'localhost',
'user-agent': 'shot'
expect(resHeaders[':status']).to.equal(200);
expectToStream(pushed, 'pushed', next);
});

promise.on('response', (pushStream) => {

expect(pushStream.statusCode).to.equal(200);
pushed.on('push', (resHeaders) => {

pushStream.on('data', (data) => {
if (reqHeaders[':path'] !== '/push-me-again') {
return;
}

expect(data.toString()).to.equal((url === '/push-me') ? 'pushed' : 'pushed again');
next();
expect(reqHeaders).to.contain({
':method': 'GET',
':path': '/push-me-again',
':scheme': 'https',
'user-agent': 'shot'
});

pushStream.on('end', next);
expect(resHeaders[':status']).to.equal(200);
expectToStream(pushed, 'pushed again', next);
});
});
}, (err1) => {
Expand All @@ -449,7 +400,7 @@ describe('Underdog', () => {
});
});

it('does not allow pushing without a response.', { plan: 3 }, (done) => {
it.only('does not allow pushing without a response.', { plan: 3 }, (done) => {

makeServer([
{
Expand All @@ -470,27 +421,36 @@ describe('Underdog', () => {
}
}
],
(err, srv, ports) => {
(err, srv, clients) => {

expect(err).to.not.exist();

const next = callNTimes(2, () => {

clients.forEach((client) => client.destroy());
return srv.stop(done);
});

srv.on('request-error', (request, err) => {

console.log(2, err);
expect(err.message).to.contain('Must server-push with a non-error response.');
return next();
});

// TODO specific language in this comment below may need to change.
// No need to test against spdy and http2 cause we aint pushin
// Also, https servers don't recover well from these errors :/
const port = ports[0];
const request = Http2.get({ path: '/', port, agent });
const request = clients[0].request({ ':path': '/' });

request.on('response', (response) => {
request.on('response', (headers) => {

expect(response.statusCode).to.equal(500);
srv.stop(done);
console.log(1, headers);
expect(headers[':status']).to.equal(500);
return next();
});

request.on('push', () => next(new Error('Should not make it here')));
request.on('push', () => done(new Error('Should not make it here')));
});
});

Expand Down Expand Up @@ -529,13 +489,13 @@ describe('Underdog', () => {
const port = ports[0];
const request = Http2.get({ path: '/', port, agent });

request.on('response', (response) => {
request.on('response', (headers) => {

expect(response.statusCode).to.equal(500);
expect(headers[':status']).to.equal(500);
srv.stop(done);
});

request.on('push', () => next(new Error('Should not make it here')));
request.on('push', () => done(new Error('Should not make it here')));
});
});

Expand Down

0 comments on commit dec3d57

Please sign in to comment.