-
Notifications
You must be signed in to change notification settings - Fork 8
/
request_test.go
129 lines (110 loc) · 3.67 KB
/
request_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package houndify_test
import (
"bytes"
. "github.com/soundhound/houndify-sdk-go"
"gotest.tools/assert"
"io/ioutil"
"net/http"
"testing"
)
type RoundTripFunc func(req *http.Request) *http.Response
// Satisfy the RoundTripper Interface that can act as a mock for making requests and
// returning responses. Any function with the matching prototype as RoundTripFunc can be
// used.
//
// This means to test a request, simply write a function that verifies the request, then
// returns a mock response. This response is then tested outside of the RoundTripFunc.
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// Return a client with the mock RoundTripper
func NewTestClient(f RoundTripFunc) *http.Client {
return &http.Client{
Transport: RoundTripFunc(f),
}
}
// Return a Client with the mock http Client
func NewTestHoundifyClient(c *http.Client) Client {
return Client{
ClientID: "9M22RyQGeu4bk1ToWkjX4g==",
ClientKey: "vHSRCJhQa6cIzZ6hCrQHwcKDQbdyBuV6mqFXuBG9vAQe3MqjVIEheNDoaTP6n-DQSzhoBsOJwOP5IrWM2pF1fg==",
HttpClient: c,
}
}
// Return a basic Text Request
func NewTestTextRequest() TextRequest {
return TextRequest{
URL: "http://test.com/v1/text",
Query: "what is the time",
UserID: "TestUserID",
RequestID: "TestRequestID",
RequestInfoFields: make(map[string]interface{}),
}
}
// Return a basic Text Request
func NewTestVoiceRequest() VoiceRequest {
return VoiceRequest{
URL: "http://test.com/v1/voice",
UserID: "TestUserID",
RequestID: "TestRequestID",
RequestInfoFields: make(map[string]interface{}),
}
}
// Tests TextRequest.NewRequest()
func TestNewTextRequest(t *testing.T) {
mockClient := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.Method, "POST")
assert.Equal(t, req.URL.String(), "http://test.com/v1/text?query=what%20is%20the%20time")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`No clue`)),
Header: make(http.Header),
}
})
textReq := NewTestTextRequest()
req, err := textReq.NewRequest()
assert.NilError(t, err)
mockClient.Do(req)
}
// Tests VoiceRequest.NewRequest()
func TestNewVoiceRequest(t *testing.T) {
mockClient := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.Method, "POST")
assert.Equal(t, req.URL.String(), "http://test.com/v1/voice")
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`No clue`)),
Header: make(http.Header),
}
})
voiceReq := NewTestVoiceRequest()
req, err := voiceReq.NewRequest()
assert.NilError(t, err)
mockClient.Do(req)
}
// Tests BuildRequest(TextRequest, Client), ensure the following:
// - URL is set to the proper URL configured in the textReq
// - User Agent is set properly
// - Headers all exist that are set
// - TODO:
// - RequestInfo verification
// - Find way to mock Auth stuff so dynamic auth headers (they change with time etc)
func TestBuildTextRequest(t *testing.T) {
var expectedVals = map[string]string{
"User-Agent": "Go Houndify SDK",
}
mockClient := NewTestClient(func(req *http.Request) *http.Response {
assert.Equal(t, req.Method, "POST")
assert.Equal(t, req.URL.String(), "http://test.com/v1/text?query=what%20is%20the%20time")
for k, v := range expectedVals {
assert.Equal(t, req.Header.Get(k), v)
}
return &http.Response{}
})
// Make the mock call after getting test structs
textReq := NewTestTextRequest()
houndifyClient := NewTestHoundifyClient(mockClient)
req, err := BuildRequest(&textReq, houndifyClient)
assert.NilError(t, err)
mockClient.Do(req)
}