123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- package sinks
- import (
- "encoding/json"
- "fmt"
- "github.com/emqx/kuiper/common"
- "github.com/emqx/kuiper/xstream/contexts"
- "io/ioutil"
- "net/http"
- "net/http/httptest"
- "reflect"
- "testing"
- )
- type request struct {
- Method string
- Body string
- ContentType string
- }
- func TestRestSink_Apply(t *testing.T) {
- var tests = []struct {
- config map[string]interface{}
- data []map[string]interface{}
- result []request
- }{
- {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "sendSingle": true,
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "POST",
- Body: `{"ab":"hello1"}`,
- ContentType: "application/json",
- }, {
- Method: "POST",
- Body: `{"ab":"hello2"}`,
- ContentType: "application/json",
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "POST",
- Body: `[{"ab":"hello1"},{"ab":"hello2"}]`,
- ContentType: "application/json",
- }},
- }, {
- config: map[string]interface{}{
- "method": "get",
- //"url": "http://localhost/test", //set dynamically to the test server
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "GET",
- ContentType: "",
- }},
- }, {
- config: map[string]interface{}{
- "method": "put",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "text",
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "PUT",
- ContentType: "text/plain",
- Body: `[{"ab":"hello1"},{"ab":"hello2"}]`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "form",
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `result=%5B%7B%22ab%22%3A%22hello1%22%7D%2C%7B%22ab%22%3A%22hello2%22%7D%5D`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "form",
- "sendSingle": true,
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `ab=hello1`,
- }, {
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `ab=hello2`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "json",
- "sendSingle": true,
- "timeout": float64(1000),
- },
- data: []map[string]interface{}{{
- "ab": "hello1",
- }, {
- "ab": "hello2",
- }},
- result: []request{{
- Method: "POST",
- Body: `{"ab":"hello1"}`,
- ContentType: "application/json",
- }, {
- Method: "POST",
- Body: `{"ab":"hello2"}`,
- ContentType: "application/json",
- }},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := common.Log.WithField("rule", "TestRestSink_Apply")
- ctx := contexts.WithValue(contexts.Background(), contexts.LoggerKey, contextLogger)
- var requests []request
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- fmt.Printf("Error reading body: %v", err)
- http.Error(w, "can't read body", http.StatusBadRequest)
- return
- }
- requests = append(requests, request{
- Method: r.Method,
- Body: string(body),
- ContentType: r.Header.Get("Content-Type"),
- })
- contextLogger.Debugf(string(body))
- fmt.Fprintf(w, string(body))
- }))
- defer ts.Close()
- for i, tt := range tests {
- requests = nil
- ss, ok := tt.config["sendSingle"]
- if !ok {
- ss = false
- }
- s := &RestSink{}
- tt.config["url"] = ts.URL
- s.Configure(tt.config)
- s.Open(ctx)
- if ss.(bool) {
- for _, d := range tt.data {
- input, err := json.Marshal(d)
- if err != nil {
- t.Errorf("Failed to parse the input into []byte]")
- continue
- }
- s.Collect(ctx, input)
- }
- } else {
- input, err := json.Marshal(tt.data)
- if err != nil {
- t.Errorf("Failed to parse the input into []byte]")
- continue
- }
- s.Collect(ctx, input)
- }
- s.Close(ctx)
- if !reflect.DeepEqual(tt.result, requests) {
- t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
- }
- }
- }
- func TestRestSinkTemplate_Apply(t *testing.T) {
- var tests = []struct {
- config map[string]interface{}
- data [][]byte
- result []request
- }{
- {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "sendSingle": true,
- "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
- },
- data: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`), []byte(`{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`)},
- result: []request{{
- Method: "POST",
- Body: `{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`,
- ContentType: "application/json",
- }, {
- Method: "POST",
- Body: `{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`,
- ContentType: "application/json",
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "dataTemplate": `{"wrapper":"arr","content":{{json .}},"content0":{{json (index . 0)}},ab0":"{{index . 0 "ab"}}"}`,
- },
- data: [][]byte{[]byte(`{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`)},
- result: []request{{
- Method: "POST",
- Body: `{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`,
- ContentType: "application/json",
- }},
- }, {
- config: map[string]interface{}{
- "method": "get",
- //"url": "http://localhost/test", //set dynamically to the test server
- "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
- },
- data: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`)},
- result: []request{{
- Method: "GET",
- ContentType: "",
- }},
- }, {
- config: map[string]interface{}{
- "method": "put",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "html",
- "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.ab}}</li>{{end}}</ul>`,
- },
- data: [][]byte{[]byte(`<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`)},
- result: []request{{
- Method: "PUT",
- ContentType: "text/html",
- Body: `<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "form",
- "dataTemplate": `{"content":{{json .}}}`,
- },
- data: [][]byte{[]byte(`{"content":[{"ab":"hello1"},{"ab":"hello2"}]}`)},
- result: []request{{
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `content=%5B%7B%22ab%22%3A%22hello1%22%7D%2C%7B%22ab%22%3A%22hello2%22%7D%5D`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "form",
- "sendSingle": true,
- "dataTemplate": `{"newab":"{{.ab}}"}`,
- },
- data: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
- result: []request{{
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `newab=hello1`,
- }, {
- Method: "POST",
- ContentType: "application/x-www-form-urlencoded;param=value",
- Body: `newab=hello2`,
- }},
- }, {
- config: map[string]interface{}{
- "method": "post",
- //"url": "http://localhost/test", //set dynamically to the test server
- "bodyType": "json",
- "sendSingle": true,
- "timeout": float64(1000),
- "dataTemplate": `{"newab":"{{.ab}}"}`,
- },
- data: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
- result: []request{{
- Method: "POST",
- Body: `{"newab":"hello1"}`,
- ContentType: "application/json",
- }, {
- Method: "POST",
- Body: `{"newab":"hello2"}`,
- ContentType: "application/json",
- }},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := common.Log.WithField("rule", "TestRestSink_Apply")
- ctx := contexts.WithValue(contexts.Background(), contexts.LoggerKey, contextLogger)
- var requests []request
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- fmt.Printf("Error reading body: %v", err)
- http.Error(w, "can't read body", http.StatusBadRequest)
- return
- }
- requests = append(requests, request{
- Method: r.Method,
- Body: string(body),
- ContentType: r.Header.Get("Content-Type"),
- })
- contextLogger.Debugf(string(body))
- fmt.Fprintf(w, string(body))
- }))
- defer ts.Close()
- for i, tt := range tests {
- requests = nil
- s := &RestSink{}
- tt.config["url"] = ts.URL
- s.Configure(tt.config)
- s.Open(ctx)
- for _, d := range tt.data {
- s.Collect(ctx, d)
- }
- s.Close(ctx)
- if !reflect.DeepEqual(tt.result, requests) {
- t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
- }
- }
- }
|