Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
1.98k stars 1.15k forks source link

API requests in tests to https://localhost doesn't work with test:node but it works with test:browser #30215

Open danielszaniszlo opened 3 days ago

danielszaniszlo commented 3 days ago

Describe the bug

I have a local setup where our service runs on https://localhost/. I have written test cases that make REST API calls to this service. These tests fail when executed in a Node.js environment, but they pass when run in a browser environment. If I use a real endpoint not localhost both node and browser test cases are passing.

Client configuration:

const endpoint = "localhost";
const client = await createClient(endpoint, credentials, recorder.configureClientOptions({}));

API call:

const response = await client.path("/deid").post({ body: content });
console.log(response)

The error I get if I use dev-tool run test:vitest

[vitest] RestError: connect ECONNREFUSED ::1:443
[vitest]  ❯ ClientRequest.<anonymous> ../../core/core-rest-pipeline/src/nodeHttpClient.ts:223:11
[vitest]  ❯ Object.onceWrapper node:events:632:26
[vitest]  ❯ ClientRequest.emit node:events:517:28
[vitest]  ❯ TLSSocket.socketErrorListener node:_http_client:501:9
[vitest]  ❯ TLSSocket.emit node:events:517:28

The same test with the same setup passes if I use dev-tool run test:vitest --browser

To Reproduce Steps to reproduce the behavior:

  1. Start your service locally using HTTPS and hit some endpoints from a test case.
  2. Execute the test case using the command dev-tool run test:vitest.

Expected behavior The test cases should have the same behavior on both node and browser environments

Screenshots N/A

Additional context Add any other context about the problem here.

jeremymeng commented 2 days ago

@danielszaniszlo from internet search I see sometimes NodeJs has problem accessing localhost due to DNS query use IPv6 first. Does 127.0.0.1 work?

danielszaniszlo commented 2 days ago

127.0.0.1

The behavior is strange. If I change the address to 127.0.0.1 Node returns a different error DEPTH_ZERO_SELF_SIGNED_CERT:

[vitest] RestError: self-signed certificate
[vitest]  ❯ ClientRequest.<anonymous> ../../core/core-rest-pipeline/src/nodeHttpClient.ts:223:11
[vitest]  ❯ Object.onceWrapper node:events:632:26
[vitest]  ❯ ClientRequest.emit node:events:517:28
[vitest]  ❯ TLSSocket.socketErrorListener node:_http_client:501:9
[vitest]  ❯ TLSSocket.emit node:events:517:28

Browser also returns an error REQUEST_SEND_ERROR:

[vitest] RestError: Error sending request: Failed to fetch
[vitest]  ❯ getError ../../core/core-rest-pipeline/dist/browser/fetchHttpClient.js:192:11
[vitest]     191|  * however they are not yet supported by all browsers i.e Firefox
[vitest]     192|  */
[vitest]     193| function buildBodyStream(readableStream, options = {}) {
[vitest]        |  ^
[vitest]     194|     let loadedBytes = 0;
[vitest]     195|     const { onProgress, onEnd } = options;
[vitest]  ❯ FetchHttpClient.sendRequest ../../core/core-rest-pipeline/dist/browser/fetchHttpClient.js:48:12
[vitest]  ❯ Object.sendRequest ../../core/core-rest-pipeline/dist/browser/policies/retryPolicy.js:57:21
[vitest]  ❯ Object.sendRequest ../../core/core-rest-pipeline/dist/browser/policies/bearerTokenAuthenticationPolicy.js:189:19
[vitest]  ❯ sendRequest ../../core/core-client-rest/dist/browser/sendRequest.js:39:19

If I use localhost browser works fine but node still fails.

jeremymeng commented 2 days ago

The nodejs error may be due to missing certificate. Would setting NODE_TLS_REJECT_UNAUTHORIZED env var to 0 helps?

and for "localhost" some post suggests to call dns.setDefaultResultOrder('ipv4first')