예제 #1
0
파일: common.go 프로젝트: iwarsong/bearded
func takeApi(fn func(*cli.Context, *client.Client, Timeout)) func(*cli.Context) {
	return func(ctx *cli.Context) {
		timeout := func() context.Context {
			return utils.JustTimeout(context.Background(), time.Duration(ctx.Int("api-timeout"))*time.Second)
		}
		api := client.NewClient(ctx.String("api-addr"), nil)
		if ctx.GlobalBool("debug") {
			api.Debug = true
		}
		fn(ctx, api, timeout)
	}
}
예제 #2
0
func TestFilesDownload(t *testing.T) {
	bg := context.Background()
	handlerMock := func(res http.ResponseWriter, req *http.Request) {
		switch req.URL.String() {
		case "/v1/files/1/download":
			res.Header().Add("Content-Type", "octet/stream")
			res.Write([]byte("data 1"))
		case "/v1/files/2/download":
			http.NotFound(res, req)
		}
	}
	s := httptest.NewServer(http.HandlerFunc(handlerMock))
	defer s.Close()
	baseUrl, _ := url.Parse(s.URL)

	client := &Client{
		BaseURL: baseUrl,
		client:  http.DefaultClient,
		Debug:   true,
	}
	f := &FilesService{client: client}

	{
		buf, err := f.Download(utils.JustTimeout(bg, time.Millisecond*10), "1")
		require.NoError(t, err)
		require.NotNil(t, buf)
		data, err := ioutil.ReadAll(buf)
		require.NoError(t, err)
		require.Equal(t, "data 1", string(data))
	}
	{
		_, err := f.Download(utils.JustTimeout(bg, time.Millisecond*10), "2")
		require.Error(t, err)
	}

}