本文整理匯總了Golang中github.com/guregu/kami.Reset函數的典型用法代碼示例。如果您正苦於以下問題:Golang Reset函數的具體用法?Golang Reset怎麽用?Golang Reset使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Reset函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGetNote
func TestGetNote(t *testing.T) {
assert := assert.New(t)
dbMap := initDb()
defer dbMap.Db.Close()
ctx := context.Background()
ctx = context.WithValue(ctx, "db", dbMap)
ctx = context.WithValue(ctx, "auth", &auth.AuthContext{})
dbMap.Insert(&model.Note{
Id: 0,
Title: "Test Title 1",
Content: "lorem ipsum dolor sit amet consetetur.",
OwnerId: 0,
CreatedAt: 1442284669000,
UpdatedAt: 1442292926000,
})
kami.Reset()
kami.Context = ctx
kami.Post("/api/notes/:noteId", GetNote)
server := httptest.NewServer(kami.Handler())
defer server.Close()
resp := request(t, server.URL+"/api/notes/1", http.StatusOK, nil)
assert.NotNil(resp)
note := resp.(map[string]interface{})
assert.Equal("Test Title 1", note["title"])
assert.Equal("lorem ipsum dolor sit amet consetetur.", note["content"])
assert.EqualValues(0, note["ownerId"])
assert.EqualValues(1442284669000, note["createdAt"])
assert.EqualValues(1442292926000, note["updatedAt"])
}
示例2: TestPanickingLogger
func TestPanickingLogger(t *testing.T) {
kami.Reset()
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
t.Log("log handler")
panic("test panic")
}
kami.PanicHandler = kami.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
t.Log("panic handler")
err := kami.Exception(ctx)
if err != "test panic" {
t.Error("unexpected exception:", err)
}
w.WriteHeader(500)
w.Write([]byte("error 500"))
})
kami.Post("/test", noop)
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/test", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 500 {
t.Error("should return HTTP 500", resp.Code, "≠", 500)
}
}
示例3: TestNotFound
func TestNotFound(t *testing.T) {
kami.Reset()
kami.Use("/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "ok", true)
})
kami.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ok, _ := ctx.Value("ok").(bool)
if !ok {
w.WriteHeader(500)
return
}
w.WriteHeader(420)
})
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/missing/hello", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 420 {
t.Error("should return HTTP 420", resp.Code, "≠", 420)
}
}
示例4: BenchmarkMiddleware5
func BenchmarkMiddleware5(b *testing.B) {
kami.Reset()
numbers := []int{1, 2, 3, 4, 5}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, n := range numbers {
if ctx.Value(n) != n {
w.WriteHeader(501)
return
}
}
})
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/test", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 200 {
panic(resp.Code)
}
}
}
示例5: TestDeleteNote
func TestDeleteNote(t *testing.T) {
assert := assert.New(t)
dbMap := initDb()
defer dbMap.Db.Close()
ctx := context.Background()
ctx = context.WithValue(ctx, "db", dbMap)
ctx = context.WithValue(ctx, "auth", &auth.AuthContext{})
dbMap.Insert(&model.Note{
Id: 0,
Title: "Test Title 1",
Content: "lorem ipsum dolor sit amet consetetur.",
OwnerId: 0,
CreatedAt: 1442284669000,
UpdatedAt: 1442292926000,
})
count, err := dbMap.SelectInt("SELECT COUNT(id) FROM notes")
assert.Nil(err)
assert.EqualValues(1, count)
kami.Reset()
kami.Context = ctx
kami.Post("/api/notes/:noteId", DeleteNote)
server := httptest.NewServer(kami.Handler())
defer server.Close()
request(t, server.URL+"/api/notes/1", http.StatusOK, nil)
count, err = dbMap.SelectInt("SELECT COUNT(id) FROM notes")
assert.Nil(err)
assert.EqualValues(0, count)
}
示例6: BenchmarkMiddleware5Afterware1
func BenchmarkMiddleware5Afterware1(b *testing.B) {
kami.Reset()
numbers := []int{1, 2, 3, 4, 5}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.After("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
for _, n := range numbers {
if ctx.Value(n) != n {
panic(n)
}
}
return ctx
})
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, n := range numbers {
if ctx.Value(n) != n {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
})
req, _ := http.NewRequest("GET", "/test", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
示例7: TestLoggerAndPanic
func TestLoggerAndPanic(t *testing.T) {
kami.Reset()
// test logger with panic
status := 0
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
status = w.Status()
}
kami.PanicHandler = kami.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
err := kami.Exception(ctx)
if err != "test panic" {
t.Error("unexpected exception:", err)
}
w.WriteHeader(http.StatusServiceUnavailable)
})
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
kami.Put("/ok", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
expectResponseCode(t, "POST", "/test", http.StatusServiceUnavailable)
if status != http.StatusServiceUnavailable {
t.Error("log handler received wrong status code", status, "≠", http.StatusServiceUnavailable)
}
// test loggers without panics
expectResponseCode(t, "PUT", "/ok", http.StatusOK)
if status != http.StatusOK {
t.Error("log handler received wrong status code", status, "≠", http.StatusOK)
}
}
示例8: TestMethodNotAllowedDefault
func TestMethodNotAllowedDefault(t *testing.T) {
kami.Reset()
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
expectResponseCode(t, "GET", "/test", http.StatusMethodNotAllowed)
}
示例9: BenchmarkParameter
func BenchmarkParameter(b *testing.B) {
kami.Reset()
kami.Get("/hello/:name", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
kami.Param(ctx, "name")
})
req, _ := http.NewRequest("GET", "/hello/bob", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
示例10: routeBench
func routeBench(b *testing.B, route string) {
kami.Reset()
kami.Use("/Z/", noopMW)
kami.After("/Z/", noopMW)
kami.Get(route, noop)
req, _ := http.NewRequest("GET", route, nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
示例11: BenchmarkStaticRoute
func BenchmarkStaticRoute(b *testing.B) {
kami.Reset()
kami.Get("/hello", noop)
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hello", nil)
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 200 {
panic(resp.Code)
}
}
}
示例12: TestLoggerAndPanic
func TestLoggerAndPanic(t *testing.T) {
kami.Reset()
// test logger with panic
status := 0
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
status = w.Status()
}
kami.PanicHandler = kami.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
err := kami.Exception(ctx)
if err != "test panic" {
t.Error("unexpected exception:", err)
}
w.WriteHeader(500)
w.Write([]byte("error 500"))
})
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
kami.Put("/ok", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/test", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 500 {
t.Error("should return HTTP 500", resp.Code, "≠", 500)
}
if status != 500 {
t.Error("should return HTTP 500", status, "≠", 500)
}
// test loggers without panics
resp = httptest.NewRecorder()
req, err = http.NewRequest("PUT", "/ok", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 200 {
t.Error("should return HTTP 200", resp.Code, "≠", 200)
}
if status != 200 {
t.Error("should return HTTP 200", status, "≠", 200)
}
}
示例13: BenchmarkParameter5
func BenchmarkParameter5(b *testing.B) {
kami.Reset()
kami.Get("/:a/:b/:c/:d/:e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, v := range []string{"a", "b", "c", "d", "e"} {
kami.Param(ctx, v)
}
})
req, _ := http.NewRequest("GET", "/a/b/c/d/e", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
示例14: TestNotFoundDefault
func TestNotFoundDefault(t *testing.T) {
kami.Reset()
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/missing/hello", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != 404 {
t.Error("should return HTTP 404", resp.Code, "≠", 404)
}
}
示例15: TestUpdateNote
func TestUpdateNote(t *testing.T) {
assert := assert.New(t)
dbMap := initDb()
defer dbMap.Db.Close()
ctx := context.Background()
ctx = context.WithValue(ctx, "db", dbMap)
ctx = context.WithValue(ctx, "auth", &auth.AuthContext{})
dbMap.Insert(&model.Note{
Id: 0,
Title: "Test Title 1",
Content: "lorem ipsum dolor sit amet consetetur.",
OwnerId: 0,
CreatedAt: 1442284669000,
UpdatedAt: 1442292926000,
})
kami.Reset()
kami.Context = ctx
kami.Post("/api/notes/:noteId", UpdateNote)
server := httptest.NewServer(kami.Handler())
defer server.Close()
resp := request(t, server.URL+"/api/notes/1", http.StatusOK, map[string]interface{}{
"title": "Test Title 2",
"content": "hoge piyo hoge piyo.",
"ownerId": 1,
})
assert.NotNil(resp)
note := resp.(map[string]interface{})
assert.Equal("Test Title 2", note["title"])
assert.Equal("hoge piyo hoge piyo.", note["content"])
assert.EqualValues(1, note["ownerId"])
assert.EqualValues(1442284669000, note["createdAt"])
count, err := dbMap.SelectInt("SELECT COUNT(id) FROM notes")
assert.Nil(err)
assert.EqualValues(1, count)
n := new(model.Note)
err = dbMap.SelectOne(n, "SELECT * FROM notes")
assert.Nil(err)
assert.Equal("Test Title 2", n.Title)
assert.Equal("hoge piyo hoge piyo.", n.Content)
assert.EqualValues(1, n.OwnerId)
assert.EqualValues(1442284669000, n.CreatedAt)
}