Merge pull request #1489 from prymitive/asset-tests

fix(tests): add more asset tests
This commit is contained in:
Łukasz Mierzwa
2020-03-05 20:07:06 +00:00
committed by GitHub
5 changed files with 135 additions and 84 deletions
+129
View File
@@ -3,8 +3,10 @@ package main
import (
"html/template"
"net/http/httptest"
"os"
"testing"
"github.com/gin-contrib/static"
"github.com/prymitive/karma/internal/config"
log "github.com/sirupsen/logrus"
@@ -138,3 +140,130 @@ func TestLoadTemplateUnparsable(t *testing.T) {
t.Error("loadTemplate() with unparsable file didn't cause log.Fatal()")
}
}
func TestAssetFallbackMIME(t *testing.T) {
mockConfig()
r := ginTestEngine()
r.Use(static.Serve(getViewURL("/"), newBinaryFileSystem("cmd/karma/tests/bindata")))
req := httptest.NewRequest("GET", "/bin.data", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != 200 {
t.Errorf("Invalid status code for GET %s: %d", "/bin.data", resp.Code)
}
if resp.Result().Header.Get("Content-Type") != "text/plain; charset=utf-8" {
t.Errorf("Invalid Content-Type for GET /bin.data: %s, expected 'text/plain; charset=utf-8'", resp.Result().Header.Get("Content-Type"))
}
}
func TestStaticFiles(t *testing.T) {
type staticFileTestCase struct {
path string
code int
mime string
}
var staticFileTests = []staticFileTestCase{
{
path: "/favicon.ico",
code: 200,
mime: "image/x-icon",
},
{
path: "/manifest.json",
code: 200,
mime: "application/json",
},
{
path: "/index.xml",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/xxx",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/static/abcd",
code: 404,
mime: "text/plain; charset=utf-8",
},
}
mockConfig()
r := ginTestEngine()
for _, staticFileTest := range staticFileTests {
req := httptest.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)
}
if resp.Result().Header.Get("Content-Type") != staticFileTest.mime {
t.Errorf("Invalid Content-Type for GET %s: %s, expected %s", staticFileTest.path, resp.Result().Header.Get("Content-Type"), staticFileTest.mime)
}
}
}
func TestStaticFilesPrefix(t *testing.T) {
type staticFileTestCase struct {
path string
code int
mime string
}
var staticFilePrefixTests = []staticFileTestCase{
{
path: "/sub/favicon.ico",
code: 200,
mime: "image/x-icon",
},
{
path: "/favicon.ico",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/sub/sub/favicon.ico",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/sub/manifest.json",
code: 200,
mime: "application/json",
},
{
path: "/sub/index.xml",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/sub/xxx",
code: 404,
mime: "text/plain; charset=utf-8",
},
{
path: "/sub/static/abcd",
code: 404,
mime: "text/plain; charset=utf-8",
},
}
os.Setenv("LISTEN_PREFIX", "/sub")
defer os.Unsetenv("LISTEN_PREFIX")
mockConfig()
r := ginTestEngine()
for _, staticFileTest := range staticFilePrefixTests {
req := httptest.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)
}
if resp.Result().Header.Get("Content-Type") != staticFileTest.mime {
t.Errorf("Invalid Content-Type for GET %s: %q, expected %q", staticFileTest.path, resp.Result().Header.Get("Content-Type"), staticFileTest.mime)
}
}
}
+2 -5
View File
@@ -68,7 +68,7 @@ func TestAuthHeader(t *testing.T) {
apiCache = cache.New(cache.NoExpiration, 10*time.Second)
am, err := alertmanager.NewAlertmanager(
"dummy",
fmt.Sprintf("testAuthHeader/%s", version),
testCase.alertmanagerURI,
alertmanager.WithRequestTimeout(time.Second*5),
alertmanager.WithHTTPHeaders(testCase.headers),
@@ -85,12 +85,9 @@ func TestAuthHeader(t *testing.T) {
"api/v2/silences",
"api/v2/alerts/groups"} {
uri := fmt.Sprintf("http://%s/%s", testCase.alertmanagerHost, m)
uri := fmt.Sprintf("%s/%s", testCase.alertmanagerURI, m)
responder := mock.GetMockResponder(uri, version, m)
if responder == nil {
continue
}
httpmock.RegisterResponder("GET", uri,
func(req *http.Request) (*http.Response, error) {
if req.Host != testCase.alertmanagerHost {
+1
View File
@@ -0,0 +1 @@
OK
-79
View File
@@ -419,85 +419,6 @@ func TestAutocomplete(t *testing.T) {
}
}
type staticFileTestCase struct {
path string
code int
}
var staticFileTests = []staticFileTestCase{
{
path: "/favicon.ico",
code: 200,
},
{
path: "/manifest.json",
code: 200,
},
{
path: "/index.xml",
code: 404,
},
{
path: "/xxx",
code: 404,
},
{
path: "/static/abcd",
code: 404,
},
}
func TestStaticFiles(t *testing.T) {
mockConfig()
r := ginTestEngine()
for _, staticFileTest := range staticFileTests {
req := httptest.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)
}
}
}
var staticFilePrefixTests = []staticFileTestCase{
{
path: "/sub/favicon.ico",
code: 200,
},
{
path: "/sub/manifest.json",
code: 200,
},
{
path: "/sub/index.xml",
code: 404,
},
{
path: "/sub/xxx",
code: 404,
},
{
path: "/sub/static/abcd",
code: 404,
},
}
func TestStaticFilesPrefix(t *testing.T) {
os.Setenv("LISTEN_PREFIX", "/sub")
defer os.Unsetenv("LISTEN_PREFIX")
mockConfig()
r := ginTestEngine()
for _, staticFileTest := range staticFilePrefixTests {
req := httptest.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)
}
}
}
func TestGzipMiddleware(t *testing.T) {
mockConfig()
r := ginTestEngine()
+3
View File
@@ -16,8 +16,10 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
@@ -566,6 +568,7 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=