func TestTimeout(t *testing.T) { to := newTimeout(1 * time.Second) pipe := xrest.NewPipeline() pipe.Plug(to) pipe.Plug(xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { select { case <-ctx.Done(): case <-time.After(to.Duration + time.Second): t.Error("Shoud timeout\n") } })) pipe.HTTPHandler().ServeHTTP(nil, nil) to = newTimeout(0) pipe = xrest.NewPipeline() pipe.Plug(to) pipe.Plug(xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { select { case <-ctx.Done(): t.Error("Shoud not timeout\n") case <-time.After(time.Second): } })) pipe.HTTPHandler().ServeHTTP(nil, nil) }
func TestRouter(t *testing.T) { router := New() sr := router.SubRouter("/api") auth := sr.SubRouter("/auth") noauth := sr.SubRouter("/noauth") authVals := []string{"Hello1", "Hello2", "Hello3"} for _, val := range authVals { auth.Plug(newTestPlug(val)) } noauthVals := []string{"World1", "World2", "World3"} for _, val := range noauthVals { noauth.Plug(newTestPlug(val)) } auth.Get("/files", xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { assert.EqualValues(t, authVals, ctx.Value(&ctxTestPlug)) })) noauth.Post("/login", xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { assert.EqualValues(t, noauthVals, ctx.Value(&ctxTestPlug)) })) authReq, _ := http.NewRequest("GET", "/api/auth/files", nil) noauthReq, _ := http.NewRequest("POST", "/api/noauth/login", nil) pipe := xrest.NewPipeline().Plug(router) pipe.HTTPHandler().ServeHTTP(nil, authReq) pipe.HTTPHandler().ServeHTTP(nil, noauthReq) }
func main() { p := xrest.NewPipeline() p.Plug(xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { // fmt.Println("Hello") })) start := time.Now() p.HTTPHandler().ServeHTTP(nil, nil) fmt.Println(time.Now().Sub(start)) }
func (r *Router) subRouter(pre *SubRouter, prefix string) *SubRouter { var plugs []xrest.Plugger if pre != nil { prefix = filepath.Join(pre.prefix, prefix) plugs = r.subs[pre.prefix].Plugs() } sub := &SubRouter{ prefix: filepath.Join("/", prefix), father: r, } r.subs[sub.prefix] = xrest.NewPipeline().Plug(plugs...) return sub }
func TestJSONDecodeIntegration(t *testing.T) { pipe := xrest.NewPipeline() origin := testPayload{ Name: "alex", Age: 27, } pipe.Plug( New(nil), newTestPlug(t, origin), newTestPlug(t, origin), newTestPlug(t, origin), newTestPlug(t, origin), ) var buf bytes.Buffer assert.Nil(t, json.NewEncoder(&buf).Encode(&origin)) r, _ := http.NewRequest("POST", "/api/test", &buf) pipe.HTTPHandler().ServeHTTP(nil, r) }
func TestPlug(t *testing.T) { p := New(nil) pipe := xrest.NewPipeline().Plug(p) pipe.SetHandler( xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { var d struct { Name string Age int } err := Decode(ctx, &d) assert.Nil(t, err) assert.Equal(t, d.Name, "Alex") assert.Equal(t, d.Age, 27) }), ) dogPayload := `{"name": "Alex", "age": 27}` r, _ := http.NewRequest("GET", "/", bytes.NewBufferString(dogPayload)) pipe.HTTPHandler().ServeHTTP(nil, r) }
func main() { p := xrest.NewPipeline() p.Plug(limit.New(1, time.Second)) p.Plug(close.New(func(r *http.Request) { fmt.Printf("CLOSED: %#v\n", r.RemoteAddr) })) p.Plug( static.New( static.Dir("./static"), static.Prefix("public"), ), ) p.Plug(body.New(func(r *http.Request, err error) { fmt.Println("Error: ", err) })) p.Plug(newRouter()) http.ListenAndServe(":8080", p.HTTPHandler()) }
func TestClose(t *testing.T) { close := New(nil) dur := 2 * time.Second pipe := xrest.NewPipeline() pipe.Plug(close) pipe.Plug(xrest.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { select { case <-ctx.Done(): case <-time.After(dur + time.Second): t.Error("Should cancel\n") } })) ts := httptest.NewServer(pipe.HTTPHandler()) defer ts.Close() go http.Get(ts.URL) time.Sleep(dur - time.Second) ts.CloseClientConnections() }
func TestStatic(t *testing.T) { s := New( Dir("test"), Prefix("/public"), ) for i := range testCases { c := testCases[i] pipe := xrest.NewPipeline() pipe.Plug(s) pipe.Plug( xrest.HandlerFunc( func(ctx context.Context, res http.ResponseWriter, req *http.Request) { if c.exists { t.Error("should not be here\n") return } t.Error("should be here\n") }, ), ) server := httptest.NewServer(pipe.HTTPHandler()) res, err := http.Get(server.URL + "/public/" + c.file) fmt.Println(err) assert.Nil(t, err) bsa, err := ioutil.ReadAll(res.Body) assert.Nil(t, err) res.Body.Close() if c.exists { bsb, err := ioutil.ReadFile("test/" + c.file) assert.Nil(t, err) assert.True(t, equalByteSlice(bsa, bsb)) } server.Close() } }
func TestLimiter(t *testing.T) { limiter := New(1, time.Second) pipe := xrest.NewPipeline() pipe.Plug(limiter) ts := httptest.NewServer(pipe.HTTPHandler()) defer ts.Close() res, err := http.Get(ts.URL) assert.Nil(t, err) assert.Equal(t, res.StatusCode, http.StatusOK) for i := 0; i < 10; i++ { res, err = http.Get(ts.URL) assert.Nil(t, err) assert.NotEmpty(t, res.StatusCode, http.StatusOK) // body, err := ioutil.ReadAll(res.Body) // // fmt.Println(">>>>", err, string(body)) } }
func TestI18n(t *testing.T) { i18nPlug, err := New("./locales", FindLangDefault) if err != nil { t.Fatal(err) } var zhCN bool t.Logf("Test en-US\n") t.Logf("Test lang from parameter\n") r1, err := http.NewRequest("GET", "http://xrest.org/api/hello?lang=en-US", nil) if err != nil { t.Fatal(err) } xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r1) t.Logf("Test lang from cookie\n") r2, err := http.NewRequest("GET", "http://xrest.org/api/hello", nil) if err != nil { t.Fatal(err) } r2.AddCookie(&http.Cookie{ Name: "lang", Value: "en-US", }) xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r2) t.Logf("Test lang from http header\n") r3, err := http.NewRequest("GET", "http://xrest.org/api/hello", nil) r3.Header.Set("Accept-Language", "en-US") xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r3) // ====================================================================== zhCN = true t.Logf("Test zh-CN\n") t.Logf("Test lang from parameter\n") r4, err := http.NewRequest("GET", "http://xrest.org/api/hello?lang=zh-CN", nil) if err != nil { t.Fatal(err) } xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r4) t.Logf("Test lang from cookie\n") r5, err := http.NewRequest("GET", "http://xrest.org/api/hello", nil) if err != nil { t.Fatal(err) } r5.AddCookie(&http.Cookie{ Name: "lang", Value: "zh-CN", }) xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r5) t.Logf("Test lang from http header\n") r6, err := http.NewRequest("GET", "http://xrest.org/api/hello", nil) r6.Header.Set("Accept-Language", "zh-CN") xrest.NewPipeline(). Plug(i18nPlug, newTestPlug(zhCN, t)). Handler().ServeHTTP(context.Background(), nil, r6) }