July 2017
Beginner to intermediate
358 pages
10h 54m
English
The first line we need to pay attention to is line 11: the net/http/httptest package has some nice convenience methods for us. The NewRequest method returns an incoming server request which we can then pass to our http.Handler:
func NewRequest(method, target string, body io.Reader) *http.Request
We can pass parameters to the method and the target, which is either the path or an absolute URL. If we only pass a path, then example.com will be used as our host setting. Finally, we can give it an io.Reader file which will correspond to the body of the request; if we do not pass a nil value then Request.ContentLength is set.
Read now
Unlock full access