added test for index

This commit is contained in:
BroodjeAap 2020-06-29 19:04:24 +00:00
parent 2cacfcbd56
commit 56716913b1
2 changed files with 49 additions and 0 deletions

15
.drone.yml Executable file
View file

@ -0,0 +1,15 @@
kind: pipeline
type: docker
name: default
steps:
- name: test
image: golang:1.14.4
commands:
- go build
- go test
trigger:
branch:
- master
event:
- push

34
main_test.go Executable file
View file

@ -0,0 +1,34 @@
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"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("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")
}
}