4 months ago
27 lines535 B
package hello | |
| |
import ( | |
"fmt" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
| |
func hello(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello, %s!\n", r.URL.Query().Get("name")) | |
} | |
| |
func TestHello(t *testing.T) { | |
req := httptest.NewRequest("GET", "/hello?name=Go", nil) | |
rec := httptest.NewRecorder() | |
hello(rec, req) | |
| |
if rec.Code != http.StatusOK { | |
t.Fatalf("status = %d", rec.Code) | |
} | |
if got := rec.Body.String(); got != "Hello, Go!\n" { | |
t.Errorf("body = %q", got) | |
} | |
} | |
| |
// This is the version we use in the video. |