-
Notifications
You must be signed in to change notification settings - Fork 15
/
client_test.go
92 lines (72 loc) · 2.39 KB
/
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
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
package clarifai
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
const (
ClientID = "CLIENT_ID"
ClientSecret = "CLIENT_SECRET"
)
func TestNewClarifaiClient(t *testing.T) {
client := NewClient(ClientID, ClientSecret)
if client == nil {
t.Error("NewClient should not return nil")
}
}
func TestNewClarifaiClientStoredValues(t *testing.T) {
client := NewClient(ClientID, ClientSecret)
if client.ClientID != ClientID || client.ClientSecret != ClientSecret {
t.Error("NewClient should store the values of clientID and clientSecret")
}
}
func TestRequestAccessToken(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := NewClient(ClientID, ClientSecret)
client.setAPIRoot(server.URL)
defer server.Close()
mux.HandleFunc("/v1/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"access_token":"1234567890abcdefg","expires_in":36000,"scope": "api_access", "token_type": "Bearer"}`)
})
err := client.requestAccessToken()
if err != nil {
t.Errorf("requestAccessToken() should not return an err upon success: %v", err)
}
}
func TestRequestAccessTokenFail(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := NewClient(ClientID, ClientSecret)
client.setAPIRoot(server.URL)
defer server.Close()
mux.HandleFunc("/v1/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
})
err := client.requestAccessToken()
if err == nil {
t.Errorf("requestAccessToken() should return an err with an invalid request: %v", err)
}
}
func TestAccessTokenIsSaved(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := NewClient(ClientID, ClientSecret)
client.setAPIRoot(server.URL)
defer server.Close()
mux.HandleFunc("/v1/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"access_token":"1234567890abcdefg","expires_in":36000,"scope": "api_access", "token_type": "Bearer"}`)
})
err := client.requestAccessToken()
if err != nil {
t.Errorf("requestAccessToken() should not return err with a valid response")
}
if client.AccessToken != "1234567890abcdefg" {
t.Errorf("requestAccessToken() should store the access token. Expected: 1234567890abcdefg, Got: %v", client.AccessToken)
}
}