bovarys.me/fudge@v0.4.0/handler/handler_test.go (about)

     1  package handler
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"bovarys.me/fudge/config"
    11  )
    12  
    13  func init() {
    14  	err := os.Chdir("..")
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  }
    19  
    20  func TestRepositoryNotFound(t *testing.T) {
    21  	cfg := &config.Config{
    22  		RepoRoot: "../",
    23  		Debug:    true,
    24  	}
    25  
    26  	h, err := NewHandler(cfg)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	request, err := http.NewRequest("GET", "/notfound", nil)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	recorder := httptest.NewRecorder()
    37  
    38  	handler := http.HandlerFunc(h.showTree)
    39  	handler.ServeHTTP(recorder, request)
    40  
    41  	status := recorder.Code
    42  	if status != http.StatusNotFound {
    43  		t.Errorf("wrong status code: got %v want %v", status, http.StatusNotFound)
    44  	}
    45  
    46  	body := recorder.Body.String()
    47  	if !strings.Contains(body, "Page not found") {
    48  		t.Error("body does not contains 'Page not found'")
    49  	}
    50  }