-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-server): add http/2 support based on spdy
- Loading branch information
1 parent
ba76ecf
commit 506cb9d
Showing
4 changed files
with
107 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,22 @@ | |
// Node module: @loopback/http-server | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
import {HttpServer, HttpOptions, HttpServerOptions} from '../../'; | ||
import { | ||
supertest, | ||
expect, | ||
itSkippedOnTravis, | ||
givenHttpServerConfig, | ||
httpGetAsync, | ||
httpsGetAsync, | ||
givenHttpServerConfig, | ||
itSkippedOnTravis, | ||
supertest, | ||
} from '@loopback/testlab'; | ||
import * as makeRequest from 'request-promise-native'; | ||
import {IncomingMessage, ServerResponse, Server} from 'http'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import {IncomingMessage, Server, ServerResponse} from 'http'; | ||
import {Agent} from 'https'; | ||
import * as path from 'path'; | ||
import * as makeRequest from 'request-promise-native'; | ||
import * as spdy from 'spdy'; | ||
import {HttpOptions, HttpServer, HttpServerOptions} from '../../'; | ||
import {Http2Options} from '../../src'; | ||
|
||
describe('HttpServer (integration)', () => { | ||
let server: HttpServer | undefined; | ||
|
@@ -156,29 +159,29 @@ describe('HttpServer (integration)', () => { | |
|
||
it('supports HTTPS protocol with key and certificate files', async () => { | ||
const serverOptions = givenHttpServerConfig(); | ||
const httpsServer: HttpServer = givenHttpsServer(serverOptions); | ||
await httpsServer.start(); | ||
const response = await httpsGetAsync(httpsServer.url); | ||
server = givenHttpsServer(serverOptions); | ||
await server.start(); | ||
const response = await httpsGetAsync(server.url); | ||
expect(response.statusCode).to.equal(200); | ||
}); | ||
|
||
it('supports HTTPS protocol with a pfx file', async () => { | ||
const serverOptions = givenHttpServerConfig({ | ||
usePfx: true, | ||
}); | ||
const httpsServer: HttpServer = givenHttpsServer(serverOptions); | ||
await httpsServer.start(); | ||
const response = await httpsGetAsync(httpsServer.url); | ||
server = givenHttpsServer(serverOptions); | ||
await server.start(); | ||
const response = await httpsGetAsync(server.url); | ||
expect(response.statusCode).to.equal(200); | ||
}); | ||
|
||
itSkippedOnTravis('handles IPv6 loopback address in HTTPS', async () => { | ||
const httpsServer: HttpServer = givenHttpsServer({ | ||
server = givenHttpsServer({ | ||
host: '::1', | ||
}); | ||
await httpsServer.start(); | ||
expect(httpsServer.address!.family).to.equal('IPv6'); | ||
const response = await httpsGetAsync(httpsServer.url); | ||
await server.start(); | ||
expect(server.address!.family).to.equal('IPv6'); | ||
const response = await httpsGetAsync(server.url); | ||
expect(response.statusCode).to.equal(200); | ||
}); | ||
|
||
|
@@ -196,6 +199,39 @@ describe('HttpServer (integration)', () => { | |
expect(server.url).to.equal(`http://127.0.0.1:${server.port}`); | ||
}); | ||
|
||
it('supports HTTP/2 protocol with key and certificate files', async () => { | ||
const serverOptions: Http2Options = Object.assign( | ||
{ | ||
protocol: 'http2' as 'http2', | ||
rejectUnauthorized: false, | ||
spdy: {protocols: ['h2' as 'h2']}, | ||
}, | ||
givenHttpServerConfig(), | ||
); | ||
server = givenHttp2Server(serverOptions); | ||
await server.start(); | ||
|
||
const agent = spdy.createAgent({ | ||
rejectUnauthorized: false, | ||
port: server.port, | ||
host: server.host, | ||
|
||
// Optional SPDY options | ||
spdy: { | ||
plain: false, | ||
ssl: true, | ||
}, | ||
}) as Agent; | ||
|
||
const response = await httpsGetAsync(server.url, agent); | ||
expect(response.statusCode).to.equal(200); | ||
|
||
// We need to close the agent so that server.close() returns | ||
// `@types/[email protected]` is not fully compatible with `[email protected]` | ||
// tslint:disable-next-line:no-any | ||
(agent as any).close(); | ||
}); | ||
|
||
function dummyRequestHandler( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
|
@@ -229,4 +265,15 @@ describe('HttpServer (integration)', () => { | |
} | ||
return new HttpServer(dummyRequestHandler, options); | ||
} | ||
|
||
function givenHttp2Server(options: Http2Options): HttpServer { | ||
const certDir = path.resolve(__dirname, '../../../fixtures'); | ||
|
||
const keyPath = path.join(certDir, 'key.pem'); | ||
const certPath = path.join(certDir, 'cert.pem'); | ||
options.key = fs.readFileSync(keyPath); | ||
options.cert = fs.readFileSync(certPath); | ||
|
||
return new HttpServer(dummyRequestHandler, options); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters