Got Error: read ECONNRESET when sending with supertest

36 Views Asked by At

I have an route where I upload files. before I upload I have two middlewares.

one for checking the jwt token, and the other is multer.

At first I check if the jwt token exists when not throw an error.

All works good but if I write an test with supertest and want to check if the jwt not exists I got this error:

  1 failing

  1) Try to Upload image without jwt
       should expect a 404 statuscode:
     Error: read ECONNRESET
      at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
      at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

But if I test it without jwt token with postman or in my web browser I got not this error. Only when sending with supertest.

describe('Try to Upload image without jwt', () => {
  it('should expect a 404 statuscode', done => {
    const filePath = path.resolve(__dirname, 'sample-asset', '../test.txt');
    supertest(app)
    .post('/api/upload/images')
    .set('content-type', 'application/octet-stream')
    .attach('image', filePath)
    .then(res => {
      expect(res.statusCode).to.equal(404);
      done();
    })
    .catch(err => {
      console.log(err);
      done(err);
    })
  });
});

All other tests are working as expected.

btw here is my route

app.post('/api/upload/images', [verifyJWT, upload.array('image', 10)], async (req: Request, res: Response) => {
  try {
 
    const apiResult = await cloudinary.uploader.upload(req.file.path, { folder: '/img' });
  

  } catch(e) {
    console.log(e);
    Sentry.captureException(e);
    return res.status(500).json({
      message: e
    });
  }
});

and here is my jwt token middleware

import * as Sentry from '@sentry/node';
import { NextFunction, Request, Response } from 'express';
import { supabase } from '../server.js';
import { ErrorException } from '../utils/HttpExceptions.js';

export const verifyJWT = async (req: Request, res: Response, next: NextFunction) => {
  try {
    // get the bearer token from the header
    const bearerHeader = req.headers['authorization'];

    // check if the bearer token exists
    if(!bearerHeader) {
      throw new ErrorException(404, 'Die Anfrage konnte wegen einem ungültigen oder fehlerhaften Token nicht verarbeitet werden. Wenden Sie sich an den Support.');
    }

    // Split the token where there is a empty space and then we get an array of all founded values
    const bearer = bearerHeader.split(' ');

    // the token is in the second value from the array so we get the second value from the array
    const token = bearer[1];
    
   // console.log(token);
    const user = await supabase.auth.getUser(token);

    if(!user) {
      throw new ErrorException(404, 'Es wurde kein Nutzer gefunden. Bitte wenden Sie sich an den Support.');
    }

    if(!user.data.user) {
      throw new ErrorException(404, 'Es wurde kein Nutzer gefunden. Bitte wenden Sie sich an den Support.');
    }

 
    next();
  } catch(e) {
    if(e instanceof ErrorException) {
      Sentry.captureException(e.message);
      console.log(e);
      return res.status(e.status).json({message: e.message});
    } else {
      return res.status(500).json({message: e});
    }
  }
};
0

There are 0 best solutions below