forked from microsoftgraph/msgraph-sdk-go-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatch_request_collection_test.go
81 lines (64 loc) · 2.04 KB
/
batch_request_collection_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
package msgraphgocore
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewBatchRequestCollectionNoLimit(t *testing.T) {
batch := NewBatchRequestCollection(reqAdapter)
reqInfo := getRequestInfo()
for i := 0; i < 20; i++ {
_, err := batch.AddBatchRequestStep(*reqInfo)
if err != nil {
return
}
}
_, err := batch.AddBatchRequestStep(*reqInfo)
assert.Nil(t, err)
}
func TestBatchRequestCollectionReturnsBatchResponse(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
jsonResponse := getDummyJSON()
w.WriteHeader(200)
fmt.Fprint(w, jsonResponse)
}))
defer testServer.Close()
reqInfo := getRequestInfo()
mockPath := testServer.URL + "/$batch"
reqAdapter.SetBaseUrl(mockPath) // check that path is not empty instead
batch := NewBatchRequestCollection(reqAdapter)
for i := 0; i < 40; i++ {
_, err := batch.AddBatchRequestStep(*reqInfo)
if err != nil {
require.NoError(t, err)
}
}
resp, err := batch.Send(context.Background(), reqAdapter)
require.NoError(t, err)
assert.Equal(t, len(resp.GetResponses()), 12)
}
func TestBatchRequestResponseGetFailedResponses(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
jsonResponse := getDummyJSON()
w.WriteHeader(200)
fmt.Fprint(w, jsonResponse)
}))
defer testServer.Close()
reqInfo := getRequestInfo()
mockPath := testServer.URL + "/$batch"
reqAdapter.SetBaseUrl(mockPath) // check that path is not empty instead
batch := NewBatchRequestCollection(reqAdapter)
_, err := batch.AddBatchRequestStep(*reqInfo)
require.NoError(t, err)
resp, err := batch.Send(context.Background(), reqAdapter)
require.NoError(t, err)
assert.Equal(t, len(resp.GetStatusCodes()), 4)
status := resp.GetFailedResponses()
assert.Equal(t, 1, len(status))
}