Path: blob/main/extensions/github-authentication/src/test/node/fetch.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 * as http from 'http';7import * as net from 'net';8import { createFetch } from '../../node/fetch';9import { Log } from '../../common/logger';10import { AuthProviderType } from '../../github';111213suite('fetching', () => {14const logger = new Log(AuthProviderType.github);15let server: http.Server;16let port: number;1718setup(async () => {19await new Promise<void>((resolve) => {20server = http.createServer((req, res) => {21const reqUrl = new URL(req.url!, `http://${req.headers.host}`);22const expectAgent = reqUrl.searchParams.get('expectAgent');23const actualAgent = String(req.headers['user-agent']).toLowerCase();24if (expectAgent && !actualAgent.includes(expectAgent)) {25if (reqUrl.searchParams.get('error') === 'html') {26res.writeHead(200, {27'Content-Type': 'text/html',28'X-Client-User-Agent': actualAgent,29});30res.end('<html><body><h1>Bad Request</h1></body></html>');31return;32} else {33res.writeHead(400, {34'X-Client-User-Agent': actualAgent,35});36res.end('Bad Request');37return;38}39}40switch (reqUrl.pathname) {41case '/json': {42res.writeHead(200, {43'Content-Type': 'application/json',44'X-Client-User-Agent': actualAgent,45});46res.end(JSON.stringify({ message: 'Hello, world!' }));47break;48}49case '/text': {50res.writeHead(200, {51'Content-Type': 'text/plain',52'X-Client-User-Agent': actualAgent,53});54res.end('Hello, world!');55break;56}57default:58res.writeHead(404);59res.end('Not Found');60break;61}62}).listen(() => {63port = (server.address() as net.AddressInfo).port;64resolve();65});66});67});6869teardown(async () => {70await new Promise<unknown>((resolve) => {71server.close(resolve);72});73});7475test('should use Electron fetch', async () => {76const res = await createFetch()(`http://localhost:${port}/json`, {77logger,78retryFallbacks: true,79expectJSON: true,80});81const actualAgent = res.headers.get('x-client-user-agent') || 'None';82assert.ok(actualAgent.includes('electron'), actualAgent);83assert.strictEqual(res.status, 200);84assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });85});8687test('should use Electron fetch 2', async () => {88const res = await createFetch()(`http://localhost:${port}/text`, {89logger,90retryFallbacks: true,91expectJSON: false,92});93const actualAgent = res.headers.get('x-client-user-agent') || 'None';94assert.ok(actualAgent.includes('electron'), actualAgent);95assert.strictEqual(res.status, 200);96assert.deepStrictEqual(await res.text(), 'Hello, world!');97});9899test('should fall back to Node.js fetch', async () => {100const res = await createFetch()(`http://localhost:${port}/json?expectAgent=node`, {101logger,102retryFallbacks: true,103expectJSON: true,104});105const actualAgent = res.headers.get('x-client-user-agent') || 'None';106assert.strictEqual(actualAgent, 'node');107assert.strictEqual(res.status, 200);108assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });109});110111test('should fall back to Node.js fetch 2', async () => {112const res = await createFetch()(`http://localhost:${port}/json?expectAgent=node&error=html`, {113logger,114retryFallbacks: true,115expectJSON: true,116});117const actualAgent = res.headers.get('x-client-user-agent') || 'None';118assert.strictEqual(actualAgent, 'node');119assert.strictEqual(res.status, 200);120assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });121});122123test('should fall back to Node.js http/s', async () => {124const res = await createFetch()(`http://localhost:${port}/json?expectAgent=undefined`, {125logger,126retryFallbacks: true,127expectJSON: true,128});129const actualAgent = res.headers.get('x-client-user-agent') || 'None';130assert.strictEqual(actualAgent, 'undefined');131assert.strictEqual(res.status, 200);132assert.deepStrictEqual(await res.json(), { message: 'Hello, world!' });133});134135test('should fail with first error', async () => {136const res = await createFetch()(`http://localhost:${port}/text`, {137logger,138retryFallbacks: true,139expectJSON: true, // Expect JSON but server returns text140});141const actualAgent = res.headers.get('x-client-user-agent') || 'None';142assert.ok(actualAgent.includes('electron'), actualAgent);143assert.strictEqual(res.status, 200);144assert.deepStrictEqual(await res.text(), 'Hello, world!');145});146});147148149