fix endpoints

This commit is contained in:
2023-03-23 18:44:14 +01:00
parent 3fb66e075d
commit b6dfafd1c1
8 changed files with 59 additions and 51 deletions

View File

@@ -135,11 +135,6 @@ func HandleResponse(c *cfg.Config, resp *req.Response) error {
// we expect a json response, extract the error, if any
r := Response{}
if err := json.Unmarshal([]byte(resp.String()), &r); err != nil {
// text output!
r.Message = resp.String()
}
if c.Debug {
trace := resp.Request.TraceInfo()
fmt.Println(trace.Blame())
@@ -147,6 +142,15 @@ func HandleResponse(c *cfg.Config, resp *req.Response) error {
fmt.Println(trace)
}
if !resp.IsSuccessState() {
return fmt.Errorf("bad response: %s", resp.Status)
}
if err := json.Unmarshal([]byte(resp.String()), &r); err != nil {
// text output!
r.Message = resp.String()
}
if !r.Success {
if len(r.Message) == 0 {
if resp.Err != nil {
@@ -200,7 +204,7 @@ func UploadFiles(w io.Writer, c *cfg.Config, args []string) error {
}
func List(w io.Writer, c *cfg.Config, args []string) error {
rq := Setup(c, "/list/")
rq := Setup(c, "/uploads")
params := &ListParams{Apicontext: c.Apicontext}
resp, err := rq.R.
@@ -245,7 +249,7 @@ func Describe(w io.Writer, c *cfg.Config, args []string) error {
id := args[0] // we describe only 1 object
rq := Setup(c, "/upload/"+id+"/")
rq := Setup(c, "/uploads/"+id+"/")
resp, err := rq.R.Get(rq.Url)
if err != nil {
@@ -266,7 +270,7 @@ func Download(w io.Writer, c *cfg.Config, args []string) error {
id := args[0]
rq := Setup(c, "/uploads/"+id+"/")
rq := Setup(c, "/uploads/"+id+"/file")
if !c.Silent {
// progres bar
@@ -289,6 +293,10 @@ func Download(w io.Writer, c *cfg.Config, args []string) error {
return err
}
if !resp.IsSuccessState() {
return fmt.Errorf("bad response: %s", resp.Status)
}
_, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
if err != nil {
os.Remove(id)

View File

@@ -33,7 +33,7 @@ import (
"testing"
)
const endpoint string = "http://localhost:8080/api/v1"
const endpoint string = "http://localhost:8080/v1"
type Unit struct {
name string
@@ -200,7 +200,7 @@ func TestList(t *testing.T) {
name: "list",
apikey: "token",
wantfail: false,
route: "/list/",
route: "/uploads",
sendcode: 200,
sendjson: listing,
files: []string{},
@@ -211,7 +211,7 @@ func TestList(t *testing.T) {
name: "list-catch-empty-json",
apikey: "token",
wantfail: true,
route: "/list/",
route: "/uploads",
sendcode: 404,
sendjson: "",
files: []string{},
@@ -221,7 +221,7 @@ func TestList(t *testing.T) {
name: "list-catch-no-access",
apikey: "token",
wantfail: true,
route: "/list/",
route: "/uploads",
sendcode: 503,
sendjson: listingnoaccess,
files: []string{},
@@ -261,7 +261,7 @@ func TestDescribe(t *testing.T) {
name: "describe",
apikey: "token",
wantfail: false,
route: "/upload/",
route: "/uploads/",
sendcode: 200,
sendjson: listing,
files: []string{"cc2c965a"},
@@ -272,7 +272,7 @@ func TestDescribe(t *testing.T) {
name: "describe-catch-empty-json",
apikey: "token",
wantfail: true,
route: "/upload/",
route: "/uploads/",
sendcode: 200,
sendjson: "",
files: []string{"cc2c965a"},
@@ -282,7 +282,7 @@ func TestDescribe(t *testing.T) {
name: "describe-catch-no-access",
apikey: "token",
wantfail: true,
route: "/upload/",
route: "/uploads/",
sendcode: 503,
sendjson: listingnoaccess,
files: []string{"cc2c965a"},
@@ -395,7 +395,7 @@ func TestDownload(t *testing.T) {
for _, unit := range tests {
var w bytes.Buffer
unit.route += unit.files[0] + "/"
unit.route += unit.files[0] + "/file"
Intercept(unit)
Check(t, unit, &w, Download(&w, conf, unit.files))