package test import ( "context" "fmt" "git.sxidc.com/service-supports/dapr_api/invoke" "github.com/stretchr/testify/assert" "io" "net/http" "sync" "testing" "time" ) var ( contentTypeHeader = map[string]string{ "Content-Type": "text/plain", } ) const ( invokeServerAddress = "localhost:10081" baseUrl = "http://localhost:10081" daprBaseUrl = "http://localhost:10080/v1.0/invoke/dapr_api/method" postJsonMethodName = "/post-json" deleteJsonMethodName = "/delete-json" putJsonMethodName = "/put-json" getJsonMethodName = "/get-json" postMethodName = "/post" deleteMethodName = "/delete" putMethodName = "/put" getMethodName = "/get" postJsonData = `"{"data":"post-json"}"` postData = `post` putJsonData = `"{"data":"put-json"}"` putData = `put` postJsonResponse = `"{"data":"response-post-json"}"` postResponse = `response-post` deleteJsonResponse = `"{"data":"response-delete-json"}"` deleteResponse = `response-delete` putJsonResponse = `"{"data":"response-put-json"}"` putResponse = `response-put` getJsonResponse = `"{"data":"response-get-json"}"` getResponse = `response-get` ) func TestPostJSON(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.PostJSON(postJsonMethodName, []byte(postJsonData), nil) if err != nil { t.Fatal(err) } assert.Equal(t, postJsonResponse, string(response), "响应不一致") daprResponse, err := daprAPI.PostJSON(postJsonMethodName, []byte(postJsonData), nil) if err != nil { t.Fatal(err) } assert.Equal(t, postJsonResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestPutJSON(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.PutJSON(putJsonMethodName, []byte(putJsonData), nil) if err != nil { t.Fatal(err) } assert.Equal(t, putJsonResponse, string(response), "响应不一致") daprResponse, err := daprAPI.PutJSON(putJsonMethodName, []byte(putJsonData), nil) if err != nil { t.Fatal(err) } assert.Equal(t, putJsonResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestPostWithoutHeader(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.PostWithoutHeaders(postMethodName, []byte(postData)) if err != nil { t.Fatal(err) } assert.Equal(t, postResponse, string(response), "响应不一致") daprResponse, err := daprAPI.PostWithoutHeaders(postMethodName, []byte(postData)) if err != nil { t.Fatal(err) } assert.Equal(t, postResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestPutWithoutHeaders(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.PutWithoutHeaders(putMethodName, []byte(putData)) if err != nil { t.Fatal(err) } assert.Equal(t, putResponse, string(response), "响应不一致") daprResponse, err := daprAPI.PutWithoutHeaders(putMethodName, []byte(putData)) if err != nil { t.Fatal(err) } assert.Equal(t, putResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestDeleteWithoutHeaders(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.DeleteWithoutHeaders(deleteJsonMethodName, nil) if err != nil { t.Fatal(err) } assert.Equal(t, deleteJsonResponse, string(response), "响应不一致") daprResponse, err := daprAPI.DeleteWithoutHeaders(deleteJsonMethodName, nil) if err != nil { t.Fatal(err) } assert.Equal(t, deleteJsonResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestGetWithoutHeaaders(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.GetWithoutHeaders(getJsonMethodName, nil) if err != nil { t.Fatal(err) } assert.Equal(t, getJsonResponse, string(response), "响应不一致") daprResponse, err := daprAPI.GetWithoutHeaders(getJsonMethodName, nil) if err != nil { t.Fatal(err) } assert.Equal(t, getJsonResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestPost(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.Post(postMethodName, contentTypeHeader, []byte(postData)) if err != nil { t.Fatal(err) } assert.Equal(t, postResponse, string(response), "响应不一致") daprResponse, err := daprAPI.Post(postMethodName, contentTypeHeader, []byte(postData)) if err != nil { t.Fatal(err) } assert.Equal(t, postResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestDelete(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.Delete(deleteMethodName, nil, contentTypeHeader) if err != nil { t.Fatal(err) } assert.Equal(t, deleteResponse, string(response), "响应不一致") daprResponse, err := daprAPI.Delete(deleteMethodName, nil, contentTypeHeader) if err != nil { t.Fatal(err) } assert.Equal(t, deleteResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestPut(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.Put(putMethodName, contentTypeHeader, []byte(putData)) if err != nil { t.Fatal(err) } assert.Equal(t, putResponse, string(response), "响应不一致") daprResponse, err := daprAPI.Put(putMethodName, contentTypeHeader, []byte(putData)) if err != nil { t.Fatal(err) } assert.Equal(t, putResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func TestGet(t *testing.T) { api := invoke.NewAPI(baseUrl, 10*time.Second) defer invoke.DestroyAPI(api) daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second) defer invoke.DestroyAPI(daprAPI) wg := &sync.WaitGroup{} wg.Add(2) wgServer := &sync.WaitGroup{} wgServer.Add(1) srv := initInvokeHttpServer(t, wgServer, wg) wgServer.Wait() response, err := api.Get(getMethodName, nil, contentTypeHeader) if err != nil { t.Fatal(err) } assert.Equal(t, getResponse, string(response), "响应不一致") daprResponse, err := daprAPI.Get(getMethodName, nil, contentTypeHeader) if err != nil { t.Fatal(err) } assert.Equal(t, getResponse, string(daprResponse), "响应不一致") wg.Wait() _ = srv.Shutdown(context.Background()) } func initInvokeHttpServer(t *testing.T, wgServer *sync.WaitGroup, wg *sync.WaitGroup) *http.Server { mux := http.NewServeMux() mux.HandleFunc("/post-json", func(w http.ResponseWriter, r *http.Request) { request, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(request) != postJsonData { t.Fatal(err) } fmt.Println("post-json Receive:", string(request)) w.WriteHeader(http.StatusOK) _, err = w.Write([]byte(postJsonResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/delete-json", func(w http.ResponseWriter, r *http.Request) { fmt.Println("delete-json") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(deleteJsonResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/put-json", func(w http.ResponseWriter, r *http.Request) { request, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(request) != putJsonData { t.Fatal(err) } fmt.Println("put-json Receive:", string(request)) w.WriteHeader(http.StatusOK) _, err = w.Write([]byte(putJsonResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/get-json", func(w http.ResponseWriter, r *http.Request) { fmt.Println("get-json") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(getJsonResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request) { request, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(request) != postData { t.Fatal(err) } fmt.Println("post Receive:", string(request)) w.WriteHeader(http.StatusOK) _, err = w.Write([]byte(postResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { fmt.Println("delete") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(deleteResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/put", func(w http.ResponseWriter, r *http.Request) { request, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(request) != putData { t.Fatal(err) } fmt.Println("put Receive:", string(request)) w.WriteHeader(http.StatusOK) _, err = w.Write([]byte(putResponse)) if err != nil { t.Fatal(err) } wg.Done() }) mux.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) { fmt.Println("get") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(getResponse)) if err != nil { t.Fatal(err) } wg.Done() }) srv := &http.Server{ Addr: invokeServerAddress, Handler: mux, } go func() { wgServer.Done() err := srv.ListenAndServe() if err != nil && err != http.ErrServerClosed { panic(err) } }() return srv }