• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang test.MakeTestContextWithPath函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/stretchr/goweb/webcontext/test.MakeTestContextWithPath函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeTestContextWithPath函数的具体用法?Golang MakeTestContextWithPath怎么用?Golang MakeTestContextWithPath使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了MakeTestContextWithPath函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestPathMatchHandler_BreakCurrentPipeline

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)

}
开发者ID:nelsam,项目名称:goweb,代码行数:26,代码来源:path_match_handler_test.go


示例2: TestRegexPath

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

}
开发者ID:antianlu,项目名称:goweb,代码行数:25,代码来源:regexp_matcher_func_test.go


示例3: TestMapStaticFile

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

}
开发者ID:antianlu,项目名称:goweb,代码行数:35,代码来源:mapping_test.go


示例4: TestPathMatchHandler

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

}
开发者ID:nelsam,项目名称:goweb,代码行数:28,代码来源:path_match_handler_test.go


示例5: TestRespondEnvelopOptions

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

}
开发者ID:antianlu,项目名称:goweb,代码行数:26,代码来源:goweb_api_responder_test.go


示例6: TestHTTP_WithStatus_WithAlways200

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

}
开发者ID:rmulley,项目名称:goweb,代码行数:15,代码来源:goweb_http_responder_test.go


示例7: assertPathMatches

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

}
开发者ID:nelsam,项目名称:goweb,代码行数:11,代码来源:path_match_handler_test.go


示例8: TestRegexPath_Error

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)

}
开发者ID:antianlu,项目名称:goweb,代码行数:12,代码来源:regexp_matcher_func_test.go


示例9: TestMap_CatchAllAssumption

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)

}
开发者ID:antianlu,项目名称:goweb,代码行数:23,代码来源:mapping_test.go


示例10: TestMapStatic

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

	}

}
开发者ID:antianlu,项目名称:goweb,代码行数:51,代码来源:mapping_test.go


示例11: TestNewPathMatchHandler

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

}
开发者ID:nelsam,项目名称:goweb,代码行数:17,代码来源:path_match_handler_test.go


示例12: TestMap

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)

}
开发者ID:antianlu,项目名称:goweb,代码行数:19,代码来源:mapping_test.go


示例13: TestMap_WithSpecificMethod

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)

}
开发者ID:antianlu,项目名称:goweb,代码行数:21,代码来源:mapping_test.go


示例14: TestPathMatchHandler_WithMatcherFuncs_Match

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

}
开发者ID:nelsam,项目名称:goweb,代码行数:21,代码来源:path_match_handler_test.go


示例15: TestMappedHandlersBreakExecution

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

}
开发者ID:antianlu,项目名称:goweb,代码行数:24,代码来源:mapping_test.go



注:本文中的github.com/stretchr/goweb/webcontext/test.MakeTestContextWithPath函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang graceful.Run函数代码示例发布时间:2022-05-28
下一篇:
Golang test.MakeTestContext函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap