Пример #1
0
func newRouter() *router.Router {
	r := router.New()
	r.Get("/api/hello/:name", xrest.HandlerFunc(helloRoute))
	r.Post("/api/hello/:name", xrest.HandlerFunc(helloRoute))
	r.Get("/api/slow", xrest.HandlerFunc(slowRoute))
	return r
}
Пример #2
0
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)
}
Пример #3
0
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)
}
Пример #4
0
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))
}
Пример #5
0
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)
}
Пример #6
0
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()
}
Пример #7
0
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()
	}
}
Пример #8
0
func fakeHandler(val string) xrest.Handler {
	return xrest.HandlerFunc(func(context.Context, http.ResponseWriter, *http.Request) {
		fakeHandlerValue = val
	})
}