Simple web server component (#9648)

* Simple web server component

* More testing
This commit is contained in:
Amir Omidi
2020-03-17 16:32:06 -07:00
committed by GitHub
parent bd8c4a44c8
commit cc6cc2889a
4 changed files with 182 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import 'mocha';
import axios from 'axios';
import { SimpleWebServer } from '../../../account-provider/utils/simpleWebServer';
let server: SimpleWebServer;
// These tests don't work on Linux systems because gnome-keyring doesn't like running on headless machines.
describe('AccountProvider.SimpleWebServer', function (): void {
beforeEach(async function (): Promise<void> {
server = new SimpleWebServer();
});
it('Starts up and returns a port', async function (): Promise<void> {
const port = await server.startup();
should(port).be.any.String().and.not.be.undefined().and.not.be.null();
});
it('Double startup should fail', async function (): Promise<void> {
await server.startup();
server.startup().should.be.rejected();
});
it('404 on unknown requests', async function (): Promise<void> {
const status = 404;
const server = new SimpleWebServer();
const port = await server.startup();
try {
const result = await axios.get(`http://localhost:${port}/hello`);
should(result).be.undefined();
} catch (ex) {
should(ex.response.status).equal(status);
}
});
it('Responds to GET requests', async function (): Promise<void> {
const msg = 'Hello World';
const status = 200;
const server = new SimpleWebServer();
const port = await server.startup();
server.on('/hello', (req, reqUrl, res) => {
res.writeHead(status);
res.write(msg);
res.end();
});
const response = await axios.get(`http://localhost:${port}/hello`);
should(response.status).equal(status);
should(response.data).equal(msg);
});
it('Server shuts off', async function (): Promise<void> {
await server.startup();
server.shutdown().should.not.be.rejected();
});
});