package main import ( "net/http" "net/http/httptest" "strconv" "testing" ) func Test_responseCodeForSource(t *testing.T) { for _, code := range []int{200, 404} { t.Run(strconv.Itoa(code), func(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(code) })) defer ts.Close() got, err := responseCodeForSource(ts.URL) if err != nil || got != code { t.Errorf("Wrong response code: Got %d, but expected %d", got, code) } }) } t.Run("Error", func(t *testing.T) { ts := httptest.NewUnstartedServer(nil) defer ts.Close() got, err := responseCodeForSource(ts.URL) if err == nil { t.Errorf("Error is nil") } if got != 0 { t.Errorf("Wrong response code: Got %d, but expected %d", got, 0) } }) }