当前位置: 首页>>代码示例>>Golang>>正文


Golang echo.NewResponse函数代码示例

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


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

示例1: TestIsAjax

func TestIsAjax(t *testing.T) {
	e := echo.New()
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Error(err)
	}
	res := httptest.NewRecorder()
	ctx := echo.NewContext(req, echo.NewResponse(res), e)

	if ok := IsAjax(ctx); ok {
		t.Errorf("expected false got %v", ok)
	}

	req1, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Error(err)
	}
	req1.Header.Set("X-Requested-With", "XMLHttpRequest")
	res1 := httptest.NewRecorder()
	ctx1 := echo.NewContext(req1, echo.NewResponse(res1), e)

	if ok := IsAjax(ctx1); !ok {
		t.Errorf("expected true got %v", ok)
	}
}
开发者ID:jwulf,项目名称:zedlist,代码行数:25,代码来源:utils_test.go

示例2: TestGzip

func TestGzip(t *testing.T) {
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	h := func(c *echo.Context) error {
		c.Response().Write([]byte("test")) // Content-Type sniffing
		return nil
	}

	// Skip if no Accept-Encoding header
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "test", rec.Body.String())

	req, _ = http.NewRequest(echo.GET, "/", nil)
	req.Header.Set(echo.AcceptEncoding, "gzip")
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), echo.New())

	// Gzip
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
	assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
	r, err := gzip.NewReader(rec.Body)
	defer r.Close()
	if assert.NoError(t, err) {
		buf := new(bytes.Buffer)
		buf.ReadFrom(r)
		assert.Equal(t, "test", buf.String())
	}
}
开发者ID:alkhatib,项目名称:echo,代码行数:32,代码来源:compress_test.go

示例3: TestLogger

func TestLogger(t *testing.T) {
	// Note: Just for the test coverage, not a real test.
	e := echo.New()
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)

	// With X-Real-IP
	req.Header.Add(echo.XRealIP, "127.0.0.1")
	h := func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)

	// With X-Forwarded-For
	req.Header.Del(echo.XRealIP)
	req.Header.Add(echo.XForwardedFor, "127.0.0.1")
	h = func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)

	// Status 2xx
	h = func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)

	// Status 3xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusTemporaryRedirect, "test")
	}
	Logger()(h)(c)

	// Status 4xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusNotFound, "test")
	}
	Logger()(h)(c)

	// Status 5xx with empty path
	req, _ = http.NewRequest(echo.GET, "", nil)
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return errors.New("error")
	}
	Logger()(h)(c)
}
开发者ID:jameswei,项目名称:kingtask,代码行数:53,代码来源:logger_test.go

示例4: TestStripTrailingSlash

func TestStripTrailingSlash(t *testing.T) {
	req, _ := http.NewRequest(echo.GET, "/users/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	StripTrailingSlash()(c)
	assert.Equal(t, "/users", c.Request().URL.Path)
}
开发者ID:alkhatib,项目名称:echo,代码行数:7,代码来源:slash_test.go

示例5: TestListRegions

func TestListRegions(t *testing.T) {
	for _, test := range []struct {
		p    mockProvisioner
		err  error
		code int
		body string
	}{
		{
			createProvisioner([]provisioner.Region{}),
			nil,
			200,
			"[]",
		},
		{
			createProvisioner([]provisioner.Region{{"name", "description", "country"}}),
			nil,
			200,
			"[{\"name\":\"name\",\"description\":\"description\",\"country\":\"country\"}]",
		},
	} {
		s := &yovpnServer{test.p}

		e := echo.New()
		req := mustNewRequest("GET", "/regions", nil)
		w := httptest.NewRecorder()
		c := echo.NewContext(req, echo.NewResponse(w, e), e)
		err := s.getRegions(c)

		assert.Equal(t, test.err, err)
		assert.Equal(t, test.code, w.Code)
		assert.Equal(t, test.body, w.Body.String())
	}
}
开发者ID:xperimental,项目名称:yovpn,代码行数:33,代码来源:web_test.go

示例6: TestLoggerIPAddress

func TestLoggerIPAddress(t *testing.T) {
	e := echo.New()
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec, e), e)
	buf := new(bytes.Buffer)
	e.Logger().SetOutput(buf)
	ip := "127.0.0.1"
	h := func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}

	mw := Logger()

	// With X-Real-IP
	req.Header.Add(echo.XRealIP, ip)
	mw(h)(c)
	assert.Contains(t, buf.String(), ip)

	// With X-Forwarded-For
	buf.Reset()
	req.Header.Del(echo.XRealIP)
	req.Header.Add(echo.XForwardedFor, ip)
	mw(h)(c)
	assert.Contains(t, buf.String(), ip)

	// with req.RemoteAddr
	buf.Reset()
	mw(h)(c)
	assert.Contains(t, buf.String(), ip)
}
开发者ID:prabirshrestha,项目名称:echo,代码行数:31,代码来源:logger_test.go

示例7: TestRedirectToSlash

func TestRedirectToSlash(t *testing.T) {
	req, _ := http.NewRequest(echo.GET, "/users", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
	assert.Equal(t, http.StatusTemporaryRedirect, rec.Code)
	assert.Equal(t, "/users/", c.Response().Header().Get("Location"))
}
开发者ID:alkhatib,项目名称:echo,代码行数:8,代码来源:slash_test.go

示例8: TestSleep

func TestSleep(t *testing.T) {
	req, _ := http.NewRequest(echo.POST, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	start := time.Now()
	Sleep(1)(c)
	duration := time.Now().Sub(start)
	assert.True(t, duration > time.Second*1)

	req, _ = http.NewRequest(echo.GET, "/", nil)
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
	start = time.Now()
	Sleep(1)(c)
	duration = time.Now().Sub(start)
	assert.True(t, duration < 1*time.Second)
}
开发者ID:binarydud,项目名称:jumphash,代码行数:17,代码来源:server_test.go

示例9: TestData

func TestData(t *testing.T) {
	sampleData := []struct {
		key string
		val interface{}
	}{
		{"one", 1},
		{"two", "two"},
		{"three", "three"},
	}
	e := echo.New()
	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Error(err)
	}
	res := httptest.NewRecorder()

	// Create the context on which we will be messing with
	ctx := echo.NewContext(req, echo.NewResponse(res), e)

	//
	//	SetData
	//
	for _, v := range sampleData {
		SetData(ctx, v.key, v.val)
	}

	//
	//	GetData
	//
	data, ok := GetData(ctx).(Data)
	if !ok {
		t.Error("kaboom")
	}

	// Check the Data methods
	d := data.Get(sampleData[0].key)
	if d == nil {
		t.Errorf("expected %v got nil instead", sampleData[0].val)
	}

	data.Set("hello", "world")
	if h := data.Get("hello"); h != nil {
		if h.(string) != "world" {
			t.Errorf("expected world got %v", h)
		}
	}

	//
	//	GetLang
	//
	SetData(ctx, settings.LangDataKey, "en")

	lang := GetLang(ctx)
	if lang != "en" {
		t.Errorf("expected en got %s", lang)
	}
}
开发者ID:jwulf,项目名称:zedlist,代码行数:57,代码来源:utils_test.go

示例10: TestHash

func TestHash(t *testing.T) {
	e := echo.New()
	req, _ := http.NewRequest(echo.POST, "/", strings.NewReader("password=angryMonkey"))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded;")
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)
	Hash(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "ZEHhWB65gUlzdVwtDQArEyx+KVLzp/aTaRaPlBzYRIFj6vjFdqEb0Q5B8zVKCZ0vKbZPZklJz0Fd7su2A+gf7Q==", rec.Body.String())
}
开发者ID:binarydud,项目名称:jumphash,代码行数:10,代码来源:server_test.go

示例11: TestJWTAuth

func TestJWTAuth(t *testing.T) {
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	key := []byte("key")
	fn := func(kid string, method jwt.SigningMethod) ([]byte, error) {
		return key, nil
	}
	ja := JWTAuth(fn)
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims["foo"] = "bar"
	token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
	ts, err := token.SignedString(key)
	assert.NoError(t, err)

	// Valid credentials
	auth := Bearer + " " + ts
	req.Header.Set(echo.Authorization, auth)
	assert.NoError(t, ja(c))

	//---------------------
	// Invalid credentials
	//---------------------

	// Expired token
	token.Claims["exp"] = time.Now().Add(-time.Second).Unix()
	ts, err = token.SignedString(key)
	assert.NoError(t, err)
	auth = Bearer + " " + ts
	req.Header.Set(echo.Authorization, auth)
	he := ja(c).(*echo.HTTPError)
	assert.Equal(t, http.StatusUnauthorized, he.Code())

	// Empty Authorization header
	req.Header.Set(echo.Authorization, "")
	he = ja(c).(*echo.HTTPError)
	assert.Equal(t, http.StatusBadRequest, he.Code())

	// Invalid Authorization header
	auth = "token"
	req.Header.Set(echo.Authorization, auth)
	he = ja(c).(*echo.HTTPError)
	assert.Equal(t, http.StatusBadRequest, he.Code())

	// Invalid scheme
	auth = "Bear token"
	req.Header.Set(echo.Authorization, auth)
	he = ja(c).(*echo.HTTPError)
	assert.Equal(t, http.StatusBadRequest, he.Code())

	// WebSocket
	c.Request().Header.Set(echo.Upgrade, echo.WebSocket)
	assert.NoError(t, ja(c))
}
开发者ID:alkhatib,项目名称:echo,代码行数:54,代码来源:auth_test.go

示例12: TestRecover

func TestRecover(t *testing.T) {
	e := echo.New()
	e.SetDebug(true)
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)
	h := func(c *echo.Context) error {
		panic("test")
	}
	Recover()(h)(c)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
	assert.Contains(t, rec.Body.String(), "panic recover")
}
开发者ID:jameswei,项目名称:kingtask,代码行数:13,代码来源:recover_test.go

示例13: TestShutdown

func TestShutdown(t *testing.T) {
	sigchan := make(chan os.Signal, 1)
	signal.Notify(sigchan, os.Interrupt, os.Kill)
	e := echo.New()
	req, _ := http.NewRequest(echo.POST, "/", strings.NewReader("command=shutdown"))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded;")
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)
	Shutdown()(c)
	s := <-sigchan
	assert.Equal(t, os.Interrupt, s)

}
开发者ID:binarydud,项目名称:jumphash,代码行数:13,代码来源:server_test.go

示例14: TestLogger

func TestLogger(t *testing.T) {
	e := echo.New()
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)
	b := &bytes.Buffer{}

	// Status 2xx
	h := func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger(b)(h)(c)

	// Status 3xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusTemporaryRedirect, "test")
	}
	Logger(b)(h)(c)

	// Status 4xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusNotFound, "test")
	}
	Logger(b)(h)(c)

	// Status 5xx with empty path
	req, _ = http.NewRequest(echo.GET, "", nil)
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), e)
	h = func(c *echo.Context) error {
		return errors.New("error")
	}
	Logger(b)(h)(c)
}
开发者ID:lestrrat,项目名称:go-echo-apachelog,代码行数:38,代码来源:apachelog_test.go

示例15: TestLogger

func TestLogger(t *testing.T) {
	// Note: Just for the test coverage, not a real test.
	e := echo.New()
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec, e), e)

	// Status 2xx
	h := func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Logger()(h)(c)

	// Status 3xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec, e), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusTemporaryRedirect, "test")
	}
	Logger()(h)(c)

	// Status 4xx
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec, e), e)
	h = func(c *echo.Context) error {
		return c.String(http.StatusNotFound, "test")
	}
	Logger()(h)(c)

	// Status 5xx with empty path
	req, _ = http.NewRequest(echo.GET, "", nil)
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec, e), e)
	h = func(c *echo.Context) error {
		return errors.New("error")
	}
	Logger()(h)(c)
}
开发者ID:prabirshrestha,项目名称:echo,代码行数:38,代码来源:logger_test.go


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