Skip to content

Commit

Permalink
chore: linting (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan committed Apr 22, 2022
1 parent fbb06ab commit 425c3f2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
59 changes: 35 additions & 24 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`^(.*/)([^?].*)?[?|.]*$`)

Expand Down Expand Up @@ -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":
Expand Down
43 changes: 24 additions & 19 deletions swagger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package echoSwagger

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}

0 comments on commit 425c3f2

Please sign in to comment.