Get response from HttpResponseMessage in unit test
I've got this method in my controller, it queries data from database in this case it takes job with an id that's provided and sends HttpResponse message to the website containing some attachment(pdf, txt, etc):
[HttpGet]
[Route("jobs/{id}/attachment")]
public HttpResponseMessage GetAttachment([FromUri]Guid id)
{
if (job.Blob == null)
{
// The job exists, but has no result data
return this.CreateApiError(ApiError.ApiErrors.NO_CONTENT, "Job has no attachment", HttpStatusCode.NoContent);
}
var result = new HttpResponseMessage();
result.Content = new StreamContent(new System.IO.MemoryStream(job.Blob));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = job.Filename;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
It returns status 200
and works fine.
I'm writing unit test for it that looks like this:
I mock a job data, add it to database with JobQuery.Update(job);
and then try to get it's data:
[Test]
public void GetAttachmentTest()
{
var text = Encoding.ASCII.GetBytes("{"name":"John"}");
System.IO.File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"" + "Report -12.txt", text);
var job = new Job
{
Id = Guid.Parse("deadbeef-dead-beef-0000-000000000000"),
Filename = "Report -12.txt",
AudienceOrganizationId = organization.Id,
AudienceUserId = apiUser.Id,
Blob = text
};
HttpResponseMessage getAttachment;
try
{
getAttachment = this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
}
catch (Exception ex)
{
Assert.Fail("Get jobs/{id}/attachment failed");
return;
}
}
this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
goes to controller and looks for job with that id and takes attachment to return.
In controller it works fine, if I look at the response from website I get it as well, but I can't make it to get it here in my unit test.
And here on getAttachment
I get null. How should I change my unit test to get response?
The job is saved in database. It has an attachment file which I get with HttpResponse.
c# asp.net-core nunit httpresponse .net-framework-version
|
show 4 more comments
I've got this method in my controller, it queries data from database in this case it takes job with an id that's provided and sends HttpResponse message to the website containing some attachment(pdf, txt, etc):
[HttpGet]
[Route("jobs/{id}/attachment")]
public HttpResponseMessage GetAttachment([FromUri]Guid id)
{
if (job.Blob == null)
{
// The job exists, but has no result data
return this.CreateApiError(ApiError.ApiErrors.NO_CONTENT, "Job has no attachment", HttpStatusCode.NoContent);
}
var result = new HttpResponseMessage();
result.Content = new StreamContent(new System.IO.MemoryStream(job.Blob));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = job.Filename;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
It returns status 200
and works fine.
I'm writing unit test for it that looks like this:
I mock a job data, add it to database with JobQuery.Update(job);
and then try to get it's data:
[Test]
public void GetAttachmentTest()
{
var text = Encoding.ASCII.GetBytes("{"name":"John"}");
System.IO.File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"" + "Report -12.txt", text);
var job = new Job
{
Id = Guid.Parse("deadbeef-dead-beef-0000-000000000000"),
Filename = "Report -12.txt",
AudienceOrganizationId = organization.Id,
AudienceUserId = apiUser.Id,
Blob = text
};
HttpResponseMessage getAttachment;
try
{
getAttachment = this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
}
catch (Exception ex)
{
Assert.Fail("Get jobs/{id}/attachment failed");
return;
}
}
this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
goes to controller and looks for job with that id and takes attachment to return.
In controller it works fine, if I look at the response from website I get it as well, but I can't make it to get it here in my unit test.
And here on getAttachment
I get null. How should I change my unit test to get response?
The job is saved in database. It has an attachment file which I get with HttpResponse.
c# asp.net-core nunit httpresponse .net-framework-version
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
where doesjob
come from inGetAttachment
?
– Nkosi
Nov 22 '18 at 20:05
HttpResponseMessage
is no longer used as a controller response type in core
– Nkosi
Nov 22 '18 at 20:06
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08
|
show 4 more comments
I've got this method in my controller, it queries data from database in this case it takes job with an id that's provided and sends HttpResponse message to the website containing some attachment(pdf, txt, etc):
[HttpGet]
[Route("jobs/{id}/attachment")]
public HttpResponseMessage GetAttachment([FromUri]Guid id)
{
if (job.Blob == null)
{
// The job exists, but has no result data
return this.CreateApiError(ApiError.ApiErrors.NO_CONTENT, "Job has no attachment", HttpStatusCode.NoContent);
}
var result = new HttpResponseMessage();
result.Content = new StreamContent(new System.IO.MemoryStream(job.Blob));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = job.Filename;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
It returns status 200
and works fine.
I'm writing unit test for it that looks like this:
I mock a job data, add it to database with JobQuery.Update(job);
and then try to get it's data:
[Test]
public void GetAttachmentTest()
{
var text = Encoding.ASCII.GetBytes("{"name":"John"}");
System.IO.File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"" + "Report -12.txt", text);
var job = new Job
{
Id = Guid.Parse("deadbeef-dead-beef-0000-000000000000"),
Filename = "Report -12.txt",
AudienceOrganizationId = organization.Id,
AudienceUserId = apiUser.Id,
Blob = text
};
HttpResponseMessage getAttachment;
try
{
getAttachment = this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
}
catch (Exception ex)
{
Assert.Fail("Get jobs/{id}/attachment failed");
return;
}
}
this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
goes to controller and looks for job with that id and takes attachment to return.
In controller it works fine, if I look at the response from website I get it as well, but I can't make it to get it here in my unit test.
And here on getAttachment
I get null. How should I change my unit test to get response?
The job is saved in database. It has an attachment file which I get with HttpResponse.
c# asp.net-core nunit httpresponse .net-framework-version
I've got this method in my controller, it queries data from database in this case it takes job with an id that's provided and sends HttpResponse message to the website containing some attachment(pdf, txt, etc):
[HttpGet]
[Route("jobs/{id}/attachment")]
public HttpResponseMessage GetAttachment([FromUri]Guid id)
{
if (job.Blob == null)
{
// The job exists, but has no result data
return this.CreateApiError(ApiError.ApiErrors.NO_CONTENT, "Job has no attachment", HttpStatusCode.NoContent);
}
var result = new HttpResponseMessage();
result.Content = new StreamContent(new System.IO.MemoryStream(job.Blob));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = job.Filename;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return result;
}
It returns status 200
and works fine.
I'm writing unit test for it that looks like this:
I mock a job data, add it to database with JobQuery.Update(job);
and then try to get it's data:
[Test]
public void GetAttachmentTest()
{
var text = Encoding.ASCII.GetBytes("{"name":"John"}");
System.IO.File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"" + "Report -12.txt", text);
var job = new Job
{
Id = Guid.Parse("deadbeef-dead-beef-0000-000000000000"),
Filename = "Report -12.txt",
AudienceOrganizationId = organization.Id,
AudienceUserId = apiUser.Id,
Blob = text
};
HttpResponseMessage getAttachment;
try
{
getAttachment = this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
}
catch (Exception ex)
{
Assert.Fail("Get jobs/{id}/attachment failed");
return;
}
}
this.Get<HttpResponseMessage>(this.url + this.prefix + "jobs/" + job.id + "/attachment");
goes to controller and looks for job with that id and takes attachment to return.
In controller it works fine, if I look at the response from website I get it as well, but I can't make it to get it here in my unit test.
And here on getAttachment
I get null. How should I change my unit test to get response?
The job is saved in database. It has an attachment file which I get with HttpResponse.
c# asp.net-core nunit httpresponse .net-framework-version
c# asp.net-core nunit httpresponse .net-framework-version
edited Nov 22 '18 at 20:23
user122222
asked Nov 22 '18 at 20:00
user122222user122222
335115
335115
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
where doesjob
come from inGetAttachment
?
– Nkosi
Nov 22 '18 at 20:05
HttpResponseMessage
is no longer used as a controller response type in core
– Nkosi
Nov 22 '18 at 20:06
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08
|
show 4 more comments
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
where doesjob
come from inGetAttachment
?
– Nkosi
Nov 22 '18 at 20:05
HttpResponseMessage
is no longer used as a controller response type in core
– Nkosi
Nov 22 '18 at 20:06
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
where does
job
come from in GetAttachment
?– Nkosi
Nov 22 '18 at 20:05
where does
job
come from in GetAttachment
?– Nkosi
Nov 22 '18 at 20:05
HttpResponseMessage
is no longer used as a controller response type in core– Nkosi
Nov 22 '18 at 20:06
HttpResponseMessage
is no longer used as a controller response type in core– Nkosi
Nov 22 '18 at 20:06
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437403%2fget-response-from-httpresponsemessage-in-unit-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437403%2fget-response-from-httpresponsemessage-in-unit-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Confirm if this is asp.net-web-api-2.* or asp.net-core.
– Nkosi
Nov 22 '18 at 20:04
where does
job
come from inGetAttachment
?– Nkosi
Nov 22 '18 at 20:05
HttpResponseMessage
is no longer used as a controller response type in core– Nkosi
Nov 22 '18 at 20:06
Probably, but I'm only writing tests there, not really supposed to change code in controller. So I am just looking for workaround
– user122222
Nov 22 '18 at 20:07
finding a workaround for things that are no longer supported by a framework makes no sense.
– Nkosi
Nov 22 '18 at 20:08