First of all, an HTTP server could be anything implementing the `Handler` interface:
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
And you can start listening as simple as:
http.ListenAndServe("0.0.0.0:8000", my_server)
But how about the testing? Easy, you should start a test server:
ts := httptest.NewServer(my_server)
I have my test server. And now what? Call it like if it was any other http server:
req, _ := http.Get(WHAT_IS_THE_URL)
res, _ := http.DefaultClient.Do(req)
And now the difference: our test server has started a real server, the url is: ts.URL
Remember to close it after your tests are finished:
ts.Close()
All this things put together:
func Test_404_OK(t *testing.T) {
server := NewServer()
ts := httptest.NewServer(server)
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL+"/hello", strings.NewReader(""))
res, _ := http.DefaultClient.Do(req)
if 200 != res.StatusCode {
t.Error("Expecting status code is 200")
}
}