Skip to content

Commit

Permalink
Merge pull request #12 from kohkimakimoto/update-test
Browse files Browse the repository at this point in the history
Update test
  • Loading branch information
kohkimakimoto committed Jun 10, 2023
2 parents 36e7d69 + 36d1073 commit 754e55c
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 151 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ node_modules/
.envrc

!.gitkeep


28 changes: 0 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,3 @@ open-coverage-html: ## Open coverage report
clean: ## Clean generated files
@rm -rf .dev/coverage.html
@rm -rf .dev/coverage.out


.PHONY: run/helloworld
run/helloworld: ## Run example helloworld app
@mkdir -p examples/helloworld/.generated
@cd examples/helloworld && ./scripts/process-starter.py \
'{"command": "air", "prefix": "[air] ", "prefixColor": "blue"}'


#.PHONY: clean
#clean: ## Clean the generated contents
# @rm -rf coverage-report.html
# @rm -rf examples/helloworld/gen/dist
# @rm -rf examples/helloworld/main
# @rm -rf examples/helloworld/.tmp

#.PHONY: examples/helloworld/start
#examples/helloworld/start: examples/helloworld/yarn-install ## start example helloworld app
# @if [[ ! -e examples/helloworld/gen/dist ]]; then cd examples/helloworld && yarn build; fi
# @cd examples/helloworld && ./process-starter.py --run "yarn dev" "air"
#
#.PHONY: examples/helloworld/build
#examples/helloworld/build: examples/helloworld/yarn-install ## build example helloworld app
# @cd examples/helloworld && yarn build && go build -o main main.go
#
#.PHONY: examples/helloworld/yarn-install
#examples/helloworld/yarn-install:
# @if [[ ! -e examples/helloworld/node_modules ]]; then cd examples/helloworld && yarn install; fi
67 changes: 39 additions & 28 deletions csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,47 @@ func TestCSRF(t *testing.T) {
}

func TestCSRFWithConfig(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
csrf := CSRFWithConfig(CSRFConfig{
Skipper: func(c echo.Context) bool {
path := c.Request().URL.Path
return strings.HasPrefix(path, "/should_skip")
},
})
h := csrf(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
t.Run("has skipper but it should not skip", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
csrf := CSRFWithConfig(CSRFConfig{
Skipper: func(c echo.Context) bool {
path := c.Request().URL.Path
return strings.HasPrefix(path, "/should_skip")
},
})
h := csrf(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})
_ = h(c)

cookie := rec.Header().Get(echo.HeaderSetCookie)
if !strings.Contains(cookie, "XSRF-TOKEN") {
t.Errorf("should contain XSRF-TOKEN, but not '%v'", cookie)
}
})
_ = h(c)

cookie := rec.Header().Get(echo.HeaderSetCookie)
if !strings.Contains(cookie, "XSRF-TOKEN") {
t.Errorf("should contain XSRF-TOKEN, but not '%v'", cookie)
}
t.Run("has skipper and it should skip", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/should_skip", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
csrf := CSRFWithConfig(CSRFConfig{
Skipper: func(c echo.Context) bool {
path := c.Request().URL.Path
return strings.HasPrefix(path, "/should_skip")
},
})
h := csrf(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})
_ = h(c)

req = httptest.NewRequest(http.MethodGet, "/should_skip", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = csrf(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
cookie := rec.Header().Get(echo.HeaderSetCookie)
if strings.Contains(cookie, "XSRF-TOKEN") {
t.Errorf("should NOT contain XSRF-TOKEN because it should be skipped, but not '%v'", cookie)
}
})
_ = h(c)
cookie = rec.Header().Get(echo.HeaderSetCookie)
if strings.Contains(cookie, "XSRF-TOKEN") {
t.Errorf("should NOT contain XSRF-TOKEN because it should be skipped, but not '%v'", cookie)
}

}
20 changes: 20 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package inertia

import (
"io"
"testing"
)

type mockRenderer struct {
render func(w io.Writer, name string, data map[string]interface{}, in *Inertia) error
}

func (r *mockRenderer) Render(w io.Writer, name string, data map[string]interface{}, in *Inertia) error {
return r.render(w, name, data, in)
}

func testNewMockRenderer(t *testing.T, renderFunc func(w io.Writer, name string, data map[string]interface{}, in *Inertia) error) Renderer {
t.Helper()

return &mockRenderer{render: renderFunc}
}
4 changes: 2 additions & 2 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func (i *Inertia) render(code int, component string, props, viewData map[string]

props = mergeProps(i.sharedProps, props)

only := splitOrNil(req.Header.Get(HeaderXInertiaPartialData), ",")
if only != nil && req.Header.Get(HeaderXInertiaPartialComponent) == component {
only := splitAndRemoveEmpty(req.Header.Get(HeaderXInertiaPartialData), ",")
if len(only) > 0 && req.Header.Get(HeaderXInertiaPartialComponent) == component {
filteredProps := map[string]interface{}{}
for _, key := range only {
filteredProps[key] = props[key]
Expand Down

0 comments on commit 754e55c

Please sign in to comment.