From 425c3f232a0b41ece7cdeaf52db3131e0b24897f Mon Sep 17 00:00:00 2001 From: Bogdan U Date: Fri, 22 Apr 2022 18:05:55 +0300 Subject: [PATCH] chore: linting (#81) --- swagger.go | 59 +++++++++++++++++++++++++++++-------------------- swagger_test.go | 43 +++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/swagger.go b/swagger.go index 7c21683..686faf4 100644 --- a/swagger.go +++ b/swagger.go @@ -17,77 +17,88 @@ import ( type Config struct { // The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `mockedSwag.json`. URL string - DeepLinking bool DocExpansion string DomID string InstanceName string + DeepLinking bool PersistAuthorization bool } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). -func URL(url string) func(c *Config) { +func URL(url string) func(*Config) { return func(c *Config) { c.URL = url } } // DeepLinking true, false. -func DeepLinking(deepLinking bool) func(c *Config) { +func DeepLinking(deepLinking bool) func(*Config) { return func(c *Config) { c.DeepLinking = deepLinking } } // DocExpansion list, full, none. -func DocExpansion(docExpansion string) func(c *Config) { +func DocExpansion(docExpansion string) func(*Config) { return func(c *Config) { c.DocExpansion = docExpansion } } // DomID #swagger-ui. -func DomID(domID string) func(c *Config) { +func DomID(domID string) func(*Config) { return func(c *Config) { c.DomID = domID } } -// InstanceName specified swag instance name -func InstanceName(instanceName string) func(c *Config) { +// InstanceName specified swag instance name. +func InstanceName(instanceName string) func(*Config) { return func(c *Config) { c.InstanceName = instanceName } } -// If set to true, it persists authorization data and it would not be lost on browser close/refresh -// Defaults to false -func PersistAuthorization(persistAuthorization bool) func(c *Config) { +// PersistAuthorization Persist authorization information over browser close/refresh. +// Defaults to false. +func PersistAuthorization(persistAuthorization bool) func(*Config) { return func(c *Config) { c.PersistAuthorization = persistAuthorization } } +func newConfig(configFns ...func(*Config)) *Config { + config := Config{ + URL: "doc.json", + DocExpansion: "list", + DomID: "swagger-ui", + InstanceName: "swagger", + DeepLinking: true, + PersistAuthorization: false, + } + + for _, fn := range configFns { + fn(&config) + } + + if config.InstanceName == "" { + config.InstanceName = swag.Name + } + + return &config +} + // WrapHandler wraps swaggerFiles.Handler and returns echo.HandlerFunc var WrapHandler = EchoWrapHandler() // EchoWrapHandler wraps `http.Handler` into `echo.HandlerFunc`. -func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { +func EchoWrapHandler(options ...func(*Config)) echo.HandlerFunc { var once sync.Once - config := &Config{ - URL: "doc.json", - DeepLinking: true, - DocExpansion: "list", - DomID: "#swagger-ui", - } - - for _, configFn := range configFns { - configFn(config) - } + config := newConfig(options...) // create a template with name - t := template.New("swagger_index.html") - index, _ := t.Parse(indexTemplate) + index, _ := template.New("swagger_index.html").Parse(indexTemplate) var re = regexp.MustCompile(`^(.*/)([^?].*)?[?|.]*$`) @@ -129,7 +140,7 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc { switch path { case "": - c.Redirect(http.StatusMovedPermanently, h.Prefix+"index.html") + _ = c.Redirect(http.StatusMovedPermanently, h.Prefix+"index.html") case "index.html": _ = index.Execute(c.Response().Writer, config) case "doc.json": diff --git a/swagger_test.go b/swagger_test.go index 359e554..7f73a7b 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -1,6 +1,7 @@ package echoSwagger import ( + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -243,11 +244,17 @@ func TestWrapHandler(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, performRequest(http.MethodGet, "/doc.json", router).Code) - swag.Register(swag.Name, &mockedSwag{}) + doc := &mockedSwag{} + swag.Register(swag.Name, doc) w2 := performRequest(http.MethodGet, "/doc.json", router) assert.Equal(t, http.StatusOK, w2.Code) assert.Equal(t, w2.Header()["Content-Type"][0], "application/json; charset=utf-8") + // Perform body rendering validation + w2Body, err := ioutil.ReadAll(w2.Body) + assert.NoError(t, err) + assert.Equal(t, doc.ReadDoc(), string(w2Body)) + w3 := performRequest(http.MethodGet, "/favicon-16x16.png", router) assert.Equal(t, http.StatusOK, w3.Code) assert.Equal(t, w3.Header()["Content-Type"][0], "image/png") @@ -323,49 +330,47 @@ func performRequest(method, target string, e http.Handler) *httptest.ResponseRec } func TestURL(t *testing.T) { + var cfg Config expected := "https://github.com/swaggo/http-swagger" - cfg := Config{} - configFunc := URL(expected) - configFunc(&cfg) + URL(expected)(&cfg) assert.Equal(t, expected, cfg.URL) } func TestDeepLinking(t *testing.T) { + var cfg Config expected := true - cfg := Config{} - configFunc := DeepLinking(expected) - configFunc(&cfg) + DeepLinking(expected)(&cfg) assert.Equal(t, expected, cfg.DeepLinking) } func TestDocExpansion(t *testing.T) { + var cfg Config expected := "https://github.com/swaggo/docs" - cfg := Config{} - configFunc := DocExpansion(expected) - configFunc(&cfg) + DocExpansion(expected)(&cfg) assert.Equal(t, expected, cfg.DocExpansion) } func TestDomID(t *testing.T) { + var cfg Config expected := "#swagger-ui" - cfg := Config{} - configFunc := DomID(expected) - configFunc(&cfg) + DomID(expected)(&cfg) assert.Equal(t, expected, cfg.DomID) } func TestInstanceName(t *testing.T) { + var cfg Config + expected := "custom-instance-name" - cfg := Config{} - configFunc := InstanceName(expected) - configFunc(&cfg) + InstanceName(expected)(&cfg) assert.Equal(t, expected, cfg.InstanceName) + + newCfg := newConfig(InstanceName("")) + assert.Equal(t, swag.Name, newCfg.InstanceName) } func TestPersistAuthorization(t *testing.T) { + var cfg Config expected := true - cfg := Config{} - configFunc := PersistAuthorization(expected) - configFunc(&cfg) + PersistAuthorization(expected)(&cfg) assert.Equal(t, expected, cfg.PersistAuthorization) }