httptest.NewRecorder

In line 12 we are creating a ResponseRecorder type: this is going to be our instance of ResponseWriter that we pass to the handler. Because a handler has no return function to validate correct operation, we need to check what has been written to the output. The ResponseRecorder type is an implementation of http.ResponseWriter which does just that: it records all the mutations we make so that it is later possible to make our assertions against it.

type ResponseRecorder struct {          Code      int           // the HTTP response code from WriteHeader          HeaderMap http.Header   // the HTTP response headers          Body      *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to          Flushed   bool          // contains filtered or unexported fields  } 

Get Building Microservices with Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.