|
1
|
|
|
package devto |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"io/ioutil" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"reflect" |
|
8
|
|
|
"strings" |
|
9
|
|
|
"testing" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/VictorAvelar/devto-api-go/testdata" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
var ctx = context.Background() |
|
15
|
|
|
|
|
16
|
|
|
func TestArticlesResource_List(t *testing.T) { |
|
17
|
|
|
setup() |
|
18
|
|
|
defer teardown() |
|
19
|
|
|
cont, err := ioutil.ReadAll(strings.NewReader(testdata.ListResponse)) |
|
20
|
|
|
if err != nil { |
|
21
|
|
|
t.Error(err) |
|
22
|
|
|
} |
|
23
|
|
|
testMux.HandleFunc("/api/articles", func(w http.ResponseWriter, r *http.Request) { |
|
24
|
|
|
w.Header().Add("content-type", "application/json") |
|
25
|
|
|
w.WriteHeader(http.StatusOK) |
|
26
|
|
|
w.Write(cont) |
|
27
|
|
|
}) |
|
28
|
|
|
|
|
29
|
|
|
var ctx = context.Background() |
|
30
|
|
|
list, err := testClientPub.Articles.List(ctx, ArticleListOptions{}) |
|
31
|
|
|
if err != nil { |
|
32
|
|
|
t.Error(err) |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if len(list) != 3 { |
|
36
|
|
|
t.Errorf("not all articles where parsed") |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
for _, a := range list { |
|
40
|
|
|
if a.Title == "" { |
|
41
|
|
|
t.Error("parsing failed / empty titles") |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
func TestArticlesResource_ListWithQueryParams(t *testing.T) { |
|
47
|
|
|
setup() |
|
48
|
|
|
defer teardown() |
|
49
|
|
|
testMux.HandleFunc("/api/articles?page=1&state=fresh&tag=go&top=1&username=victoravelar", func(w http.ResponseWriter, r *http.Request) { |
|
50
|
|
|
if !strings.HasSuffix("username=victoravelar", r.URL.String()) { |
|
51
|
|
|
t.Error("url mismatch") |
|
52
|
|
|
} |
|
53
|
|
|
w.WriteHeader(http.StatusOK) |
|
54
|
|
|
}) |
|
55
|
|
|
|
|
56
|
|
|
q := ArticleListOptions{ |
|
57
|
|
|
Tags: "go", |
|
58
|
|
|
Username: "victoravelar", |
|
59
|
|
|
State: "fresh", |
|
60
|
|
|
Top: "1", |
|
61
|
|
|
Page: 1, |
|
62
|
|
|
} |
|
63
|
|
|
list, err := testClientPub.Articles.List(ctx, q) |
|
64
|
|
|
if err != nil { |
|
65
|
|
|
t.Error(err) |
|
66
|
|
|
} |
|
67
|
|
|
if len(list) != 0 { |
|
68
|
|
|
t.Error("response is unexpected") |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func TestArticlesResource_Find(t *testing.T) { |
|
73
|
|
|
setup() |
|
74
|
|
|
defer teardown() |
|
75
|
|
|
cont, err := ioutil.ReadAll(strings.NewReader(testdata.FindResponse)) |
|
76
|
|
|
if err != nil { |
|
77
|
|
|
t.Error(err) |
|
78
|
|
|
} |
|
79
|
|
|
testMux.HandleFunc("/api/articles/164198", func(w http.ResponseWriter, r *http.Request) { |
|
80
|
|
|
w.Header().Add("content-type", "application/json") |
|
81
|
|
|
w.WriteHeader(http.StatusOK) |
|
82
|
|
|
w.Write(cont) |
|
83
|
|
|
}) |
|
84
|
|
|
|
|
85
|
|
|
article, err := testClientPub.Articles.Find(ctx, 164198) |
|
86
|
|
|
if err != nil { |
|
87
|
|
|
t.Error(err) |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if article.ID != 164198 { |
|
91
|
|
|
t.Error("article returned is not the one requested") |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
func TestArticlesResource_New(t *testing.T) { |
|
96
|
|
|
setup() |
|
97
|
|
|
defer teardown() |
|
98
|
|
|
testMux.HandleFunc("/api/articles", func(w http.ResponseWriter, r *http.Request) { |
|
99
|
|
|
if r.Method != http.MethodPost { |
|
100
|
|
|
t.Error("invalid method for request") |
|
101
|
|
|
} |
|
102
|
|
|
cont, _ := ioutil.ReadAll(r.Body) |
|
103
|
|
|
w.Header().Add("content-type", "application/json") |
|
104
|
|
|
w.WriteHeader(http.StatusCreated) |
|
105
|
|
|
w.Write(cont) |
|
106
|
|
|
}) |
|
107
|
|
|
|
|
108
|
|
|
res, _ := testClientPro.Articles.New(ctx, Article{ |
|
109
|
|
|
TypeOf: "article", |
|
110
|
|
|
Title: "Demo article", |
|
111
|
|
|
Published: false, |
|
112
|
|
|
}) |
|
113
|
|
|
|
|
114
|
|
|
if res.Title != "Demo article" { |
|
115
|
|
|
t.Error("article parsing failed") |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
func TestArticlesResource_NewFailsWhenInsecure(t *testing.T) { |
|
120
|
|
|
setup() |
|
121
|
|
|
defer teardown() |
|
122
|
|
|
|
|
123
|
|
|
_, err := testClientPub.Articles.New(ctx, Article{ |
|
124
|
|
|
TypeOf: "article", |
|
125
|
|
|
Title: "Demo article", |
|
126
|
|
|
Published: false, |
|
127
|
|
|
}) |
|
128
|
|
|
|
|
129
|
|
|
if !reflect.DeepEqual(err, ErrProtectedEndpoint) { |
|
130
|
|
|
t.Error("auth check failed") |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
func TestArticlesResource_Update(t *testing.T) { |
|
135
|
|
|
setup() |
|
136
|
|
|
defer teardown() |
|
137
|
|
|
testMux.HandleFunc("/api/articles/164198", func(w http.ResponseWriter, r *http.Request) { |
|
138
|
|
|
if r.Method != http.MethodPut { |
|
139
|
|
|
t.Error("invalid method for request") |
|
140
|
|
|
} |
|
141
|
|
|
cont, _ := ioutil.ReadAll(r.Body) |
|
142
|
|
|
w.Header().Add("content-type", "application/json") |
|
143
|
|
|
w.WriteHeader(http.StatusCreated) |
|
144
|
|
|
w.Write(cont) |
|
145
|
|
|
}) |
|
146
|
|
|
|
|
147
|
|
|
res, _ := testClientPro.Articles.Update(ctx, Article{ |
|
148
|
|
|
TypeOf: "article", |
|
149
|
|
|
ID: 164198, |
|
150
|
|
|
Title: "Demo article", |
|
151
|
|
|
Published: false, |
|
152
|
|
|
}) |
|
153
|
|
|
|
|
154
|
|
|
if res.Title != "Demo article" { |
|
155
|
|
|
t.Error("article parsing failed") |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
func TestArticlesResource_UpdateFailsWhenInsecure(t *testing.T) { |
|
160
|
|
|
setup() |
|
161
|
|
|
defer teardown() |
|
162
|
|
|
|
|
163
|
|
|
_, err := testClientPub.Articles.Update(ctx, Article{ |
|
164
|
|
|
TypeOf: "article", |
|
165
|
|
|
ID: 164198, |
|
166
|
|
|
Title: "Demo article", |
|
167
|
|
|
Published: false, |
|
168
|
|
|
}) |
|
169
|
|
|
|
|
170
|
|
|
if !reflect.DeepEqual(err, ErrProtectedEndpoint) { |
|
171
|
|
|
t.Error("auth check failed") |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|