-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttpclient_test.go
More file actions
126 lines (116 loc) · 3.07 KB
/
httpclient_test.go
File metadata and controls
126 lines (116 loc) · 3.07 KB
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
package httpclient
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"testing"
"github.com/Kairum-Labs/should"
)
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)
should.BeNil(t, err)
should.BeEqual(t, response.StatusCode, http.StatusOK)
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
should.BeNil(t, err)
ip := net.ParseIP(string(bytes))
if ip == nil {
t.Log(string(bytes))
t.Fail()
}
})
t.Run("invalid endpoint", func(t *testing.T) {
response, err := GetResponse(nil, http.MethodGet, "https://firefly.nusak.ca/invalidendpoint", "", nil)
should.BeNil(t, err)
should.BeEqual(t, 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)
should.BeNil(t, err)
defer response.Body.Close()
should.BeNil(t, json.NewDecoder(response.Body).Decode(&jwt))
response, err = GetResponse("", http.MethodGet, "https://firefly.nusak.ca/api/hello", jwt.JWT, nil)
should.BeNil(t, err)
should.BeEqual(t, response.StatusCode, http.StatusOK)
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
should.BeNil(t, err)
ip := net.ParseIP(string(bytes))
if ip == nil {
t.Fail()
}
})
t.Run("badlogin", func(t *testing.T) {
data := struct {
User string
Pass string
}{
User: "demo",
Pass: "badpass",
}
answer := struct {
Message string
}{}
response, err := GetResponse(data, http.MethodPost, "https://firefly.nusak.ca/login", "", nil)
should.BeNil(t, err)
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
should.BeNil(t, err)
should.BeEqual(t, response.StatusCode, http.StatusBadRequest)
should.BeNil(t, json.Unmarshal(body, &answer))
should.BeEqual(t, answer.Message, "invalid username or password")
})
t.Run("hello", func(t *testing.T) {
})
}
func TestGetJSON(t *testing.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)
should.BeNil(t, err)
answerType := fmt.Sprintf("%T", answer)
t.Log(answerType)
should.BeTrue(t, answerType == "struct { JWT string }")
should.BeNil(t, errs)
})
}
type dummyStruct struct {
Value string `json:"value"`
}
func TestGetJSON_PanicOnNilResponse(t *testing.T) {
var resp dummyStruct
var errResp dummyStruct
_, _, err := GetJSON(nil, resp, errResp, "GET", ":", "", nil)
should.BeError(t, err)
}