-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepgram_test.go
109 lines (99 loc) · 2.81 KB
/
deepgram_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
103
104
105
106
107
108
109
package deepgram
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
type invalidResponse struct {
Error string `json:"error"`
}
var (
dg Deepgram = Deepgram{
ApiKey: "123-abc-456-def",
}
respSuccess CheckBalanceResponse = CheckBalanceResponse{
Balance: 123.0,
UserId: "123-abc-456-def",
}
invalidRes invalidResponse = invalidResponse{
Error: "invalid contentID / userID",
}
goodJson string = "{\"balance\":123,\"userID\":\"123-abc-456-def\"}"
errorJson string = "{\"error\":\"invalid contentID / userID\"}"
badJson string = "{"
)
func TestMakeRequestSuccess(t *testing.T) {
// setup test server
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
res, _ := json.Marshal(respSuccess)
fmt.Fprint(w, string(res))
})
testServer := httptest.NewServer(handler)
defer testServer.Close()
payload := checkBalanceRequest{
Action: "get_balance",
UserId: dg.ApiKey,
}
response, err := makeRequest(testServer.URL, payload)
if err != nil {
t.Error("Got error", err, "expected", nil)
}
respSuccessBytes, _ := json.Marshal(respSuccess)
if string(respSuccessBytes) != string(response) {
t.Error("Expected", string(respSuccessBytes), "Got", string(response))
}
}
func TestMakeRequestNilResponse(t *testing.T) {
// setup test server
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, nil)
})
testServer := httptest.NewServer(handler)
defer testServer.Close()
payload := checkBalanceRequest{
Action: "get_balance",
UserId: dg.ApiKey,
}
response, err := makeRequest(testServer.URL, payload)
if err != nil {
t.Error("Got error", err, "expected", nil)
}
if string(response) == "" {
t.Error("Expected", nil, "Got", string(response))
}
}
func TestParseResponseSuccess(t *testing.T) {
response := new(CheckBalanceResponse)
err := parseResponse([]byte(goodJson), response)
if err != nil {
t.Error("Got error", err, "expected", nil)
}
if !reflect.DeepEqual(*response, respSuccess) {
t.Error("Expected", respSuccess, "Got", *response)
}
}
func TestParseResponseInvalidJsonResponse(t *testing.T) {
response := new(CheckBalanceResponse)
err := parseResponse([]byte(badJson), response)
if err == nil {
t.Error("Expecting error", err, "got", nil)
}
if *response != *(new(CheckBalanceResponse)) {
t.Error("Expected", *(new(CheckBalanceResponse)), "Got", *response)
}
}
func TestParseResponseErrorResponse(t *testing.T) {
response := new(CheckBalanceResponse)
err := parseResponse([]byte(errorJson), response)
if err == nil {
t.Error("Expecting error", err, "got", nil)
}
if *response != *(new(CheckBalanceResponse)) {
t.Error("Expected", *(new(CheckBalanceResponse)), "Got", *response)
}
}