Last active
March 9, 2021 23:18
-
-
Save podhmo/6a84b56712e2f3575a1a842f02062be2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package expecterror | |
import ( | |
"errors" | |
"io" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
func TestCloseConnection(t *testing.T) { | |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
c, _, err := w.(http.Hijacker).Hijack() | |
if err != nil { | |
t.Fatal(err) | |
} | |
t.Log("for test -- close connection") | |
c.Close() | |
})) | |
defer ts.Close() | |
_, err := http.Get(ts.URL) | |
if err == nil { | |
t.Errorf("error is expected but nil") | |
} | |
if !errors.Is(err, io.EOF) { | |
t.Errorf("EOF is expected but return error is %[1]T, %+[1]v", err) | |
} | |
} | |
// // panic: interface conversion: *httptest.ResponseRecorder is not http.Hijacker: missing method Hijack [recovered] | |
// // panic: interface conversion: *httptest.ResponseRecorder is not http.Hijacker: missing method Hijack | |
// func TestErrorWithRecorder(t *testing.T) { | |
// rec := httptest.NewRecorder() | |
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// c, _, err := w.(http.Hijacker).Hijack() | |
// if err != nil { | |
// t.Fatal(err) | |
// } | |
// t.Log("for test -- close connection") | |
// c.Close() | |
// }) | |
// handler(rec, httptest.NewRequest("GET", "http://example.net", nil)) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment