-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_client_test.go
62 lines (58 loc) · 1.49 KB
/
api_client_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
package gopify
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-Shopify-Access-Token")
if token != "valid access token" {
failureResponse := map[string]any{
"errors": "Invalid API key or access token",
}
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(failureResponse)
return
}
successResponse := map[string]any{
"products": []map[string]any{
{
"title": "Product 1",
},
},
}
json.NewEncoder(w).Encode(successResponse)
}))
defer ts.Close()
cases := []struct {
accessToken string
want map[string]any
err error
}{
{
accessToken: "valid access token",
want: map[string]any{"products": []map[string]any{{"title": "Product 1"}}},
err: nil,
},
{
accessToken: "invalid access token",
want: map[string]any{},
err: ResponseError{Errors: `"Invalid API key or access token"`},
},
}
for _, c := range cases {
apiClient := NewClient(ts.URL[7:], c.accessToken)
apiClient.baseUrl = fmt.Sprintf("%s/admin/api/%s", ts.URL, apiClient.version)
res := map[string]any{}
_, err := apiClient.Get("products.json", nil, &res)
if err != c.err {
t.Errorf("Expected error %v, got %v", c.err, err)
}
if fmt.Sprint(res) != fmt.Sprint(c.want) {
t.Errorf("Expected response %v, got %v", c.want, res)
}
}
}