예제 #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
	})
}