Correctly fill swagger host field

This commit is contained in:
Jannis Mattheis 2019-01-03 15:52:12 +01:00
parent 82cb2e384a
commit 19811dafb2
2 changed files with 10 additions and 2 deletions

View File

@ -5,11 +5,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr"
"github.com/gotify/location"
)
// Serve serves the documentation.
func Serve(ctx *gin.Context) {
ctx.Writer.WriteString(get(ctx.Request.URL.Host))
url := location.Get(ctx)
ctx.Writer.WriteString(get(url.Host))
}
func get(host string) string {

View File

@ -2,6 +2,7 @@ package docs
import (
"net/http/httptest"
"net/url"
"testing"
"github.com/gin-gonic/gin"
@ -13,11 +14,16 @@ func TestServe(t *testing.T) {
mode.Set(mode.TestDev)
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
withURL(ctx, "http", "example.com")
ctx.Request = httptest.NewRequest("GET", "/swagger", nil)
ctx.Request.URL.Host = "localhost"
Serve(ctx)
content := recorder.Body.String()
assert.NotEmpty(t, content)
}
func withURL(ctx *gin.Context, scheme, host string) {
ctx.Set("location", &url.URL{Scheme: scheme, Host: host})
}