How to test a http function which takes folder as an input?

Refresh

April 2019

Views

6 time

0

I have a http handler function(POST) which allows a user to upload a folder from a web browser application. The folder is passed from javascript code as array of files in folder and on backend (golang API) it is accepted as []*multipart.FileHeader. I am struggling in writing a go unit test for this function. How to pass folder as input from a test function? Need help in creating the httpRequest in the right format.

Have tried to use / set values for an array of FileHeader but some attributes are not allowed to be imported. So there must be a different way of testing this handler that I am not aware of..

Handler Function for folder upload:

func FolderUpload(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 
// SOME LOGIC

files := r.MultipartForm.File["multiplefiles"] // files is of the type []*multipart.FileHeader

// SOME LOGIC TO PARSE THE FILE NAMES TO RECREATE THE SAME TREE STRUCTURE ON THE SERVER-SIDE AND STORE THEM AS A FOLDER

Unit Test function for the same handler:

func TestFolderUpload(t *testing.T) {
//     FolderPreCondition()
     request, err := http.NewRequest("POST", uri, body) //Q: HOW TO CREATE THE BODY ACCEPTABLE BY THE ABOVE HANDLER FUNC?
//     SOME ASSERTION LOGIC
}

0 answers