Test::Mojo with uploading files

34 views
Skip to first unread message

yanni1...@g.ucla.edu

unread,
Jan 2, 2018, 10:09:49 PM1/2/18
to Mojolicious
Hello all,

How do I ask the test object to upload files?
Here's the source code for dragging files to the drop zone:
 <form action="/uploadsave" method="post" class="dropzone" id="dropzoneform" enctype="multipart/form-data">
  </form>
<script type="text/javascript"> Dropzone.options.dropzoneform = { init: function() { uploadMultiple: true, this.on("queuecomplete", function() { console.log("queue completed."); window.location.reload(true); }); this.on("success", function(file, response) { console.log(file.name + " files successfully uploaded."); }); this.on("error", function(file, errorMessage) { console.log(errorMessage); }); } }; </script>

And the source code for selecting a file to submit:

<form action="/uploadsave" id="uploadform" method="post" enctype="multipart/form-data" style="display:block"> <label for="idupload">Upload A New File: </label> <input type="file" name="file" id="idupload" style="display:inline" > </form> <script> document.getElementById("idupload").onchange = function() { document.getElementById("uploadform").submit(); } </script>

Thanks!

Ni

Daniel Mantovani

unread,
Jan 4, 2018, 4:53:29 PM1/4/18
to Mojolicious
Hi,

You can find an example of file upload testing here: https://metacpan.org/pod/Test::Mojo#post_ok

I haven't tested what follows, but in your case (using "file" as the name of the file input field and "/uploadsave" as the url to post) that should be:

my $upload = {file => {file => '/path/to/the/file/to/upload'}};
$t->post_ok('/uploadsave' => form => $upload)->status_is(200);

(the first "file" corresponds to the name of the input field, and that will be the name of the parameter at your controller. The second "file" tells the user agent that it should upload the file that follows);

Hope it helps.
BR,
Reply all
Reply to author
Forward
0 new messages