-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpclient_test.go
114 lines (108 loc) · 2.71 KB
/
httpclient_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
package httpclient
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"testing"
"github.com/matryer/is"
)
func TestGetResponse(t *testing.T) {
t.Run("ip endpoint", func(t *testing.T) {
response, err := GetResponse(nil, http.MethodGet, "https://firefly.nusak.ca/ip", "", nil)
is := is.New(t)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusOK)
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
is.NoErr(err)
ip := net.ParseIP(string(bytes))
if ip == nil {
is.Fail()
}
})
t.Run("invalid endpoint", func(t *testing.T) {
response, err := GetResponse(nil, http.MethodGet, "https://firefly.nusak.ca/invalidendpoint", "", nil)
is := is.New(t)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusNotFound)
})
t.Run("login and hello", func(t *testing.T) {
type Data struct {
User string
Pass string
}
var data Data
data.User = "demo"
data.Pass = "pass"
jwt := struct {
JWT string
}{}
response, err := GetResponse(data, http.MethodPost, "https://firefly.nusak.ca/login", "", nil)
is := is.New(t)
is.NoErr(err)
defer response.Body.Close()
is.NoErr(json.NewDecoder(response.Body).Decode(&jwt))
response, err = GetResponse("", http.MethodGet, "https://firefly.nusak.ca/api/hello", jwt.JWT, nil)
is.NoErr(err)
is.Equal(response.StatusCode, http.StatusOK)
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
is.NoErr(err)
ip := net.ParseIP(string(bytes))
if ip == nil {
is.Fail()
}
})
t.Run("badlogin", func(t *testing.T) {
type Data struct {
User string
Pass string
}
answer := struct {
Request Data
Error string
}{}
var data Data
data.Pass = "badpass"
response, err := GetResponse(data, http.MethodPost, "https://firefly.nusak.ca/login", "", nil)
is := is.New(t)
is.NoErr(err)
defer response.Body.Close()
is.Equal(response.StatusCode, http.StatusBadRequest)
is.NoErr(json.NewDecoder(response.Body).Decode(&answer))
is.Equal(answer.Error, "invalid username or password")
})
t.Run("hello", func(t *testing.T) {
})
}
func TestGetJSON(t *testing.T) {
is := is.New(t)
t.Run("login", func(t *testing.T) {
data := struct {
User string
Pass string
}{
User: "demo",
Pass: "pass",
}
response := struct {
JWT string
}{}
var errResponse any
e := JSONEndpoint[struct{ JWT string }, any]{
URL: "http://firefly.nusak.ca",
Route: "/login",
Method: http.MethodPost,
Data: data,
Response: response,
ErrorResponse: errResponse,
}
answer, errs, err := e.GetJSON(response, errResponse)
is.NoErr(err)
answerType := fmt.Sprintf("%T", answer)
is.True(answerType == "struct { JWT string }")
is.Equal(errs, nil)
})
}