-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathclient_test.go
72 lines (65 loc) · 1.78 KB
/
client_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
package msgraph
import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)
func TestClient_GetWithError(t *testing.T) {
// This creates a listener on a random available port.
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()
hc := NewClient(VersionBeta)
hc.Endpoint = fmt.Sprintf("https://localhost:%d/", port)
hc.RetryableClient.RetryMax = 2
_, _, _, err = hc.Get(context.Background(), GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/users/test",
},
})
if err == nil {
t.Error("expected to get an error, got nil")
}
if msg := err.Error(); !strings.Contains(msg, "connect: connection refused") {
log.Fatalf("got %s, want message with 'connection refused'", msg)
}
}
func TestClient_GetWithResponseAndError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/beta/users/test" {
n, _ := strconv.Atoi(r.FormValue("n"))
if n < 15 {
http.Redirect(w, r, fmt.Sprintf("%s?n=%d", r.URL.Path, 1), http.StatusTemporaryRedirect)
return
}
}
}))
defer ts.Close()
hc := NewClient(VersionBeta)
hc.Endpoint = ts.URL
hc.RetryableClient.RetryMax = 2
_, _, _, err := hc.Get(context.Background(), GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/users/test",
},
})
if err == nil {
t.Error("expected to get an error, got nil")
}
if msg := err.Error(); !strings.Contains(msg, "stopped after 10 redirects") {
log.Fatalf("got %s, want message with 'stopped after 10 redirects'", msg)
}
}