-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
38 lines (29 loc) · 967 Bytes
/
utils.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
package tests
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/gin-gonic/gin"
"github.com/wenzzyx/go-ushort/app/modules/auth"
)
func authFn(r *gin.Engine, authRoute, email, password string) (*auth.AuthResponse, error) {
w := httptest.NewRecorder()
reqBody, _ := json.Marshal(map[string]any{
"email": email,
"password": password,
})
req, _ := http.NewRequest("POST", "/api/v1/auth"+authRoute, bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
var resBody *auth.AuthResponse
err := json.Unmarshal(w.Body.Bytes(), &resBody)
return resBody, err
}
// Run this on top function of test file
func RegisterForTest(r *gin.Engine, email, password string) (*auth.AuthResponse, error) {
return authFn(r, "/registration", email, password)
}
func LoginForTest(r *gin.Engine, email, password string) (*auth.AuthResponse, error) {
return authFn(r, "/login", email, password)
}