These are chat archives for gin-gonic/gin

26th
Mar 2016
Thomas Boerger
@tboerger
Mar 26 2016 13:14
How can I really debug failed bindings? I have not figured out so far how I can get the provided json payload of the request :(
Andrey Nering
@andreynering
Mar 26 2016 13:50
I don't know, but have noticed that it don't work for time.Time type.
Thomas Boerger
@tboerger
Mar 26 2016 14:00
https://golang.org/pkg/net/http/httputil/#DumpRequest prints the headers, but i need the body...
Andrey Nering
@andreynering
Mar 26 2016 14:06
Try c.Request.Body()
Thomas Boerger
@tboerger
Mar 26 2016 14:47
c.Request.Body() is not a string
Andrey Nering
@andreynering
Mar 26 2016 16:17
Hmm... But I think there is a way to get the string. Maybe c.Request.Body.String() or c.Request.Body().String()
Ousmane Traore
@otraore
Mar 26 2016 19:12
@tboerger Testing gin controllers isn't hard, but you're right finding examples is a bit difficult Here's one to get you started: http://play.golang.org/p/qyhD0mlCST, I was actually frustrated at first but once you get up and running, tests become a breeze.
Ousmane Traore
@otraore
Mar 26 2016 19:17
I personally use github.com/smartystreets/goconvey/convey so my tests look something like this:
func TestUsersResource(t *testing.T) {
    r := controllers.Router() //Insert your router here
    Convey("Empty POST request to /users should return Bad Request", t, func() {
        req, _ := http.NewRequest("POST", "/users", nil)
        resp := httptest.NewRecorder()
        r.ServeHTTP(resp, req)
        So(resp.Code, ShouldEqual, http.StatusBadRequest)
    })
    ...
}
Thomas Boerger
@tboerger
Mar 26 2016 19:52
@otraore thanks for the example
Ousmane Traore
@otraore
Mar 26 2016 19:52
@tboerger My pleasure, if you have any other questions just shoot.
Thomas Boerger
@tboerger
Mar 26 2016 19:57
For the future it should be interesting how I can see the body of a request if the binding fails the next time ;)
Ousmane Traore
@otraore
Mar 26 2016 20:38
If the binding fails c.Bind... should return an error, just check if that's not nil.
Or am I not understanding what you mean
Thomas Boerger
@tboerger
Mar 26 2016 21:19
For debugging why it fails in a case of json requests I want to see the body because the error is not self explaining