From 38f90f396fa0c19acc1afbe13a48b89c773a377c Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Tue, 4 Dec 2018 15:57:31 -0800 Subject: [PATCH] Adds more tests --- __snapshots__/dozzle.snapshot | 31 +++++++++++++++ main.go | 39 +++++++++---------- main_test.go | 72 ++++++++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 22 deletions(-) diff --git a/__snapshots__/dozzle.snapshot b/__snapshots__/dozzle.snapshot index 541b07c2..e10821f6 100644 --- a/__snapshots__/dozzle.snapshot +++ b/__snapshots__/dozzle.snapshot @@ -1,3 +1,34 @@ +/* snapshot: Test_createRoutes_foobar */ +HTTP/1.1 200 OK +Connection: close +Content-Type: text/plain; charset=utf-8 + +foo page + +/* snapshot: Test_createRoutes_index */ +HTTP/1.1 200 OK +Connection: close +Content-Type: text/plain; charset=utf-8 + +index page + +/* snapshot: Test_createRoutes_redirect */ +HTTP/1.1 301 Moved Permanently +Connection: close +Content-Type: text/html; charset=utf-8 +Location: /foobar/ + +Moved Permanently. + +/* snapshot: Test_createRoutes_version */ +HTTP/1.1 200 OK +Connection: close +Content-Type: text/plain; charset=utf-8 + +dev +none +unknown + /* snapshot: Test_handler_listContainers_happy */ HTTP/1.1 200 OK Connection: close diff --git a/main.go b/main.go index 384d2356..09883718 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,22 @@ func init() { }) } +func createRoutes(base string, h *handler) *mux.Router { + r := mux.NewRouter() + if base != "/" { + r.HandleFunc(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + http.Redirect(w, req, base+"/", http.StatusMovedPermanently) + })) + } + s := r.PathPrefix(base).Subrouter() + s.HandleFunc("/api/containers.json", h.listContainers) + s.HandleFunc("/api/logs/stream", h.streamLogs) + s.HandleFunc("/api/events/stream", h.streamEvents) + s.HandleFunc("/version", h.version) + s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index))) + return r +} + func main() { dockerClient := docker.NewClient() _, err := dockerClient.ListContainers() @@ -55,27 +71,8 @@ func main() { } box := packr.NewBox("./static") - h := &handler{dockerClient, box} - - r := mux.NewRouter() - - if base != "/" { - r.HandleFunc(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - http.Redirect(w, req, base+"/", http.StatusMovedPermanently) - })) - } - - s := r.PathPrefix(base).Subrouter() - s.HandleFunc("/api/containers.json", h.listContainers) - s.HandleFunc("/api/logs/stream", h.streamLogs) - s.HandleFunc("/api/events/stream", h.streamEvents) - s.HandleFunc("/version", h.version) - s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(h.index))) - - srv := &http.Server{ - Addr: addr, - Handler: r, - } + r := createRoutes(base, &handler{dockerClient, box}) + srv := &http.Server{Addr: addr, Handler: r} go func() { log.Infof("Accepting connections on %s", srv.Addr) diff --git a/main_test.go b/main_test.go index 5319e54f..c38413ad 100644 --- a/main_test.go +++ b/main_test.go @@ -3,7 +3,7 @@ package main import ( "context" "errors" - "github.com/docker/docker/api/types/events" + "github.com/magiconair/properties/assert" "net/http" "net/http/httptest" "os" @@ -11,6 +11,8 @@ import ( "github.com/amir20/dozzle/docker" "github.com/beme/abide" + "github.com/docker/docker/api/types/events" + "github.com/gobuffalo/packr" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -210,6 +212,74 @@ func Test_handler_streamEvents_error_request(t *testing.T) { mockedClient.AssertExpectations(t) } +func Test_createRoutes_index(t *testing.T) { + mockedClient := new(MockedClient) + box := packr.NewBox("./virtual") + require.NoError(t, box.AddString("index.html", "index page"), "AddString should have no error.") + + handler := createRoutes("/", &handler{mockedClient, box}) + req, err := http.NewRequest("GET", "/", nil) + require.NoError(t, err, "NewRequest should not return an error.") + rr := httptest.NewRecorder() + + handler.ServeHTTP(rr, req) + abide.AssertHTTPResponse(t, t.Name(), rr.Result()) +} + +func Test_createRoutes_redirect(t *testing.T) { + mockedClient := new(MockedClient) + box := packr.NewBox("./virtual") + + handler := createRoutes("/foobar", &handler{mockedClient, box}) + req, err := http.NewRequest("GET", "/foobar", nil) + require.NoError(t, err, "NewRequest should not return an error.") + rr := httptest.NewRecorder() + + handler.ServeHTTP(rr, req) + abide.AssertHTTPResponse(t, t.Name(), rr.Result()) +} + +func Test_createRoutes_foobar(t *testing.T) { + mockedClient := new(MockedClient) + box := packr.NewBox("./virtual") + require.NoError(t, box.AddString("index.html", "foo page"), "AddString should have no error.") + + handler := createRoutes("/foobar", &handler{mockedClient, box}) + req, err := http.NewRequest("GET", "/foobar/", nil) + require.NoError(t, err, "NewRequest should not return an error.") + rr := httptest.NewRecorder() + + handler.ServeHTTP(rr, req) + abide.AssertHTTPResponse(t, t.Name(), rr.Result()) +} + +func Test_createRoutes_foobar_file(t *testing.T) { + mockedClient := new(MockedClient) + box := packr.NewBox("./virtual") + require.NoError(t, box.AddString("/test", "test page"), "AddString should have no error.") + + handler := createRoutes("/foobar", &handler{mockedClient, box}) + req, err := http.NewRequest("GET", "/foobar/test", nil) + require.NoError(t, err, "NewRequest should not return an error.") + rr := httptest.NewRecorder() + + handler.ServeHTTP(rr, req) + assert.Equal(t, rr.Body.String(), "test page", "page doesn't match") +} + +func Test_createRoutes_version(t *testing.T) { + mockedClient := new(MockedClient) + box := packr.NewBox("./virtual") + + handler := createRoutes("/", &handler{mockedClient, box}) + req, err := http.NewRequest("GET", "/version", nil) + require.NoError(t, err, "NewRequest should not return an error.") + rr := httptest.NewRecorder() + + handler.ServeHTTP(rr, req) + abide.AssertHTTPResponse(t, t.Name(), rr.Result()) +} + func TestMain(m *testing.M) { exit := m.Run() abide.Cleanup()