I'm trying to write a test for a handler which receives a file. As part of such I'm trying to configure my context so the handler can use it.
My intention is to create a file and use multipart.FileHeader to open it.
f, err := os.CreateTemp("", "upload-test")
require.NoError(t, err)
_, err = f.Write([]byte("1234"))
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
fileHeader := &multipart.FileHeader{
Filename: f.Name(),
Size: 4,
}
open, err := fileHeader.Open()
require.NoError(t, err)
However the Open
method returns: "open : no such file or directory"
UPDATE:
My endpoint receives the below struct
type UploadDatabaseRequest struct {
Group string `form:"group" binding:"required"`
Database *multipart.FileHeader `form:"database" binding:"required"`
}
Based on Ivan 's answer I tried to read the request using multipart.NewReader
so I could get a *multipart.FileHeader
. However the below code results in "unexpected EOF" from the call to multipartReader.ReadForm(100000000)
.
contentType := multipartWriter.FormDataContentType()
_, params, err := mime.ParseMediaType(contentType)
assert.NoError(t, err)
multipartReader := multipart.NewReader(bytes.NewReader(buf.Bytes()), params["boundary"])
form, err := multipartReader.ReadForm(100000000)
require.NoError(t, err)
fileHeader := form.File["file"][0]
uploadRequest := &UploadDatabaseRequest{
Group: groupName,
Database: fileHeader,
}
I created an example to demonstrate how it should work. First, let me present the code, then, I'll walk you through all of the relevant parts.
Upload
The upload is done in the
handlers_test.go
file. I wrote two tests to show off how to create a valid HTTP Request with a multipart body (I assumed that the communication was based on HTTP). Here you can find the code:We can focus on the subtest
MultipartRequest
. First, it instantiates a multipart body which will be used later as the request payload of the HTTP request we're going to send. Then, we create a file part and write dummy content to it. Before sending out the request, we've to see theContent-Type
header that will be used for parsing stuff. The rest of the test should be pretty straightforward.Read
The read (or parsing) is done by the HTTP server. The file involved is the
handlers.go
file:Here, the relevant steps can be summarized in the following list:
Content-Type
header from the HTTP Requestmultipart/
BadRequest
error to the HTTP clientIn the code I put some comments to explain some delicate sections that deserve attention. Furthermore, I simplified the codebase by not handling any error that might happen.
Hope to help in solving your issue, let me know!