35 lines
742 B
Go
35 lines
742 B
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestIndexHandler(t *testing.T) {
|
|
// TestIndexHandler tests the index
|
|
req, err := http.NewRequest("GET", "/", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
handler := http.HandlerFunc(index)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("Index returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
indexTemplate, err := ioutil.ReadFile(filepath.Join("templates", "index.html"))
|
|
if err != nil {
|
|
t.Fatal("Can't read index.html")
|
|
}
|
|
|
|
if rr.Body.String() != string(indexTemplate) {
|
|
t.Fatal("Index response is not the same as index.html")
|
|
}
|
|
}
|