Skip to content

Commit

Permalink
fix flashing errors on version diff redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Sarvarov committed Jul 18, 2024
1 parent 89ec8e9 commit 9925544
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (i *Inertia) Middleware(next http.Handler) http.Handler {
//
// https://inertiajs.com/asset-versioning
if r.Method == http.MethodGet && inertiaVersionFromRequest(r) != i.version {
setInertiaLocationInResponse(w2, r.URL.RequestURI())
i.Location(w2, r, r.URL.RequestURI())
return
}

Expand Down
16 changes: 15 additions & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,21 @@ func TestInertia_Middleware(t *testing.T) {
t.Run("assert versioning", func(t *testing.T) {
t.Parallel()

t.Run("diff version with GET, should change location with 409", func(t *testing.T) {
t.Run("diff version with GET, should change location with 409 and flash errors", func(t *testing.T) {
t.Parallel()

errors := ValidationErrors{
"foo": "baz",
"baz": "quz",
}

flashProvider := &flashProviderMock{
errors: errors,
}

i := I(func(i *Inertia) {
i.version = "foo"
i.flash = flashProvider
})

w, r := requestMock(http.MethodGet, "https://example.com/home")
Expand All @@ -79,6 +89,10 @@ func TestInertia_Middleware(t *testing.T) {
assertInertiaVary(t, w)
assertResponseStatusCode(t, w, http.StatusConflict)
assertInertiaLocation(t, w, "/home")

if !reflect.DeepEqual(flashProvider.errors, errors) {
t.Fatalf("got validation errors=%#v, want=%#v", flashProvider.errors, errors)
}
})

t.Run("diff version with POST, do nothing", func(t *testing.T) {
Expand Down

0 comments on commit 9925544

Please sign in to comment.