forwardemail / forwardemail/supertest

file upload testcase with graphql issue.

Open
#848 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

## Bug Report

**Node.js version:** v18.20.4
**OS version:** macOS Monterey 12.7.5

### Description

The following error occurs when attempting to make a `POST` request to the `/graphql` endpoint using Supertest:

```js
Error: cannot POST /graphql (500)
at Response.toError (/src/node_modules/superagent/src/node/response.js:110:17)
at Response._setStatusProperties (/src/node_modules/superagent/src/response-base.js:107:48)
at new Response (/src/node_modules/superagent/src/node/response.js:41:8)
at Test._emitResponse (/src/node_modules/superagent/src/node/index.js:953:20)
at IncomingMessage. (/src/node_modules/superagent/src/node/index.js:1166:38)
at IncomingMessage.emit (node:events:525:35)
at IncomingMessage.emit (node:domain:489:12)
at endReadableNT (node:internal/streams/readable:1358:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
status: 500,
text: 'POST body missing. Did you forget use body-parser middleware?',
method: 'POST',
path: '/graphql'
}
```

### Actual Behavior

A `500` error is returned with the message:
```
POST body missing. Did you forget use body-parser middleware?
```
FYI: The API is working fine with appolo graphql client and postman.

### Expected Behavior

The mutation should execute successfully with the attached file being processed by the `/graphql` endpoint.

### Steps to Reproduce

The error can be reproduced using the following test code:
```javascript
export class IntegrationTestManager {
public httpServer: any;

private app: INestApplication;

async beforeAll(): Promise {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

this.app = moduleRef.createNestApplication();
await this.app.init();
this.httpServer = this.app.getHttpServer();

}

async afterAll() {
await this.app.close();
}

executeRESTRequest({ path, requestType }: { path: string, requestType: string }): Promise {

const restRequest = request(this.httpServer)[requestType](path);
return restRequest.send();
}

executeGraphQLRequest({ query, variables, authToken }: { query: string, variables?: Object, authToken?: string }): Promise {
const graphqlRequest = request(this.httpServer).post('/graphql');

if (authToken) {
graphqlRequest.set('Authorization', authToken);
}

return graphqlRequest.send({
query,
variables,
});
}

async getServerInstance() {
return this.httpServer
}
}
```

```javascript
const filePath = path.join(__dirname, '1.png');
const serverInstance = await integrationTestManager.getServerInstance()

const graphqlRequest = request(serverInstance).post('/graphql')
.field('operations', jsonRequestBodyString)
.set('Content-Type', 'multipart/form-data')
.field("map", JSON.stringify({
"0": ["variables.picUpload"],
}))
.attach("0", filePath)
.set('Authorization', testingContext.users[user].token);

const testResponse = await graphqlRequest;
```

### Notes for Maintainers

- The issue seems related to missing or misconfigured middleware for processing `multipart/form-data` requests, such as `body-parser` or an equivalent package.
- Ensure that the GraphQL server is configured to support file uploads and the appropriate middleware is applied before handling requests.

### Request for Prioritization

This issue impacts the ability to test and upload files via the GraphQL API. A prompt resolution would be appreciated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.