github.com/DamienFontaine/lunarc@v0.0.0-20190122154304-2e7332a51f55/web/handlers_test.go (about)

     1  // Copyright (c) - Damien Fontaine <damien.fontaine@lineolia.net>
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>
    15  
    16  package web
    17  
    18  import (
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/Sirupsen/logrus/hooks/test"
    24  )
    25  
    26  func TestSingleFileNormal(t *testing.T) {
    27  	request, _ := http.NewRequest("GET", "robot.txt", nil)
    28  
    29  	w := httptest.NewRecorder()
    30  	SingleFile("robot.txt").ServeHTTP(w, request)
    31  
    32  	if w.Code != http.StatusOK {
    33  		t.Fatalf("Non expected code: %v", w.Code)
    34  	}
    35  }
    36  
    37  func TestLoggingNormal(t *testing.T) {
    38  	request, _ := http.NewRequest("GET", "robot.txt", nil)
    39  
    40  	logger, hook := test.NewNullLogger()
    41  
    42  	next := SingleFile("robot.txt")
    43  
    44  	w := httptest.NewRecorder()
    45  	Logging(next, logger).ServeHTTP(w, request)
    46  
    47  	if len(hook.Entries) != 1 {
    48  		t.Fatalf("Must return 1 but : %v", len(hook.Entries))
    49  	}
    50  }
    51  
    52  func TestSingleFileNotFound(t *testing.T) {
    53  	request, _ := http.NewRequest("GET", "robot.txt", nil)
    54  
    55  	w := httptest.NewRecorder()
    56  	SingleFile("robots.txt").ServeHTTP(w, request)
    57  
    58  	if w.Code != http.StatusNotFound {
    59  		t.Fatalf("Non expected code: %v", w.Code)
    60  	}
    61  }