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

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

Open
3 tasks done
blackjacques opened this issue Jan 18, 2025 · 0 comments
Open
3 tasks done

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

blackjacques opened this issue Jan 18, 2025 · 0 comments
Labels

Comments

@blackjacques
Copy link

blackjacques commented Jan 18, 2025

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant