Example #1
0
func (s *SimpleService) GetMostPopular(r *http.Request) (int, interface{}, error) {
	resourceType := mux.Vars(r)["resourceType"]
	section := mux.Vars(r)["section"]
	timeframe := web.GetUInt64Var(r, "timeframe")
	res, err := s.client.GetMostPopular(resourceType, section, uint(timeframe))
	if err != nil {
		return http.StatusInternalServerError, nil, &jsonErr{err.Error()}
	}
	return http.StatusOK, res, nil
}
Example #2
0
func (s *RPCService) GetMostPopularJSON(ctx context.Context, r *http.Request) (int, interface{}, error) {
	res, err := s.GetMostPopular(
		ctx,
		&MostPopularRequest{
			web.Vars(r)["resourceType"],
			web.Vars(r)["section"],
			uint32(web.GetUInt64Var(r, "timeframe")),
		})
	if err != nil {
		return http.StatusInternalServerError, nil, err
	}
	return http.StatusOK, res.Result, nil
}
Example #3
0
func TestGetUInt64Var(t *testing.T) {

	tests := []struct {
		givenURL   string
		givenRoute string

		want uint64
	}{
		{
			"/blah/123",
			"/blah/{key}",
			123,
		},
		{
			"/blah/adsf",
			"/blah/{key}",
			0,
		},
		{
			"/blah?key=123",
			"/blah",
			123,
		},
		{
			"/blah?key=abc",
			"/blah",
			0,
		},
	}

	for _, test := range tests {
		route := mux.NewRouter()
		route.HandleFunc(test.givenRoute, func(w http.ResponseWriter, r *http.Request) {
			web.SetRouteVars(r, mux.Vars(r))
			got := web.GetUInt64Var(r, "key")
			if got != test.want {
				t.Errorf("got int of %#v, expected %#v", got, test.want)
			}
		})

		r, _ := http.NewRequest("GET", test.givenURL, nil)
		route.ServeHTTP(httptest.NewRecorder(), r)
	}
}