asp.net web api - C# Test File Upload -
i have browsed through various posts , tried several things haven't been able things working right. dont feel should hard, here's got. want post file server testing purposes. should work if uploading standard <input type="file"/>
on web page.
this actual handler want test:
[route("myaction")] [httppost] public ihttpactionresult myaction() { try { var request = httpcontext.current.request; if (request.files.count == 1) { var postedfile = request.files[0]; if (request.files[0].filename.endswith(".zip")) { // unzip file // stuff } } } catch { return new validationerror("an error has occurred , has been logged"); } }
this test:
public void setupserver() { server = testserver.create(app => { var startup = new startup(); startup.configureoauth(app); var config = new httpconfiguration(); webapiconfig.register(config); app.usewebapi(config); }); } [test] public async task myactiontest() { setupserver(); uri = "/api/myclass/myroute"; var file = properties.resources.myzipfile; var boundary = "------------------------" + datetime.now.ticks; var multipartfilecontent = new multipartformdatacontent(boundary); var content = new bytearraycontent(file); content.headers.add("content-type", "application/octet-stream"); multipartfilecontent.add(content, "myfilename", "myzipfile.zip"); var serverresp = (await server.createrequest(uri) .and(req => req.content = multipartfilecontent) .postasync()); }
running test causes error on var request = httpcontext.current.request;
because httpcontext.current
null reason. going in wrong or hard way? feel shouldn't difficult.
the issue httpcontext.current
isn't available in self-hosted test you're spinning up.
take here:
Comments
Post a Comment