Skip to content

POST Request Hangs on multipart/form-data Response #850

@blackjacques

Description

@blackjacques

Describe the bug

Node.js version: 22.11.0

OS version: Windows 10 Home

Description: I am testing an Express API that returns a multipart/form-data response using the form-data library. When invoking the post() method, it hangs until the test times out.

Actual behavior

The post method never resolves and the test times out.
The exact error is:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

● TCPSERVERWRAP

  195 |       });
  196 |       */
> 197 |       const result = await api.post('/')
      |                                ^
  198 |             .send()
  199 |             .set('Content-Type', 'multipart/form-data')
  200 |             .set('Accept', 'multipart/form-data');

  at Test.serverAddress (node_modules/supertest/lib/test.js:48:35)
  at new Test (node_modules/supertest/lib/test.js:34:14)
  at Object.obj.<computed> [as post] (node_modules/supertest/index.js:43:18)
  at Object.post (__tests__/test.js:197:32)

Expected behavior

It should return a multipart/form-data response that I can inspect.

Code to reproduce

// server code:
const app = express();
const FormData = require('form-data');

app.post("/", async (req, res) => {
  const form = new FormData();
  form.append('errors', 'there was an error');
  res.writeHead(200, {
    'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`, // boundary must match the one generated by form-data
    'Content-Length': form.getLengthSync(),
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Allow-Origin': '*', // Update based on your needs (for demo purposes, '*' is more flexible)
  });

  // Write the multipart form data response.
  res.write(form.getBuffer());
  res.end();
});

// in test.js:
it('should return errors about missing params', async () => {
  const result = await api.post('/')
            .send()
            .set('Content-Type', 'multipart/form-data')
            .set('Accept', 'multipart/form-data');
  expect(result).not.toBeNull();
  expect(result.body).not.toBeNull();
});

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions