Skip to content

Commit

Permalink
add base path to ServerErrorResponse (#112)
Browse files Browse the repository at this point in the history
Add base path to ServerErrorResponse

The root path is configurable via config "flamingo.router.path".

Having the base path available in the Response allows to set the path "dynamically" in static HMTL like.
  • Loading branch information
doertydoerk committed Feb 3, 2020
1 parent eb5761e commit 723fde7
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 0 deletions.
10 changes: 10 additions & 0 deletions framework/flamingo/mocks/event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions framework/flamingo/mocks/event_router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions framework/flamingo/mocks/event_subscriber.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions framework/flamingo/mocks/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions framework/flamingo/mocks/partial_template_engine.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions framework/flamingo/mocks/template_engine.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions framework/flamingo/mocks/template_func.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions framework/web/result.go
Expand Up @@ -370,13 +370,20 @@ func (r *Responder) ServerErrorWithCodeAndTemplate(err error, tpl string, status
if r.debug {
errstr = fmt.Sprintf("%+v", err)
}

basePath := ""
if r.router != nil {
basePath = r.router.base.Path
}

return &ServerErrorResponse{
Error: err,
RenderResponse: RenderResponse{
Template: tpl,
engine: r.engine,
DataResponse: DataResponse{
Data: map[string]interface{}{
"base": basePath,
"code": status,
"error": errstr,
},
Expand Down
75 changes: 75 additions & 0 deletions framework/web/result_test.go
@@ -0,0 +1,75 @@
package web

import (
"errors"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/flamingo/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
)

var mockTemplateEngine = &mocks.TemplateEngine{}

func getResponder(withRouter bool) Responder {
var router *Router

if withRouter == true {
router = &Router{
base: &url.URL{Path: "base_path"},
}
}

responder := Responder{}

responder.Inject(
router,
flamingo.NullLogger{},
&struct {
Engine flamingo.TemplateEngine `inject:",optional"`
Debug bool `inject:"config:flamingo.debug.mode"`
TemplateForbidden string `inject:"config:flamingo.template.err403"`
TemplateNotFound string `inject:"config:flamingo.template.err404"`
TemplateUnavailable string `inject:"config:flamingo.template.err503"`
TemplateErrorWithCode string `inject:"config:flamingo.template.errWithCode"`
}{
Engine: mockTemplateEngine,
Debug: false,
TemplateForbidden: "403_template",
TemplateNotFound: "404_template",
TemplateUnavailable: "503_template",
TemplateErrorWithCode: "withErrorCode_template",
})
return responder
}

func TestServerErrorWithCodeAndTemplate(t *testing.T) {
testErr := errors.New("test error")

t.Run("Router is not nil", func(t *testing.T) {
responder := getResponder(true)
require.NotNil(t, responder.router)

actual := responder.ServerErrorWithCodeAndTemplate(testErr, "403_template", 403)
assert.Equal(t, 403, int(actual.Response.Status))
assert.Equal(t, "403_template", actual.Template)

assert.Equal(t, "base_path", actual.Data.(map[string]interface{})["base"].(string))
assert.Equal(t, 403, int(actual.Data.(map[string]interface{})["code"].(uint)))
assert.Equal(t, testErr.Error(), actual.Data.(map[string]interface{})["error"].(string))
})

t.Run("Router is nil", func(t *testing.T) {
responder := getResponder(false)
require.Nil(t, responder.router)

actual := responder.ServerErrorWithCodeAndTemplate(testErr, "403_template", 403)
assert.Equal(t, 403, int(actual.Response.Status))
assert.Equal(t, "403_template", actual.Template)

assert.Equal(t, "", actual.Data.(map[string]interface{})["base"].(string))
assert.Equal(t, 403, int(actual.Data.(map[string]interface{})["code"].(uint)))
assert.Equal(t, testErr.Error(), actual.Data.(map[string]interface{})["error"].(string))
})
}

0 comments on commit 723fde7

Please sign in to comment.