コード例 #1
0
ファイル: denco_bench_test.go プロジェクト: colegion/contrib
func init() {
	// Initialize routers.
	dencoMux := denco.NewMux()
	httprtrH = httprouter.New()
	routingH = NewRouter()

	// Allocate and initialize lists of handlers.
	dencoList := []denco.Handler{}
	routingList := Routes{}
	for _, route := range githubAPI {
		// Add a route to denco router.
		dencoList = append(
			dencoList, dencoMux.Handler(route.method, route.pattern, testHandlerFuncDenco),
		)

		// Add a route to httprouter.
		httprtrH.Handle(route.method, route.pattern, testHandlerFuncHTTPRouter)

		// Add a route to routing.
		routingList = append(
			routingList, Do(route.method, route.pattern, testHandlerFunc),
		)
	}

	// create http.handler-s to be used by http.ListenAndServe.
	var err error
	dencoH, err = dencoMux.Build(dencoList)
	if err != nil {
		log.Fatal(err)
	}
	err = routingH.Handle(routingList).Build()
	if err != nil {
		log.Fatal(err)
	}
}
コード例 #2
0
ファイル: server_test.go プロジェクト: Cl0udPhish/go-swagger
func TestNotFound(t *testing.T) {
	mux := denco.NewMux()
	handler, err := mux.Build([]denco.Handler{})
	if err != nil {
		t.Fatal(err)
	}
	server := httptest.NewServer(handler)
	defer server.Close()

	origNotFound := denco.NotFound
	defer func() {
		denco.NotFound = origNotFound
	}()
	denco.NotFound = func(w http.ResponseWriter, r *http.Request, params denco.Params) {
		w.WriteHeader(http.StatusServiceUnavailable)
		fmt.Fprintf(w, "method: %s, path: %s, params: %v", r.Method, r.URL.Path, params)
	}
	res, err := http.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Fatal(err)
	}
	actual := string(body)
	expected := "method: GET, path: /, params: []"
	if res.StatusCode != http.StatusServiceUnavailable || actual != expected {
		t.Errorf(`GET "/" => %#v %#v, want %#v %#v`, res.StatusCode, actual, http.StatusServiceUnavailable, expected)
	}
}
コード例 #3
0
func loadDencoSingle(method, path string, handle denco.HandlerFunc) http.Handler {
	mux := denco.NewMux()
	handler, err := mux.Build([]denco.Handler{{method, path, handle}})
	if err != nil {
		panic(err)
	}
	return handler
}
コード例 #4
0
ファイル: server_test.go プロジェクト: Cl0udPhish/go-swagger
func TestMux(t *testing.T) {
	mux := denco.NewMux()
	handler, err := mux.Build([]denco.Handler{
		mux.GET("/", testHandlerFunc),
		mux.GET("/user/:name", testHandlerFunc),
		mux.POST("/user/:name", testHandlerFunc),
		mux.HEAD("/user/:name", testHandlerFunc),
		mux.PUT("/user/:name", testHandlerFunc),
		mux.Handler("GET", "/user/handler", testHandlerFunc),
		mux.Handler("POST", "/user/handler", testHandlerFunc),
		{"PUT", "/user/inference", testHandlerFunc},
	})
	if err != nil {
		t.Fatal(err)
	}
	server := httptest.NewServer(handler)
	defer server.Close()

	for _, v := range []struct {
		status                 int
		method, path, expected string
	}{
		{200, "GET", "/", "method: GET, path: /, params: []"},
		{200, "GET", "/user/alice", "method: GET, path: /user/alice, params: [{name alice}]"},
		{200, "POST", "/user/bob", "method: POST, path: /user/bob, params: [{name bob}]"},
		{200, "HEAD", "/user/alice", ""},
		{200, "PUT", "/user/bob", "method: PUT, path: /user/bob, params: [{name bob}]"},
		{404, "POST", "/", "404 page not found\n"},
		{404, "GET", "/unknown", "404 page not found\n"},
		{404, "POST", "/user/alice/1", "404 page not found\n"},
		{200, "GET", "/user/handler", "method: GET, path: /user/handler, params: []"},
		{200, "POST", "/user/handler", "method: POST, path: /user/handler, params: []"},
		{200, "PUT", "/user/inference", "method: PUT, path: /user/inference, params: []"},
	} {
		req, err := http.NewRequest(v.method, server.URL+v.path, nil)
		if err != nil {
			t.Error(err)
			continue
		}
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Error(err)
			continue
		}
		defer res.Body.Close()
		body, err := ioutil.ReadAll(res.Body)
		if err != nil {
			t.Error(err)
			continue
		}
		actual := string(body)
		expected := v.expected
		if res.StatusCode != v.status || actual != expected {
			t.Errorf(`%s "%s" => %#v %#v, want %#v %#v`, v.method, v.path, res.StatusCode, actual, v.status, expected)
		}
	}
}
コード例 #5
0
func loadDenco(routes []route) http.Handler {
	mux := denco.NewMux()
	handlers := make([]denco.Handler, len(routes))
	for i, route := range routes {
		handlers[i] = mux.Handler(route.method, route.path, dencoHandle)
	}
	handler, err := mux.Build(handlers)
	if err != nil {
		panic(err)
	}
	return handler
}
コード例 #6
0
ファイル: app.go プロジェクト: MStoykov/go-swagger
// ServeAPI serves this api
func ServeAPI() error {
	mux := denco.NewMux()

	routes := []denco.Handler{
		mux.GET("/pets", handlers.GetPets),
		mux.POST("/pets", handlers.CreatePet),
		mux.GET("/pets/:id", handlers.GetPetByID),
		mux.PUT("/pets/:id", handlers.UpdatePet),
		mux.Handler("DELETE", "/pets/:id", handlers.DeletePet),
		mux.GET("/orders/:id", handlers.GetOrderDetails),
		mux.POST("/orders", handlers.CreateOrder),
		mux.PUT("/orders/:id", handlers.UpdateOrder),
		mux.Handler("DELETE", "/orders/:id", handlers.CancelOrder),
	}
	handler, err := mux.Build(routes)
	if err != nil {
		return err
	}
	return http.ListenAndServe(":8000", handler)
}
コード例 #7
0
func startDenco() {
	mux := denco.NewMux()
	handler, _ := mux.Build([]denco.Handler{mux.GET("/hello", denco.HandlerFunc(dencoHandler))})
	http.ListenAndServe(":"+strconv.Itoa(port), handler)
}