gofight icon indicating copy to clipboard operation
gofight copied to clipboard

go fight.HTTPResponse masking ResponseRecorder methods?

Open apsdsm opened this issue 6 years ago • 2 comments

I might be missing something totally obvious here. If so I apologize in advance.

It seems like by aliasing *httptest.ResponseRecorder with HTTPResponse, you can no longer access the methods on the ResponseRecorder object. This means to access headers you must use the deprecated HeaderMap object, rather than calling the Header() method as is suggested.

Is there a way to call the method on the type aliased object?

It doesn't look like there's a huge amount of gain from aliasing the object outside saving a few keystrokes, so would it be possible to just use the actual object instead of the aliased one? I'd be happy to put a pull request together, though this would change the public interface of the project..

Any guidance in this matter would be appreciated!!

Thank you for making this library btw, it's made my life much nicer!

apsdsm avatar Mar 12 '19 03:03 apsdsm

I am running into the same problem right now. How were you able to fix or work around this? I do not wish to use the deprecated HeaderMap() method neither.

tolgaatam avatar Dec 30 '21 21:12 tolgaatam

I found one solution to this problem. As gofight.HTTPResponse type is an alias for httptest.Response , it is possible to do a conversion. Then you can use the methods of httptest.Response freely. Here is an example code:

g := gofight.New()
g.POST("/api/signup").
	SetJSON(gofight.D{
		"email":          "[email protected]",
		"password":      "1234",
	}).
	Run(engine, func(response gofight.HTTPResponse, request gofight.HTTPRequest) {
		assert.Equal(t, http.StatusOK, response.Code)
		r := (*httptest.ResponseRecorder) (response)
		someHeader := r.Header.Get("Some-Header")
		
		...
		...
	})

tolgaatam avatar Dec 31 '21 10:12 tolgaatam