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

Using the MockAgent and the pipeline method #2791

Open
Dafyh opened this issue Feb 20, 2024 · 0 comments
Open

Using the MockAgent and the pipeline method #2791

Dafyh opened this issue Feb 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Dafyh
Copy link

Dafyh commented Feb 20, 2024

Bug Description

Pipeline mock fails when node:stream/promise pipeline is used.

Reproducible By

The first test passes. For the second test, the await pipeline(...) fails. The error is not catched.

// Import Node.js Dependencies
import { describe, it, afterEach } from "node:test";
import { pipeline } from "node:stream/promises";
import stream, { PassThrough, Readable } from "node:stream";
import assert from "node:assert";
import timers from "node:timers/promises";

// Import Third-Party Dependencies
import undici, { MockAgent } from "undici";

describe("undici.pipeline", async() => {
  let kMockAgent: MockAgent;

  afterEach(async() => {
    await kMockAgent.close();
  });

  it("OK : stream.pipeline", async() => {
    kMockAgent = new MockAgent();
    kMockAgent.disableNetConnect();
    undici.setGlobalDispatcher(kMockAgent);

    kMockAgent
      .get("https://boz.com")
      .intercept({ method: "POST", path: "/" })
      .reply(200, "Bar");

    const readable = Readable.from("Some text.".repeat(10_000));
    const request = undici.pipeline(
      "https://boz.com",
      { method: "POST" },
      ({ body }) => body
    );

    stream.pipeline(readable, request, new PassThrough(), () => void 0);
    await timers.setTimeout(100);

    assert.ok(1);
  });

  it("KO : stream/promises.pipeline", async() => {
    kMockAgent = new MockAgent();
    kMockAgent.disableNetConnect();
    undici.setGlobalDispatcher(kMockAgent);

    kMockAgent
      .get("https://boz.com")
      .intercept({ method: "POST", path: "/" })
      .reply(200, "Bar");

    const readable = Readable.from("Some text.".repeat(10_000));
    const request = undici.pipeline(
      "https://boz.com",
      { method: "POST" },
      ({ body }) => body
    );

    try {
      await pipeline(readable, request, new PassThrough());
    }
    catch (error) {
      console.error(error);
    }

    assert.ok(1);
  });
});

Expected Behavior

stream/promises.pipeline should work in the same way as stream/pipeline ?

Logs & Screenshots

image

Environment

Node 20.x
Undici 6.6.2

@Dafyh Dafyh added the bug Something isn't working label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant