Created
July 10, 2017 15:02
-
-
Save mindscratch/61ed738c91e9b6ecc089c0dd91263c49 to your computer and use it in GitHub Desktop.
[golang] http handler test
This file contains hidden or 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 main | |
import ( | |
“net/http” | |
“net/http/httptest” | |
“testing” | |
) | |
fun TestHandler(t *testing.T) { | |
req, err := http.NewRequest( | |
http.MethodGet, | |
“http://localhost/foobar”, | |
nil, | |
) | |
if err != nil { | |
t.Fatalf(“could not create request: %v”, err) | |
} | |
// recorder satisfies the http response interface | |
rec := httptest.NewRecorder() | |
// handler is some http handler function we wrote that we want to test | |
handler(rec, req) | |
if rec.Code != http.StatusOK { | |
t.Errorf(“expected status 200; got %d”, rec.Code) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment