Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 of 6 tasks
danielszaniszlo opened this issue Jun 27, 2024 · 3 comments
Assignees
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library.

Comments

@danielszaniszlo
Copy link
Member

  • Package Name: @azure-rest/core-client
  • Package Version: ^2.0.0
  • Operating system: Windows
  • nodejs
    • version: v18.20.1
  • browser
    • name/version: Check the config in vitest.browser.shared.config.ts
  • typescript
    • version: ~5.4.5
  • Is the bug related to documentation in

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.

@github-actions github-actions bot added the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jun 27, 2024
@jeremymeng
Copy link
Member

@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?

@jeremymeng jeremymeng added Client This issue points to a problem in the data-plane of the library. Azure.Core labels Jun 28, 2024
@jeremymeng jeremymeng self-assigned this Jun 28, 2024
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jun 28, 2024
@danielszaniszlo
Copy link
Member Author

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
Copy link
Member

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library.
Projects
None yet
Development

No branches or pull requests

2 participants