From 19811dafb2d44ae636d287c92c30281076a772d6 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Thu, 3 Jan 2019 15:52:12 +0100 Subject: [PATCH] Correctly fill swagger host field --- docs/swagger.go | 4 +++- docs/swagger_test.go | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/swagger.go b/docs/swagger.go index c68f5d3..2029209 100644 --- a/docs/swagger.go +++ b/docs/swagger.go @@ -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 { diff --git a/docs/swagger_test.go b/docs/swagger_test.go index 7e5e605..945bb7a 100644 --- a/docs/swagger_test.go +++ b/docs/swagger_test.go @@ -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}) +}