forked from koltyakov/gosip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosip_test.go
109 lines (92 loc) · 2.55 KB
/
gosip_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
package gosip
import (
"fmt"
"net/http"
"testing"
"time"
)
func TestEdges(t *testing.T) {
siteURL := "http://localhost:8989/sub" // sub URI to avoid digest caching when running tests in parallel
closer, err := startFakeServer(":8989", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// faking digest response
if r.RequestURI == "/sub/_api/ContextInfo" {
_, _ = fmt.Fprintf(w, `{"d":{"GetContextWebInformation":{"FormDigestValue":"","FormDigestTimeoutSeconds":120,"LibraryVersion":"FAKE"}}}`)
return
}
if r.RequestURI == "/sub/_api/wait" {
time.Sleep(1 * time.Second)
_, _ = fmt.Fprintf(w, `{"result":"one eternity later"}`)
return
}
_, _ = fmt.Fprintf(w, `{ "result": "Cool alfter some retries" }`)
}))
if err != nil {
t.Fatal(err)
}
defer func() { _ = closer.Close() }()
t.Run("EmptyURLShouldFail", func(t *testing.T) {
client := &SPClient{
AuthCnfg: &AnonymousCnfg{SiteURL: ""},
}
req, err := http.NewRequest("POST", client.AuthCnfg.GetSiteURL()+"/_api/post", nil)
if err != nil {
t.Fatal(err)
}
if _, err := client.Execute(req); err == nil {
t.Error(err)
}
})
t.Run("ImcorrectConfigShouldFail", func(t *testing.T) {
client := &SPClient{
AuthCnfg: &AnonymousCnfg{},
ConfigPath: "incorrect",
}
req, err := http.NewRequest("POST", client.AuthCnfg.GetSiteURL()+"/_api/post", nil)
if err != nil {
t.Fatal(err)
}
if _, err := client.Execute(req); err == nil {
t.Error(err)
}
})
t.Run("SetAuthReturnError", func(t *testing.T) {
client := &SPClient{
AuthCnfg: &AnonymousCnfg{
SiteURL: "http://restricted",
},
}
req, err := http.NewRequest("POST", client.AuthCnfg.GetSiteURL()+"/_api/post", nil)
if err != nil {
t.Fatal(err)
}
if _, err := client.Execute(req); err == nil {
t.Error(err)
}
})
t.Run("OnDigestFailed", func(t *testing.T) {
client := &SPClient{
AuthCnfg: &AnonymousCnfg{SiteURL: siteURL},
}
req, err := http.NewRequest("POST", client.AuthCnfg.GetSiteURL()+"/_api/faildigest", nil)
if err != nil {
t.Fatal(err)
}
if _, err := client.Execute(req); err == nil {
t.Error("should fail to retrieve digest")
}
})
t.Run("ClientTimeout", func(t *testing.T) {
client := &SPClient{
AuthCnfg: &AnonymousCnfg{SiteURL: siteURL},
}
client.Timeout = 1 * time.Millisecond
req, err := http.NewRequest("GET", client.AuthCnfg.GetSiteURL()+"/_api/wait", nil)
if err != nil {
t.Fatal(err)
}
_, err = client.Execute(req) // should fail after a timeout
if err == nil {
t.Error("request canceling failed")
}
})
}