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

Golang iris.ResetDefault函数代码示例

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

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



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

示例1: TestContextReadXML

func TestContextReadXML(t *testing.T) {
	iris.ResetDefault()

	iris.Post("/xml", func(ctx *iris.Context) {
		obj := testBinderXMLData{}
		err := ctx.ReadXML(&obj)
		if err != nil {
			t.Fatalf("Error when parsing the XML body: %s", err.Error())
		}
		ctx.XML(iris.StatusOK, obj)
	})

	e := httptest.New(iris.Default, t)
	expectedObj := testBinderXMLData{
		XMLName:    xml.Name{Local: "info", Space: "info"},
		FirstAttr:  "this is the first attr",
		SecondAttr: "this is the second attr",
		Name:       "Iris web framework",
		Birth:      "13 March 2016",
		Stars:      4064,
	}
	// so far no WithXML or .XML like WithJSON and .JSON on httpexpect I added a feature request as post issue and we're waiting
	expectedBody := `<` + expectedObj.XMLName.Local + ` first="` + expectedObj.FirstAttr + `" second="` + expectedObj.SecondAttr + `"><name>` + expectedObj.Name + `</name><birth>` + expectedObj.Birth + `</birth><stars>` + strconv.Itoa(expectedObj.Stars) + `</stars></info>`
	e.POST("/xml").WithText(expectedBody).Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
}
开发者ID:kataras,项目名称:iris,代码行数:25,代码来源:context_test.go


示例2: TestMuxSimpleParty

func TestMuxSimpleParty(t *testing.T) {
	iris.ResetDefault()

	h := func(c *iris.Context) { c.WriteString(c.HostString() + c.PathString()) }

	if testEnableSubdomain {
		subdomainParty := iris.Party(testSubdomain + ".")
		{
			subdomainParty.Get("/", h)
			subdomainParty.Get("/path1", h)
			subdomainParty.Get("/path2", h)
			subdomainParty.Get("/namedpath/:param1/something/:param2", h)
			subdomainParty.Get("/namedpath/:param1/something/:param2/else", h)
		}
	}

	// simple
	p := iris.Party("/party1")
	{
		p.Get("/", h)
		p.Get("/path1", h)
		p.Get("/path2", h)
		p.Get("/namedpath/:param1/something/:param2", h)
		p.Get("/namedpath/:param1/something/:param2/else", h)
	}

	iris.Default.Config.VHost = "0.0.0.0:8080"
	// iris.Default.Config.Tester.Debug = true
	// iris.Default.Config.Tester.ExplicitURL = true
	e := httptest.New(iris.Default, t)

	request := func(reqPath string) {

		e.Request("GET", reqPath).
			Expect().
			Status(iris.StatusOK).Body().Equal(iris.Default.Config.VHost + reqPath)
	}

	// run the tests
	request("/party1/")
	request("/party1/path1")
	request("/party1/path2")
	request("/party1/namedpath/theparam1/something/theparam2")
	request("/party1/namedpath/theparam1/something/theparam2/else")

	if testEnableSubdomain {
		es := subdomainTester(e)
		subdomainRequest := func(reqPath string) {
			es.Request("GET", reqPath).
				Expect().
				Status(iris.StatusOK).Body().Equal(testSubdomainHost() + reqPath)
		}

		subdomainRequest("/")
		subdomainRequest("/path1")
		subdomainRequest("/path2")
		subdomainRequest("/namedpath/theparam1/something/theparam2")
		subdomainRequest("/namedpath/theparam1/something/theparam2/else")
	}
}
开发者ID:kataras,项目名称:iris,代码行数:60,代码来源:http_test.go


示例3: TestContextRedirectTo

// TestContextRedirectTo tests the named route redirect action
func TestContextRedirectTo(t *testing.T) {
	iris.ResetDefault()
	h := func(ctx *iris.Context) { ctx.Write(ctx.PathString()) }
	iris.Get("/mypath", h)("my-path")
	iris.Get("/mypostpath", h)("my-post-path")
	iris.Get("mypath/with/params/:param1/:param2", func(ctx *iris.Context) {
		if l := ctx.ParamsLen(); l != 2 {
			t.Fatalf("Strange error, expecting parameters to be two but we got: %d", l)
		}
		ctx.Write(ctx.PathString())
	})("my-path-with-params")

	iris.Get("/redirect/to/:routeName/*anyparams", func(ctx *iris.Context) {
		routeName := ctx.Param("routeName")
		var args []interface{}
		anyparams := ctx.Param("anyparams")
		if anyparams != "" && anyparams != "/" {
			params := strings.Split(anyparams[1:], "/") // firstparam/secondparam
			for _, s := range params {
				args = append(args, s)
			}
		}
		//println("Redirecting to: " + routeName + " with path: " + Path(routeName, args...))
		ctx.RedirectTo(routeName, args...)
	})

	e := httptest.New(iris.Default, t)

	e.GET("/redirect/to/my-path/").Expect().Status(iris.StatusOK).Body().Equal("/mypath")
	e.GET("/redirect/to/my-post-path/").Expect().Status(iris.StatusOK).Body().Equal("/mypostpath")
	e.GET("/redirect/to/my-path-with-params/firstparam/secondparam").Expect().Status(iris.StatusOK).Body().Equal("/mypath/with/params/firstparam/secondparam")
}
开发者ID:kataras,项目名称:iris,代码行数:33,代码来源:context_test.go


示例4: TestMuxAPI

func TestMuxAPI(t *testing.T) {
	iris.ResetDefault()

	middlewareResponseText := "I assume that you are authenticated\n"
	iris.API("/users", testUserAPI{}, func(ctx *iris.Context) { // optional middleware for .API
		// do your work here, or render a login window if not logged in, get the user and send it to the next middleware, or do  all here
		ctx.Set("user", "username")
		ctx.Next()
	}, func(ctx *iris.Context) {
		if ctx.Get("user") == "username" {
			ctx.Write(middlewareResponseText)
			ctx.Next()
		} else {
			ctx.SetStatusCode(iris.StatusUnauthorized)
		}
	})

	e := httptest.New(iris.Default, t)

	userID := "4077"
	formname := "kataras"

	e.GET("/users").Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get Users\n")
	e.GET("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get By " + userID + "\n")
	e.PUT("/users").WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Put, name: " + formname + "\n")
	e.POST("/users/"+userID).WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n")
	e.DELETE("/users/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Delete By " + userID + "\n")
}
开发者ID:kataras,项目名称:iris,代码行数:28,代码来源:http_test.go


示例5: TestMuxAPIWithParty

func TestMuxAPIWithParty(t *testing.T) {
	iris.ResetDefault()
	siteParty := iris.Party("sites/:site")

	middlewareResponseText := "I assume that you are authenticated\n"
	siteParty.API("/users", testUserAPI{}, func(ctx *iris.Context) {
		ctx.Set("user", "username")
		ctx.Next()
	}, func(ctx *iris.Context) {
		if ctx.Get("user") == "username" {
			ctx.Write(middlewareResponseText)
			ctx.Next()
		} else {
			ctx.SetStatusCode(iris.StatusUnauthorized)
		}
	})

	e := httptest.New(iris.Default, t)
	siteID := "1"
	apiPath := "/sites/" + siteID + "/users"
	userID := "4077"
	formname := "kataras"

	e.GET(apiPath).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get Users\n")
	e.GET(apiPath + "/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Get By " + userID + "\n")
	e.PUT(apiPath).WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Put, name: " + formname + "\n")
	e.POST(apiPath+"/"+userID).WithFormField("name", formname).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n")
	e.DELETE(apiPath + "/" + userID).Expect().Status(iris.StatusOK).Body().Equal(middlewareResponseText + "Delete By " + userID + "\n")
}
开发者ID:kataras,项目名称:iris,代码行数:29,代码来源:http_test.go


示例6: TestMuxCustomHandler

func TestMuxCustomHandler(t *testing.T) {
	iris.ResetDefault()
	myData := myTestHandlerData{
		Sysname: "Redhat",
		Version: 1,
	}
	iris.Handle("GET", "/custom_handler_1/:myparam", &myTestCustomHandler{myData})
	iris.Handle("GET", "/custom_handler_2/:myparam", &myTestCustomHandler{myData})

	e := httptest.New(iris.Default, t)
	// two times per testRoute
	param1 := "thisimyparam1"
	expectedData1 := myData
	expectedData1.DynamicPathParameter = param1
	e.GET("/custom_handler_1/" + param1).Expect().Status(iris.StatusOK).JSON().Equal(expectedData1)

	param2 := "thisimyparam2"
	expectedData2 := myData
	expectedData2.DynamicPathParameter = param2
	e.GET("/custom_handler_1/" + param2).Expect().Status(iris.StatusOK).JSON().Equal(expectedData2)

	param3 := "thisimyparam3"
	expectedData3 := myData
	expectedData3.DynamicPathParameter = param3
	e.GET("/custom_handler_2/" + param3).Expect().Status(iris.StatusOK).JSON().Equal(expectedData3)

	param4 := "thisimyparam4"
	expectedData4 := myData
	expectedData4.DynamicPathParameter = param4
	e.GET("/custom_handler_2/" + param4).Expect().Status(iris.StatusOK).JSON().Equal(expectedData4)
}
开发者ID:kataras,项目名称:iris,代码行数:31,代码来源:http_test.go


示例7: TestContextReadJSON

func TestContextReadJSON(t *testing.T) {
	iris.ResetDefault()
	iris.Post("/json", func(ctx *iris.Context) {
		obj := testBinderData{}
		err := ctx.ReadJSON(&obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	iris.Post("/json_pointer", func(ctx *iris.Context) {
		obj := &testBinderData{}
		err := ctx.ReadJSON(obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	e := httptest.New(iris.Default, t)
	passed := map[string]interface{}{"Username": "myusername", "Mail": "[email protected]", "mydata": []string{"mydata1", "mydata2"}}
	expectedObject := testBinderData{Username: "myusername", Mail: "[email protected]", Data: []string{"mydata1", "mydata2"}}

	e.POST("/json").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
	e.POST("/json_pointer").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
}
开发者ID:kataras,项目名称:iris,代码行数:27,代码来源:context_test.go


示例8: TestCache

func TestCache(t *testing.T) {

	iris.ResetDefault()

	expectedBodyStr := "Imagine it as a big message to achieve x20 response performance!"
	var textCounter, htmlCounter uint32

	iris.Get("/text", iris.Cache(func(ctx *iris.Context) {
		atomic.AddUint32(&textCounter, 1)
		ctx.Text(iris.StatusOK, expectedBodyStr)
	}, cacheDuration))

	iris.Get("/html", iris.Cache(func(ctx *iris.Context) {
		atomic.AddUint32(&htmlCounter, 1)
		ctx.HTML(iris.StatusOK, expectedBodyStr)
	}, cacheDuration))

	e := httptest.New(iris.Default, t)

	// test cache on text/plain
	if err := runCacheTest(e, "/text", &textCounter, expectedBodyStr, "text/plain"); err != nil {
		t.Fatal(err)
	}

	// text cache on text/html
	if err := runCacheTest(e, "/html", &htmlCounter, expectedBodyStr, "text/html"); err != nil {
		t.Fatal(err)
	}
}
开发者ID:kataras,项目名称:iris,代码行数:29,代码来源:http_test.go


示例9: TestPluginEvents

func TestPluginEvents(t *testing.T) {
	iris.ResetDefault()
	var plugins = iris.Default.Plugins
	var prelistenran, postlistenran, precloseran bool

	plugins.Add(iris.PreListenFunc(func(*iris.Framework) {
		prelistenran = true
	}))

	plugins.Add(iris.PostListenFunc(func(*iris.Framework) {
		postlistenran = true
	}))

	plugins.Add(iris.PreCloseFunc(func(*iris.Framework) {
		precloseran = true
	}))

	myplugin := &testPluginEx{}
	plugins.Add(myplugin)
	if plugins.Len() != 4 {
		t.Fatalf("Expected: %d plugins to be registed but we got: %d", 4, plugins.Len())
	}
	desc := plugins.GetDescription(myplugin)
	if desc != testPluginExDescription {
		t.Fatalf("Expected: %s as Description of the plugin but got: %s", testPluginExDescription, desc)
	}

	plugins.DoPreListen(nil)
	plugins.DoPostListen(nil)
	plugins.DoPreClose(nil)

	if !prelistenran {
		t.Fatalf("Expected to run PreListen Func but it doesn't!")
	}
	if !postlistenran {
		t.Fatalf("Expected to run PostListen Func but it doesn't!")
	}
	if !precloseran {
		t.Fatalf("Expected to run PostListen Func but it doesn't!")
	}

	if !myplugin.named {
		t.Fatalf("Plugin should be named with: %s!", testPluginExName)
	}
	if !myplugin.activated {
		t.Fatalf("Plugin should be activated but it's not!")
	}
	if !myplugin.prelistenran {
		t.Fatalf("Expected to run PreListen Struct but it doesn't!")
	}
	if !myplugin.postlistenran {
		t.Fatalf("Expected to run PostListen Struct but it doesn't!")
	}
	if !myplugin.precloseran {
		t.Fatalf("Expected to run PostListen Struct but it doesn't!")
	}

}
开发者ID:kataras,项目名称:iris,代码行数:58,代码来源:plugin_test.go


示例10: TestMuxCustomErrors

func TestMuxCustomErrors(t *testing.T) {
	var (
		notFoundMessage        = "Iris custom message for 404 not found"
		internalServerMessage  = "Iris custom message for 500 internal server error"
		testRoutesCustomErrors = []testRoute{
			// NOT FOUND CUSTOM ERRORS - not registed
			{"GET", "/test_get_nofound_custom", "/test_get_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"POST", "/test_post_nofound_custom", "/test_post_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"PUT", "/test_put_nofound_custom", "/test_put_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"DELETE", "/test_delete_nofound_custom", "/test_delete_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"HEAD", "/test_head_nofound_custom", "/test_head_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"OPTIONS", "/test_options_nofound_custom", "/test_options_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"CONNECT", "/test_connect_nofound_custom", "/test_connect_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"PATCH", "/test_patch_nofound_custom", "/test_patch_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			{"TRACE", "/test_trace_nofound_custom", "/test_trace_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
			// SERVER INTERNAL ERROR 500 PANIC CUSTOM ERRORS - registed
			{"GET", "/test_get_panic_custom", "/test_get_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"POST", "/test_post_panic_custom", "/test_post_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"PUT", "/test_put_panic_custom", "/test_put_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"DELETE", "/test_delete_panic_custom", "/test_delete_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"HEAD", "/test_head_panic_custom", "/test_head_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"OPTIONS", "/test_options_panic_custom", "/test_options_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"CONNECT", "/test_connect_panic_custom", "/test_connect_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"PATCH", "/test_patch_panic_custom", "/test_patch_panic_custom", "", internalServerMessage, 500, true, nil, nil},
			{"TRACE", "/test_trace_panic_custom", "/test_trace_panic_custom", "", internalServerMessage, 500, true, nil, nil},
		}
	)
	iris.ResetDefault()
	// first register the testRoutes needed
	for _, r := range testRoutesCustomErrors {
		if r.Register {
			iris.HandleFunc(r.Method, r.Path, func(ctx *iris.Context) {
				ctx.EmitError(r.Status)
			})
		}
	}

	// register the custom errors
	iris.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
		ctx.Write("%s", notFoundMessage)
	})

	iris.OnError(iris.StatusInternalServerError, func(ctx *iris.Context) {
		ctx.Write("%s", internalServerMessage)
	})

	// create httpexpect instance that will call fasthtpp.RequestHandler directly
	e := httptest.New(iris.Default, t)

	// run the tests
	for _, r := range testRoutesCustomErrors {
		e.Request(r.Method, r.RequestPath).
			Expect().
			Status(r.Status).Body().Equal(r.Body)
	}
}
开发者ID:kataras,项目名称:iris,代码行数:56,代码来源:http_test.go


示例11: TestPluginActivate

func TestPluginActivate(t *testing.T) {
	iris.ResetDefault()
	var plugins = iris.Default.Plugins
	myplugin := testPluginActivationType{shouldError: false}
	plugins.Add(myplugin)

	if plugins.Len() != 2 { // 2 because it registeres a second plugin also
		t.Fatalf("Expected activated plugins to be: %d but we got: %d", 0, plugins.Len())
	}
}
开发者ID:kataras,项目名称:iris,代码行数:10,代码来源:plugin_test.go


示例12: TestPluginActivationError

// if any error returned from the Activate plugin's method, then this plugin and the plugins it registers should not be registered at all
func TestPluginActivationError(t *testing.T) {
	iris.ResetDefault()
	var plugins = iris.Default.Plugins
	myplugin := testPluginActivationType{shouldError: true}
	plugins.Add(myplugin)

	if plugins.Len() > 0 {
		t.Fatalf("Expected activated plugins to be: %d but we got: %d", 0, plugins.Len())
	}
}
开发者ID:kataras,项目名称:iris,代码行数:11,代码来源:plugin_test.go


示例13: TestContextURLParams

func TestContextURLParams(t *testing.T) {
	iris.ResetDefault()
	passedParams := map[string]string{"param1": "value1", "param2": "value2"}
	iris.Get("/", func(ctx *iris.Context) {
		params := ctx.URLParams()
		ctx.JSON(iris.StatusOK, params)
	})
	e := httptest.New(iris.Default, t)

	e.GET("/").WithQueryObject(passedParams).Expect().Status(iris.StatusOK).JSON().Equal(passedParams)
}
开发者ID:kataras,项目名称:iris,代码行数:11,代码来源:context_test.go


示例14: TestContextFormValueString

func TestContextFormValueString(t *testing.T) {
	iris.ResetDefault()
	var k, v string
	k = "postkey"
	v = "postvalue"
	iris.Post("/", func(ctx *iris.Context) {
		ctx.Write(k + "=" + ctx.FormValueString(k))
	})
	e := httptest.New(iris.Default, t)

	e.POST("/").WithFormField(k, v).Expect().Status(iris.StatusOK).Body().Equal(k + "=" + v)
}
开发者ID:kataras,项目名称:iris,代码行数:12,代码来源:context_test.go


示例15: TestMuxDecodeURL

func TestMuxDecodeURL(t *testing.T) {
	iris.ResetDefault()

	iris.Get("/encoding/:url", func(ctx *iris.Context) {
		url := iris.DecodeURL(ctx.Param("url"))
		ctx.SetStatusCode(iris.StatusOK)
		ctx.Write(url)
	})

	e := httptest.New(iris.Default, t)

	e.GET("/encoding/http%3A%2F%2Fsome-url.com").Expect().Status(iris.StatusOK).Body().Equal("http://some-url.com")
}
开发者ID:kataras,项目名称:iris,代码行数:13,代码来源:http_test.go


示例16: TestMultiRunningServers_v1_PROXY

// Contains the server test for multi running servers
func TestMultiRunningServers_v1_PROXY(t *testing.T) {
	defer iris.Close()
	host := "localhost" // you have to add it to your hosts file( for windows, as 127.0.0.1 mydomain.com)
	hostTLS := "localhost:9999"
	iris.Close()
	defer iris.Close()
	iris.ResetDefault()
	iris.Default.Config.DisableBanner = true
	// create the key and cert files on the fly, and delete them when this test finished
	certFile, ferr := ioutil.TempFile("", "cert")

	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	keyFile, ferr := ioutil.TempFile("", "key")
	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	defer func() {
		certFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(certFile.Name())

		keyFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(keyFile.Name())
	}()

	certFile.WriteString(testTLSCert)
	keyFile.WriteString(testTLSKey)

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write("Hello from %s", hostTLS)
	})

	go iris.ListenTLS(hostTLS, certFile.Name(), keyFile.Name())
	if ok := <-iris.Default.Available; !ok {
		t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
	}

	closeProxy := iris.Proxy("localhost:8080", "https://"+hostTLS)
	defer closeProxy()

	e := httptest.New(iris.Default, t, httptest.ExplicitURL(true))

	e.Request("GET", "http://"+host+":8080").Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)
	e.Request("GET", "https://"+hostTLS).Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)

}
开发者ID:kataras,项目名称:iris,代码行数:52,代码来源:http_test.go


示例17: TestContextHostString

// hoststring returns the full host, will return the HOST:IP
func TestContextHostString(t *testing.T) {
	iris.ResetDefault()
	iris.Default.Config.VHost = "0.0.0.0:8080"
	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write(ctx.HostString())
	})

	iris.Get("/wrong", func(ctx *iris.Context) {
		ctx.Write(ctx.HostString() + "w")
	})

	e := httptest.New(iris.Default, t)
	e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(iris.Default.Config.VHost)
	e.GET("/wrong").Expect().Body().NotEqual(iris.Default.Config.VHost)
}
开发者ID:kataras,项目名称:iris,代码行数:16,代码来源:context_test.go


示例18: TestMuxPathEscape

func TestMuxPathEscape(t *testing.T) {
	iris.ResetDefault()

	iris.Get("/details/:name", func(ctx *iris.Context) {
		name := ctx.Param("name")
		highlight := ctx.URLParam("highlight")
		ctx.Text(iris.StatusOK, fmt.Sprintf("name=%s,highlight=%s", name, highlight))
	})

	e := httptest.New(iris.Default, t)

	e.GET("/details/Sakamoto desu ga").
		WithQuery("highlight", "text").
		Expect().Status(iris.StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text")
}
开发者ID:kataras,项目名称:iris,代码行数:15,代码来源:http_test.go


示例19: TestContextVirtualHostName

// VirtualHostname returns the hostname only,
// if the host starts with 127.0.0.1 or localhost it gives the registered hostname part of the listening addr
func TestContextVirtualHostName(t *testing.T) {
	iris.ResetDefault()
	vhost := "mycustomvirtualname.com"
	iris.Default.Config.VHost = vhost + ":8080"
	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write(ctx.VirtualHostname())
	})

	iris.Get("/wrong", func(ctx *iris.Context) {
		ctx.Write(ctx.VirtualHostname() + "w")
	})

	e := httptest.New(iris.Default, t)
	e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(vhost)
	e.GET("/wrong").Expect().Body().NotEqual(vhost)
}
开发者ID:kataras,项目名称:iris,代码行数:18,代码来源:context_test.go


示例20: TestContextParams

// White-box testing *
func TestContextParams(t *testing.T) {
	context := &iris.Context{RequestCtx: &fasthttp.RequestCtx{}}
	params := pathParameters{
		pathParameter{Key: "testkey", Value: "testvalue"},
		pathParameter{Key: "testkey2", Value: "testvalue2"},
		pathParameter{Key: "id", Value: "3"},
		pathParameter{Key: "bigint", Value: "548921854390354"},
	}
	for _, p := range params {
		context.Set(p.Key, p.Value)
	}

	if v := context.Param(params[0].Key); v != params[0].Value {
		t.Fatalf("Expecting parameter value to be %s but we got %s", params[0].Value, context.Param("testkey"))
	}
	if v := context.Param(params[1].Key); v != params[1].Value {
		t.Fatalf("Expecting parameter value to be %s but we got %s", params[1].Value, context.Param("testkey2"))
	}

	if context.ParamsLen() != len(params) {
		t.Fatalf("Expecting to have %d parameters but we got %d", len(params), context.ParamsLen())
	}

	if vi, err := context.ParamInt(params[2].Key); err != nil {
		t.Fatalf("Unexpecting error on context's ParamInt while trying to get the integer of the %s", params[2].Value)
	} else if vi != 3 {
		t.Fatalf("Expecting to receive %d but we got %d", 3, vi)
	}

	if vi, err := context.ParamInt64(params[3].Key); err != nil {
		t.Fatalf("Unexpecting error on context's ParamInt while trying to get the integer of the %s", params[2].Value)
	} else if vi != 548921854390354 {
		t.Fatalf("Expecting to receive %d but we got %d", 548921854390354, vi)
	}

	// end-to-end test now, note that we will not test the whole mux here, this happens on http_test.go

	iris.ResetDefault()
	expectedParamsStr := "param1=myparam1,param2=myparam2,param3=myparam3afterstatic,anything=/andhere/anything/you/like"
	iris.Get("/path/:param1/:param2/staticpath/:param3/*anything", func(ctx *iris.Context) {
		paramsStr := ctx.ParamsSentence()
		ctx.Write(paramsStr)
	})

	httptest.New(iris.Default, t).GET("/path/myparam1/myparam2/staticpath/myparam3afterstatic/andhere/anything/you/like").Expect().Status(iris.StatusOK).Body().Equal(expectedParamsStr)

}
开发者ID:kataras,项目名称:iris,代码行数:48,代码来源:context_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang iris.Context类代码示例发布时间:2022-05-23
下一篇:
Golang iris.Get函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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