-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_internal_test.go
65 lines (57 loc) · 1.72 KB
/
api_internal_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
package portfolio
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
)
func Test_portfolioTreeURL_default(t *testing.T) {
t.Setenv(ServerURLEnvironmentVariableName, "")
assert.Equal(t, DefaultURL, portfolioTreeURL())
}
func Test_portfolioTreeURL_other(t *testing.T) {
t.Setenv(ServerURLEnvironmentVariableName, "other")
assert.Equal(t, "other", portfolioTreeURL())
}
func Test_doJSONRequest_do_fails(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
_, err := doJSONRequest[struct{}](func(r *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("lemon")
}, req)
assert.Error(t, err)
}
func Test_doJSONRequest_unexpected_status(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
_, err := doJSONRequest[struct{}](func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusTeapot,
Body: io.NopCloser(&bytes.Reader{}),
}, nil
}, req)
assert.Error(t, err)
}
func Test_doJSONRequest_read_fails(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
_, err := doJSONRequest[struct{}](func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(iotest.ErrReader(fmt.Errorf("lemon"))),
}, nil
}, req)
assert.Error(t, err)
}
func Test_doJSONRequest_invalid_json(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
_, err := doJSONRequest[struct{}](func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("[]")),
}, nil
}, req)
assert.Error(t, err)
}