123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- 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
- }
|