forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_requests_test.go
67 lines (57 loc) · 1.47 KB
/
batch_requests_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
package main
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/TykTechnologies/tyk/test"
)
const testBatchRequest = `{
"requests": [
{
"method": "GET",
"headers": {
"test-header-1": "test-1",
"test-header-2": "test-2"
},
"relative_url": "get/?param1=this"
},
{
"method": "POST",
"body": "TEST BODY",
"relative_url": "post/"
},
{
"method": "PUT",
"relative_url": "put/"
}
],
"suppress_parallel_execution": true
}`
func TestBatch(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/v1/"
spec.EnableBatchRequestSupport = true
})
ts.Run(t, []test.TestCase{
{Method: "POST", Path: "/v1/tyk/batch/", Data: `{"requests":[]}`, Code: 200, BodyMatch: "[]"},
{Method: "POST", Path: "/v1/tyk/batch/", Data: "malformed", Code: 400},
{Method: "POST", Path: "/v1/tyk/batch/", Data: testBatchRequest, Code: 200},
}...)
resp, _ := ts.Do(test.TestCase{Method: "POST", Path: "/v1/tyk/batch/", Data: testBatchRequest})
if resp != nil {
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var batchResponse []map[string]json.RawMessage
if err := json.Unmarshal(body, &batchResponse); err != nil {
t.Fatal(err)
}
if len(batchResponse) != 3 {
t.Errorf("Length not match: %d", len(batchResponse))
}
if string(batchResponse[0]["relative_url"]) != `"get/?param1=this"` {
t.Error("Url order not match:", string(batchResponse[0]["relative_url"]))
}
}
}