-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_test.go
43 lines (40 loc) · 958 Bytes
/
api_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
package nap
import (
"net/http"
"testing"
)
func TestAPICall(t *testing.T) {
api := NewAPI("https://httpbin.org")
router := NewRouter()
router.RegisterFunc(200, func(resp *http.Response, _ interface{}) error {
return nil
})
res := NewResource("/get", "GET", router)
api.AddResource("get", res)
if err := api.Call("get", nil); err != nil {
t.Fail()
}
resources := api.ResourceNames()
if len(resources) != 1 || resources[0] != "get" {
t.Fail()
}
}
func TestAPIAuth(t *testing.T) {
api := NewAPI("https://httpbin.org")
router := NewRouter()
router.RegisterFunc(200, func(resp *http.Response, _ interface{}) error {
return nil
})
res := NewResource("/basic-auth/{{.user}}/{{.pass}}", "GET", router)
api.AddResource("basicauth", res)
api.SetAuth(&AuthBasic{
Username: "user",
Password: "passw0rd",
})
if err := api.Call("basicauth", map[string]string{
"user": "user",
"pass": "passw0rd",
}); err != nil {
t.Fail()
}
}