From 54d67cbbaaf9814c32f345d8e484cd3f78291d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Tue, 28 Mar 2017 09:17:02 -0700 Subject: [PATCH] Add more tests for views We moved static file handling to binary assets, add some tests so we can spot issues with this code --- views_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/views_test.go b/views_test.go index 2983f8312..5023176f8 100644 --- a/views_test.go +++ b/views_test.go @@ -29,6 +29,7 @@ func mockConfig() { func ginTestEngine() *gin.Engine { gin.SetMode(gin.TestMode) r := gin.New() + r.SetHTMLTemplate(loadTemplates("templates")) setupRouter(r) return r } @@ -256,6 +257,14 @@ var acTests = []acTestCase{ "node=localhost", }, }, + // duplicated to test reponse caching + acTestCase{ + Term: "Nod", + Results: []string{ + "node!=localhost", + "node=localhost", + }, + }, } func TestAutocomplete(t *testing.T) { @@ -294,3 +303,45 @@ func TestAutocomplete(t *testing.T) { } } } + +type staticFileTestCase struct { + path string + code int +} + +var staticFileTests = []staticFileTestCase{ + staticFileTestCase{ + path: "/favicon.ico", + code: 200, + }, + staticFileTestCase{ + path: "/static/unsee.js", + code: 200, + }, + staticFileTestCase{ + path: "/static/managed/js/assets.txt", + code: 200, + }, + staticFileTestCase{ + path: "/xxx", + code: 404, + }, + staticFileTestCase{ + path: "/static/abcd", + code: 404, + }, +} + +func TestStaticFiles(t *testing.T) { + mockConfig() + mockAlerts() + r := ginTestEngine() + for _, staticFileTest := range staticFileTests { + req, _ := http.NewRequest("GET", staticFileTest.path, nil) + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + if resp.Code != staticFileTest.code { + t.Errorf("Invalid status code for GET %s: %d", staticFileTest.path, resp.Code) + } + } +}