-
Notifications
You must be signed in to change notification settings - Fork 0
/
xeno_test.go
64 lines (55 loc) · 1.44 KB
/
xeno_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
package xeno
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestNewClient(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if client.httpClient != http.DefaultClient {
t.Errorf("Expected default HTTP client, got different client")
}
if client.baseURL.String() != APIEndpointBase {
t.Errorf("Expected baseURL %s, got %s", APIEndpointBase, client.baseURL.String())
}
}
func TestClient_Get(t *testing.T) {
testCases := []struct {
name string
query string
expectedResult *Response
mockResponse string
mockStatusCode int
}{
{
name: "Successful response",
query: "test",
expectedResult: &Response{Page: 1},
mockResponse: `{"page":1}`,
mockStatusCode: http.StatusOK,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.mockStatusCode)
w.Write([]byte(tc.mockResponse))
}))
defer mockServer.Close()
client, _ := NewClient(WithBaseURL(mockServer.URL + "?query="))
response, err := client.Get(context.Background(), tc.query)
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if !reflect.DeepEqual(response, tc.expectedResult) {
t.Errorf("Expected %+v, got %+v", tc.expectedResult, response)
}
})
}
}