-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_upload_test.go
102 lines (98 loc) · 2.33 KB
/
cmd_upload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/urfave/cli/v2"
)
func TestCmdUploadCreate(t *testing.T) {
got := testWithServer(
func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/2/paper/docs/create":
h := r.Header.Get("Dropbox-API-Arg")
var v map[string]interface{}
err := json.NewDecoder(strings.NewReader(h)).Decode(&v)
if err != nil {
t.Fatal(err)
}
if _, ok := v["doc_id"]; ok {
t.Fatal("bad request")
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if string(b) != "hello world" {
t.Fatal("bad request")
}
fmt.Fprintln(w, `{"doc_id":"xxx"}`)
return
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
},
func(app *cli.App) {
app.Metadata["stdin"] = strings.NewReader("hello world")
app.Run([]string{"dboxpaper", "upload"})
},
)
want := "xxx\n"
if got != want {
t.Fatalf("want %v but got %v", want, got)
}
}
func TestCmdUploadUpdate(t *testing.T) {
got := testWithServer(
func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/2/paper/docs/get_metadata":
var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatal(err)
}
if v["doc_id"].(string) != "xxx" {
t.Fatal("bad request")
}
fmt.Fprintln(w, `{"doc_id":"xxx","revision":123}`)
return
case "/2/paper/docs/update":
h := r.Header.Get("Dropbox-API-Arg")
var v map[string]interface{}
err := json.NewDecoder(strings.NewReader(h)).Decode(&v)
if err != nil {
t.Fatal(err)
}
if v["doc_id"].(string) != "xxx" {
t.Fatal("bad request")
}
if int64(v["revision"].(float64)) != 123 {
t.Fatal("bad request")
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if string(b) != "good morning" {
t.Fatal("bad request")
}
fmt.Fprintln(w, `{"doc_id":"xxx"}`)
return
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
},
func(app *cli.App) {
app.Metadata["stdin"] = strings.NewReader("good morning")
app.Run([]string{"dboxpaper", "upload", "xxx"})
},
)
want := "xxx\n"
if got != want {
t.Fatalf("want %v but got %v", want, got)
}
}