Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

go - How to UT upload files

I'm working on making UT for my GraphQL API. I need to test a mutation where I upload a file. I am using gqlgen on this project.

...
localFile, err := os.Open("./file.xlsx")
if err != nil {
    fmt.Errorf(err.Error())
}

c.MustPost(queries.UPLOAD_CSV, &resp, client.Var("id", id), client.Var("file", localFile), client.AddHeader("Authorization", "Bearer "+hub.AccessToken))

c.MustPost panic and sends an error:

--- FAIL: TestUploadCSV (0.00s)
panic: [{"message":"map[string]interface {} is not an Upload","path":["uploadCSV","file"]}] [recovered]
panic: [{"message":"map[string]interface {} is not an Upload","path":["uploadCSV","file"]}]

How can I send the localFile to my API? I thought about making it through curl, but I'm not sure if it's a clean way to do so.

question from:https://stackoverflow.com/questions/65924693/how-to-ut-upload-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot just pass the os.File like that. You need to actually read the file, construct MIME multi-part request body (see spec) and send it in POST request.

buf := &bytes.Buffer{}
w := multipart.NewWriter(...)

// add other required fields (operations, map) here

// load file (you can do these directly I am emphasizing them 
// as variables so code below is more understandable
fileKey := "0" // file key in 'map'
fileName := "file.xslx" // file name
fileContentType := "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
fileContents, err := ioutil.ReadFile("./file.xlsx")
// ...

// make multipart body
h := make(textproto.MIMEHeader)

h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fileKey, fileName))
h.Set("Content-Type", fileContentType)
ff, err := bodyWriter.CreatePart(h)
// ...
_, err = ff.Write(fileContents)
// ...
err = bodyWriter.Close()
// ...

req, err := http.NewRequest("POST", fmt.Sprintf("https://endpoint"), buf)
//...

There is a good working example of doing this in gqlgen repository itself here: example/fileupload/fileupload_test.go.

In that example each file is loaded into (and represented by) file struct type defined on line I linked which might make it a bit confusing at first sight.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...