supertest icon indicating copy to clipboard operation
supertest copied to clipboard

With http2 "socket hang up"

Open crystalfp opened this issue 5 years ago • 4 comments

I'm using supertest on Windows 10 and node 12.14.1 with great satisfaction. However, if I switch my server to http2 all tests fail. They prints only socket hang up and nothing more. My server is Koa and tests are written with typescript. Here is an excerpt:

import http2 from "http2";

const app = new Koa();
const options = {
	key:  fs.readFileSync(path.join(__dirname, "cert", "privkey.pem")),
	cert: fs.readFileSync(path.join(__dirname, "cert", "cert.pem"))
};

const server = http2
			.createSecureServer(options, app.callback())
			.once("error", handleServerErrors)
			.listen(3111, () => runApp("https"));

request = supertest(server);

Any ideas? Everything runs smoothly if I use http and createServer()

Thanks! mario

crystalfp avatar Jan 22 '20 04:01 crystalfp

Someone suggested to use .trustLocalhost() from superagent, but I don't know where to attach it.

crystalfp avatar Jan 22 '20 04:01 crystalfp

I'm using fastify and ran into the same problem when I turned on http2. I then turned it off but kept https configured and that worked. At this point I'm thinking that the issue is http2, not https itself.

The only difference from when I ran the tests only with http is that I used the environment variable NODE_EXTRA_CA_CERTS=... to point to the self-signed certificate I created for testing.

I also used the .ca call provided through superagent, and that worked after I turned off http2, but I would have had to add it to all tests. NODE_EXTRA_CA_CERTS= take care of the whole test suite in one fell swoop.

lddubeau avatar Feb 01 '20 00:02 lddubeau

Unfortunately, defining NODE_EXTRA_CA_CERTS before tests changes nothing. All tests fail with socket hang up as before.

crystalfp avatar Feb 03 '20 11:02 crystalfp

Dear @lddubeau, could you share your code? I'm lost on how to turn off http2 maintaining https. My real code is like the one I posted. Simply based on a variable useHTTP2 I run the code above or a code running http. If someone thinks it is relevant, I could post a minimal example. Thanks! mario

crystalfp avatar Feb 29 '20 17:02 crystalfp

Given the date of this issue, it should be supertest@<=5.0.0-0. Back then, http2 wasn't supported by supertest.


Nevertheless, this http2 feature has been incorporated into [email protected] (PR #793). A sample code snippet is in the README.md.

Based on your code, I would reckon it would be something like:

const request = require('supertest');

const options = {
  key:  fs.readFileSync(path.join(__dirname, "cert", "privkey.pem")),
  cert: fs.readFileSync(path.join(__dirname, "cert", "cert.pem"))
};
const app = function(req, res) {
  res.end('hey');
};
const server = http2.createSecureServer(options, app);
// either
request(server)       // pass in your http2 server
  .get('/')
  .http2()            // https://visionmedia.github.io/superagent/#using-http/2
  .disableTLSCerts(); // https://visionmedia.github.io/superagent/#tls-options

// or
request(server)       // pass in your http2 server
  .get('/')
  .http2()            // https://visionmedia.github.io/superagent/#using-http/2
  .trustLocalhost();  // https://visionmedia.github.io/superagent/#testing-on-localhost

lamweili avatar Oct 04 '22 07:10 lamweili

Closed via https://github.com/visionmedia/supertest/pull/793 and released in v6.3.0

titanism avatar Oct 04 '22 09:10 titanism