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

}
Esempio n. 3
0
func TestMapStaticFile(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	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")

}
Esempio n. 4
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).(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")

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

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContextWithPath("/?envelop=false")
	data := map[string]interface{}{"name": "Mat"}

	// When AlwaysEvenlopResponse = true but ?envelop=false
	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false
	ctx = context_test.MakeTestContext()
	API.AlwaysEnvelopResponse = false

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false but ?envelop=true
	ctx = context_test.MakeTestContextWithPath("/?envelop=true")

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"name\":\"Mat\"},\"s\":200}")

}
Esempio n. 6
0
// https://github.com/stretchr/goweb/issues/30
func TestHTTP_WithStatus_WithAlways200(t *testing.T) {

	httpResponder := new(GowebHTTPResponder)
	var ctx context.Context

	ctx = context_test.MakeTestContextWithPath("people/123?always200=true")
	httpResponder.WithStatus(ctx, 500)
	assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, 200)

	ctx = context_test.MakeTestContextWithPath("people/123?always200=1")
	httpResponder.WithStatus(ctx, 500)
	assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, 200)

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

}
Esempio n. 9
0
func TestMap_CatchAllAssumption(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	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)

}
Esempio n. 10
0
func TestMapStatic(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	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) {

	}

}
Esempio n. 11
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).(objects.Map).Get("id"))

}
Esempio n. 12
0
func TestMap(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	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)

}
Esempio n. 13
0
func TestMap_WithSpecificMethod(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	handler := NewHttpHandler(codecService)

	called := false
	handler.Map("GET", "/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.True(t, handler.HandlersPipe()[0].(*PathMatchHandler).BreakCurrentPipeline)

}
Esempio n. 14
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. 15
0
// https://github.com/stretchr/goweb/issues/19
func TestMappedHandlersBreakExecution(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	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.")

}