Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:08:22+00:00 2026-06-04T02:08:22+00:00

I have an app built on Express.js and I’d like to test the file

  • 0

I have an app built on Express.js and I’d like to test the file upload functionality. I’m trying to reproduce the object parsed to req.files (when using express.bodyParser middleware). How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:08:23+00:00Added an answer on June 4, 2026 at 2:08 am

    You can do this directly in Mocha but it’s a bit tricky. Here’s an example posting an image:

    var filename = 'x.png'
      , boundary = Math.random()
    
    request(app)
      .post('/g/' + myDraftGallery._id)
      .set('Content-Type', 'multipart/form-data; boundary=' + boundary)
      .write('--' + boundary + '\r\n')
      .write('Content-Disposition: form-data; name="image"; filename="'+filename+'"\r\n')
      .write('Content-Type: image/png\r\n')
      .write('\r\n')
      .write(fs.readFileSync('test/'+filename))
      .write('\r\n--' + boundary + '--')
      .end(function(res){
        res.should.have.status(200)
        done()
      })
    

    The name parameter of Content-Disposition is what your file will be accessible as via req.files (so req.files.image for my example)
    You can also use an array value like this: name=”images[]” and your file(s) will be available via an array, e.g: req.files.images[0]

    Also if you’re not already using it you should have a look at this (makes mocha/express testing a ~bit~ easier):
    https://github.com/visionmedia/express/blob/master/test/support/http.js

    Edit: Since express 3-beta5, express uses supertest. To look at the old http.js code look here: https://github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js
    Or simply move over to supertest..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app built in CI and I would like that sessions don't
I have a very simple node.js app built on express which has been handling
I have an app built on Cordova and on some of my pages I
I have an app built in ASP.NET/C# . I have created a WebSetUp for
I have a web app built with Dojo 1.7.2, using RequireJS to load individual
I have a web app built on Java Servlet technology. I am thinking of
We have a J2EE app built on Struts2+spring+iBatis; not all DAO's use iBatis...some code
I have a console app built on .NET 4 that uses the HttpClient library
We have an Android app built with Mono for Android, and now we have
I have a WinForms app built with VB.Net in VS 2010, and I'm scratching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.