testify icon indicating copy to clipboard operation
testify copied to clipboard

Http Assertions does not allow the creation of a body

Open ihulsbus opened this issue 4 years ago • 23 comments

Hi,

It seems like the HTTP Assert package does not allow me to specify a body to pass in a POST request for example. Looking at the HTTPBody() function, the body argument is set to nil: req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)

Is this by design, or am I looking at a missing feature?

Cheers,

ihulsbus avatar Apr 16 '20 12:04 ihulsbus

We feel that it's reasonable to want to be able to set the body in certain cases. If you'd like to help out, feel free to open a PR.

mvdkleijn avatar Apr 24 '20 19:04 mvdkleijn

Hi @mvdkleijn , I just made changes for the issue, tried to push to a new branch and got 403, do I need some permissions to make PR?

gohargasparyan avatar Apr 29 '20 17:04 gohargasparyan

Hi @mvdkleijn , I just made changes for the issue, tried to push to a new branch and got 403, do I need some permissions to make PR?

This sounds like you're new or perhaps I'm misunderstanding? :smile:

The way you create a PR is by forking the repository to your own copy, create a branch with your changes pushed to it and then use the Github UI to create the PR. Basically that will be a request for us to merge changes from your fork into our original copy.

It sounds like you were trying to push to a branch in the main Testify repository which is indeed off limits to all but the maintainers.

Hope that helps!

mvdkleijn avatar Apr 29 '20 17:04 mvdkleijn

Yeah first time trying to contribute to open source, thanks, it helps :)

gohargasparyan avatar Apr 29 '20 17:04 gohargasparyan

Yeah first time trying to contribute to open source, thanks, it helps :)

No worries, feel free to ask more if desired. The Github docs also provide a lot of information on how to do things.

Once you have the PR in place, I'll take a look and see if anything needs adjustment and once satisfied all looks good, we can merge it. :+1:

mvdkleijn avatar Apr 29 '20 17:04 mvdkleijn

No worries, it was straight forward. https://github.com/stretchr/testify/pull/938

gohargasparyan avatar Apr 29 '20 17:04 gohargasparyan

@mvdkleijn hi i need help. very much help. i just joined the Github community yesterday and i don't understand a single thing I've been reading here 😩

fazypng avatar Jan 22 '21 16:01 fazypng

@mvdkleijn hi i need help. very much help. i just joined the Github community yesterday and i don't understand a single thing I've been reading here 😩

I'm not sure what exactly your question is that I could help with...?

mvdkleijn avatar Jan 22 '21 17:01 mvdkleijn

should this issue be closed?

lwlach avatar May 06 '23 00:05 lwlach

Not really, since the merged PR was reverted by @boyan-soubachov and aa such the issue is still valid.

mvdkleijn avatar May 06 '23 10:05 mvdkleijn

Hello @boyan-soubachov, if you could share the details, I'm gonna file a PR to integrate this functionality. Let me know, thanks.

ossan-dev avatar Jul 18 '23 16:07 ossan-dev

@ossan-dev See #938.

dolmen avatar Jul 29 '23 04:07 dolmen

Hey @dolmen do you mean this issue could be closed as soon as #938 will be merged? I apologize for the silly question but I'm a beginner of open-source contributions.

ossan-dev avatar Aug 07 '23 08:08 ossan-dev

Hey @dolmen do you mean this issue could be closed as soon as #938 will be merged? I apologize for the silly question but I'm a beginner of open-source contributions.

see the details in https://github.com/stretchr/testify/pull/938#issuecomment-660009610

hendrywiranto avatar Oct 16 '23 12:10 hendrywiranto

Is an issue still relevant? cc @arjunmahishi

myusko avatar Feb 22 '24 12:02 myusko

@myusko Looks like the linked PR's comments are still not addressed.

Is an issue still relevant?

Yes. The request raised in this issue is still not satisfied.


Based on my understanding of reading the comments on this issue and the PR, it looks like the PR was raised with v2 in mind. But that might not be happening. So, the next logical step for this would be to follow either suggestion 'b' or 'c' in this comment. 'c' being the best case scenario.

arjunmahishi avatar Feb 22 '24 17:02 arjunmahishi

Any ideas on how this could be done without introducing breaking changes?

I can't seem to find a solution that doesn't involve an extension of the surface of the library (the b scenario, in this comment)

st3penta avatar Feb 24 '24 11:02 st3penta

#1491 is related. What do the HTTP handler assertions offer over calling the handler with a httptest.ResponseRecorder?

brackendawson avatar Feb 24 '24 12:02 brackendawson

The only http assertions available right now are on the response status codes (HTTPSuccess, HTTPRedirect, HTTPStatusCode, etc) and body (HTTPBodyContains, HTTPBodyNotContains) (I hope this is what you asked, i'm not sure i understood your question!)

st3penta avatar Feb 24 '24 12:02 st3penta

I mean, why are the HTTP assertions better than this:

r := httptest.NewRequest(http.MethodGet, "http://example.com/biscuit", nil)
w := httptest.NewRecorder()
myHandler(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "custard cream", w.Body.String())

This only calls the handler once, so is a better test as it doesn't allow a stateful handler to sneak incorrect behaviour past the test. Like returning status code 500 with a correct body for every request after the first, for example.

brackendawson avatar Feb 24 '24 13:02 brackendawson

I agree that your example is much more effective. However those methods are part of the promise too, so what we're supposed to do with them?

st3penta avatar Feb 24 '24 14:02 st3penta

I agree that your example is much more effective. However those methods are part of the promise too, so what we're supposed to do with them?

Maintain the existing ones. 🙂

brackendawson avatar Feb 24 '24 14:02 brackendawson

I mean, why are the HTTP assertions better than this:

r := httptest.NewRequest(http.MethodGet, "http://example.com/biscuit", nil)
w := httptest.NewRecorder()
myHandler(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "custard cream", w.Body.String())

This only calls the handler once, so is a better test as it doesn't allow a stateful handler to sneak incorrect behaviour past the test. Like returning status code 500 with a correct body for every request after the first, for example.

Suggest to include this example in documentation and close this issue

KianYang-Lee avatar May 11 '24 01:05 KianYang-Lee