func TestPathMatchHandler_BreakCurrentPipeline(t *testing.T) { pathPattern, _ := paths.NewPathPattern("collection/{id}/name") h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { return nil })) h.BreakCurrentPipeline = true ctx1 := context_test.MakeTestContextWithPath("/collection/123/name") breakCurrentPipeline, _ := h.Handle(ctx1) assert.True(t, breakCurrentPipeline) h = NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { return nil })) h.BreakCurrentPipeline = false ctx1 = context_test.MakeTestContextWithPath("/collection/123/name") breakCurrentPipeline, _ = h.Handle(ctx1) assert.False(t, breakCurrentPipeline) }
func TestMapStaticFile(t *testing.T) { codecService := new(codecservices.WebCodecService) h := NewHttpHandler(codecService) h.MapStaticFile("/static-file", "/location/of/static-file") assert.Equal(t, 1, len(h.HandlersPipe())) staticHandler := h.HandlersPipe()[0].(*PathMatchHandler) if assert.Equal(t, 1, len(staticHandler.HttpMethods)) { assert.Equal(t, goweb_http.MethodGet, staticHandler.HttpMethods[0]) } var ctx context.Context var willHandle bool ctx = context_test.MakeTestContextWithPath("/static-file") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("static-file") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("static-file/") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("static-file/something-else") willHandle, _ = staticHandler.WillHandle(ctx) assert.False(t, willHandle, "Static handler NOT should handle") }
func TestPathMatchHandler(t *testing.T) { pathPattern, _ := paths.NewPathPattern("collection/{id}/name") var called bool = false h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { called = true return nil })) ctx1 := context_test.MakeTestContextWithPath("/collection/123/name") will, _ := h.WillHandle(ctx1) assert.True(t, will) h.Handle(ctx1) assert.True(t, called, "Method should be called") assert.Equal(t, "123", ctx1.Data().Get(context.DataKeyPathParameters).(objects.Map).Get("id")) ctx2 := context_test.MakeTestContextWithPath("/collection") will, _ = h.WillHandle(ctx2) assert.False(t, will) assert.Nil(t, ctx2.Data().Get(context.DataKeyPathParameters)) h.BreakCurrentPipeline = true shouldStop, handleErr := h.Handle(ctx2) assert.Nil(t, handleErr) assert.True(t, shouldStop) assert.True(t, called, "Handler func should get called") }
func TestRegexPath(t *testing.T) { pattern := `^[a-z]+\[[0-9]+\]$` matcherFunc := RegexPath(pattern) var ctx context.Context var decision MatcherFuncDecision ctx = context_test.MakeTestContextWithPath("adam[23]") decision, _ = matcherFunc(ctx) assert.Equal(t, Match, decision, "adam[23] should match") ctx = context_test.MakeTestContextWithPath("eve[7]") decision, _ = matcherFunc(ctx) assert.Equal(t, Match, decision, "eve[7] should match") ctx = context_test.MakeTestContextWithPath("Job[23]") decision, _ = matcherFunc(ctx) assert.Equal(t, NoMatch, decision, "Job[23] should NOT match") ctx = context_test.MakeTestContextWithPath("snakey") decision, _ = matcherFunc(ctx) assert.Equal(t, NoMatch, decision, "snakey should NOT match") }
func assertPathMatches(t *testing.T, pattern, path string, shouldMatch bool) bool { pathPattern, _ := paths.NewPathPattern(pattern) ctx := context_test.MakeTestContextWithPath(path) h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { return nil })) willHandle, _ := h.WillHandle(ctx) return assert.Equal(t, shouldMatch, willHandle, fmt.Sprintf("WillHandle should be %v for '%s' with path '%s'.", shouldMatch, pattern, path)) }
func TestRegexPath_Error(t *testing.T) { pattern := `[0-9]++` matcherFunc := RegexPath(pattern) var ctx context.Context ctx = context_test.MakeTestContextWithPath("adam[23]") _, err := matcherFunc(ctx) assert.Error(t, err) }
func TestMap_CatchAllAssumption(t *testing.T) { codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) called := false handler.Map(func(c context.Context) error { called = true return nil }) assert.Equal(t, 1, len(handler.HandlersPipe())) ctx := context_test.MakeTestContextWithPath("people/123") handler.Handlers.Handle(ctx) assert.True(t, called) called = false ctx = context_test.MakeTestContextWithPath("something-else") handler.Handlers.Handle(ctx) assert.True(t, called) }
func TestMapStatic(t *testing.T) { codecService := new(codecservices.WebCodecService) h := NewHttpHandler(codecService) h.MapStatic("/static", "/location/of/static") assert.Equal(t, 1, len(h.HandlersPipe())) staticHandler := h.HandlersPipe()[0].(*PathMatchHandler) if assert.Equal(t, 1, len(staticHandler.HttpMethods)) { assert.Equal(t, goweb_http.MethodGet, staticHandler.HttpMethods[0]) } var ctx context.Context var willHandle bool ctx = context_test.MakeTestContextWithPath("/static/some/deep/file.dat") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("/static/../static/some/deep/file.dat") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("/static/some/../file.dat") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("/static/../file.dat") willHandle, _ = staticHandler.WillHandle(ctx) assert.False(t, willHandle, "Static handler should not handle") ctx = context_test.MakeTestContextWithPath("/static") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("/static/") willHandle, _ = staticHandler.WillHandle(ctx) assert.True(t, willHandle, "Static handler should handle") ctx = context_test.MakeTestContextWithPath("/static/doc.go") willHandle, _ = staticHandler.WillHandle(ctx) _, staticHandleErr := staticHandler.Handle(ctx) if assert.NoError(t, staticHandleErr) { } }
func TestNewPathMatchHandler(t *testing.T) { pathPattern, _ := paths.NewPathPattern("collection/{id}/name") var called bool = false h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { called = true return nil })) ctx1 := context_test.MakeTestContextWithPath("/collection/123/name") will, _ := h.WillHandle(ctx1) assert.True(t, will) h.Handle(ctx1) assert.True(t, called, "Method should be called") assert.Equal(t, "123", ctx1.Data().Get(context.DataKeyPathParameters).(objects.Map).Get("id")) }
func TestMap(t *testing.T) { codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) called := false handler.Map("/people/{id}", func(c context.Context) error { called = true return nil }) assert.Equal(t, 1, len(handler.HandlersPipe())) ctx := context_test.MakeTestContextWithPath("people/123") handler.Handlers.Handle(ctx) assert.True(t, called) }
func TestPathMatchHandler_WithMatcherFuncs_Match(t *testing.T) { matcherFuncCalled := false handler := new(PathMatchHandler) handler.PathPattern, _ = paths.NewPathPattern("/specific/things") handler.ExecutionFunc = HandlerExecutionFunc(func(c context.Context) error { return nil }) handler.MatcherFuncs = []MatcherFunc{func(c context.Context) (MatcherFuncDecision, error) { matcherFuncCalled = true return Match, nil }} ctx1 := context_test.MakeTestContextWithPath("/collection/123/name") will, _ := handler.WillHandle(ctx1) assert.True(t, will, "Should want to handle even though the path DOESNT match") }
func TestMap_WithSpecificMethods(t *testing.T) { codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) called := false handler.Map([]string{"GET", "POST"}, "/people/{id}", func(c context.Context) error { called = true return nil }) assert.Equal(t, 1, len(handler.HandlersPipe())) ctx := context_test.MakeTestContextWithPath("people/123") handler.Handlers.Handle(ctx) assert.True(t, called) assert.Equal(t, "GET", handler.HandlersPipe()[0].(*PathMatchHandler).HttpMethods[0]) assert.Equal(t, "POST", handler.HandlersPipe()[0].(*PathMatchHandler).HttpMethods[1]) }
// https://github.com/stretchrcom/goweb/issues/19 func TestMappedHandlersBreakExecution(t *testing.T) { codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) handlerCalled := false catchAllCalled := false handler.Map("/people/{id}", func(c context.Context) error { handlerCalled = true return nil }) handler.Map(func(c context.Context) error { catchAllCalled = true return nil }) ctx := context_test.MakeTestContextWithPath("people/123") handler.Handlers.Handle(ctx) assert.True(t, handlerCalled) assert.False(t, catchAllCalled, "Catch-all should NOT get called, becuase something else specifically handled this context.") }