Esempio n. 1
0
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)

}
Esempio n. 2
0
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).ObjxMap().Get("id").Data())

	ctx2 := context_test.MakeTestContextWithPath("/collection")
	will, _ = h.WillHandle(ctx2)
	assert.False(t, will)
	assert.Nil(t, ctx2.Data().Get(context.DataKeyPathParameters).Data())

	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")

}
Esempio n. 3
0
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))

}
Esempio n. 4
0
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).ObjxMap().Get("id").Data())

}
Esempio n. 5
0
func TestPathMatches_WithHttpMethod(t *testing.T) {

	pathPattern, _ := paths.NewPathPattern("people/123")

	h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { return nil }))
	h.HttpMethods = []string{"POST"}

	ctx := context_test.MakeTestContextWithDetails("/people/123", "GET")

	willHandle, _ := h.WillHandle(ctx)
	assert.Equal(t, false, willHandle)

	ctx2 := context_test.MakeTestContextWithDetails("/people/123", "POST")

	willHandle, _ = h.WillHandle(ctx2)
	assert.Equal(t, true, willHandle)

}
Esempio n. 6
0
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")

}
Esempio n. 7
0
// handlerForOptions gets or creates a Handler object based on the specified
// options.  See goweb.Map for details of valid options.
func (h *HttpHandler) handlerForOptions(options ...interface{}) (Handler, error) {

	if len(options) == 0 {
		// no arguments is an error
		panic("goweb: Cannot call Map functions with no arguments.")
	}

	var matcherFuncStartPos int = -1
	var methods []string
	var path string
	var executor HandlerExecutionFunc

	switch options[0].(type) {
	case string, []string:

		switch options[1].(type) {
		case nil:
			panic("goweb: Cannot call Map with 2nd argument nil.")
		case string: // (method|methods, path, executor, ...)

			// get the methods from the arguments
			switch options[0].(type) {
			case []string:
				methods = options[0].([]string)
			case string:
				methods = []string{options[0].(string)}
			}

			path = options[1].(string)
			executor = options[2].(func(context.Context) error)
			matcherFuncStartPos = 3
		default: // (path, executor, ...)
			path = options[0].(string)
			executor = options[1].(func(context.Context) error)
			matcherFuncStartPos = 2
		}
	case Handler: // actual handler object
		return options[0].(Handler), nil
	default: // (executor)
		matcherFuncStartPos = 1
		path = "***"
		executor = options[0].(func(context.Context) error)
	}

	// collect the matcher funcs
	var matcherFuncs []MatcherFunc = findMatcherFuncs(options[matcherFuncStartPos:]...)

	pathPattern, pathErr := paths.NewPathPattern(path)

	if pathErr != nil {
		return nil, pathErr
	}

	handler := NewPathMatchHandler(pathPattern, executor)

	// did they specify a method?
	if len(methods) > 0 {
		handler.HttpMethods = methods
	}

	// do we have any MatcherFuncs?
	handler.MatcherFuncs = matcherFuncs

	// return the handler
	return handler, nil

}