Path: blob/main/extensions/github-authentication/src/test/node/authServer.test.ts
3326 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import { LoopbackAuthServer } from '../../node/authServer';7import { env } from 'vscode';89suite('LoopbackAuthServer', () => {10let server: LoopbackAuthServer;11let port: number;1213setup(async () => {14server = new LoopbackAuthServer(__dirname, 'http://localhost:8080', 'https://code.visualstudio.com');15port = await server.start();16});1718teardown(async () => {19await server.stop();20});2122test('should redirect to starting redirect on /signin', async () => {23const response = await fetch(`http://localhost:${port}/signin?nonce=${server.nonce}`, {24redirect: 'manual'25});26// Redirect27assert.strictEqual(response.status, 302);2829// Check location30const location = response.headers.get('location');31assert.ok(location);32const locationUrl = new URL(location);33assert.strictEqual(locationUrl.origin, 'http://localhost:8080');3435// Check state36const state = locationUrl.searchParams.get('state');37assert.ok(state);38const stateLocation = new URL(state);39assert.strictEqual(stateLocation.origin, `http://127.0.0.1:${port}`);40assert.strictEqual(stateLocation.pathname, '/callback');41assert.strictEqual(stateLocation.searchParams.get('nonce'), server.nonce);42});4344test('should return 400 on /callback with missing parameters', async () => {45const response = await fetch(`http://localhost:${port}/callback`);46assert.strictEqual(response.status, 400);47});4849test('should resolve with code and state on /callback with valid parameters', async () => {50server.state = 'valid-state';51const response = await fetch(52`http://localhost:${port}/callback?code=valid-code&state=${server.state}&nonce=${server.nonce}`,53{ redirect: 'manual' }54);55assert.strictEqual(response.status, 302);56assert.strictEqual(response.headers.get('location'), `/?redirect_uri=https%3A%2F%2Fcode.visualstudio.com&app_name=${encodeURIComponent(env.appName)}`);57await Promise.race([58server.waitForOAuthResponse().then(result => {59assert.strictEqual(result.code, 'valid-code');60assert.strictEqual(result.state, server.state);61}),62new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))63]);64});65});666768