invoke_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. package test
  2. import (
  3. "context"
  4. "fmt"
  5. "git.sxidc.com/service-supports/dapr_api/invoke"
  6. "github.com/stretchr/testify/assert"
  7. "io"
  8. "net/http"
  9. "sync"
  10. "testing"
  11. "time"
  12. )
  13. var (
  14. contentTypeHeader = map[string]string{
  15. "Content-Type": "text/plain",
  16. }
  17. )
  18. const (
  19. invokeServerAddress = "localhost:10081"
  20. baseUrl = "http://localhost:10081"
  21. daprBaseUrl = "http://localhost:10080/v1.0/invoke/dapr_api/method"
  22. postJsonMethodName = "/post-json"
  23. deleteJsonMethodName = "/delete-json"
  24. putJsonMethodName = "/put-json"
  25. getJsonMethodName = "/get-json"
  26. postMethodName = "/post"
  27. deleteMethodName = "/delete"
  28. putMethodName = "/put"
  29. getMethodName = "/get"
  30. postJsonData = `"{"data":"post-json"}"`
  31. postData = `post`
  32. putJsonData = `"{"data":"put-json"}"`
  33. putData = `put`
  34. postJsonResponse = `"{"data":"response-post-json"}"`
  35. postResponse = `response-post`
  36. deleteJsonResponse = `"{"data":"response-delete-json"}"`
  37. deleteResponse = `response-delete`
  38. putJsonResponse = `"{"data":"response-put-json"}"`
  39. putResponse = `response-put`
  40. getJsonResponse = `"{"data":"response-get-json"}"`
  41. getResponse = `response-get`
  42. )
  43. func TestPostJSON(t *testing.T) {
  44. api := invoke.NewAPI(baseUrl, 10*time.Second)
  45. defer invoke.DestroyAPI(api)
  46. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  47. defer invoke.DestroyAPI(daprAPI)
  48. wg := &sync.WaitGroup{}
  49. wg.Add(2)
  50. wgServer := &sync.WaitGroup{}
  51. wgServer.Add(1)
  52. srv := initInvokeHttpServer(t, wgServer, wg)
  53. wgServer.Wait()
  54. response, err := api.PostJSON(postJsonMethodName, []byte(postJsonData), nil)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. assert.Equal(t, postJsonResponse, string(response), "响应不一致")
  59. daprResponse, err := daprAPI.PostJSON(postJsonMethodName, []byte(postJsonData), nil)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. assert.Equal(t, postJsonResponse, string(daprResponse), "响应不一致")
  64. wg.Wait()
  65. _ = srv.Shutdown(context.Background())
  66. }
  67. func TestPutJSON(t *testing.T) {
  68. api := invoke.NewAPI(baseUrl, 10*time.Second)
  69. defer invoke.DestroyAPI(api)
  70. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  71. defer invoke.DestroyAPI(daprAPI)
  72. wg := &sync.WaitGroup{}
  73. wg.Add(2)
  74. wgServer := &sync.WaitGroup{}
  75. wgServer.Add(1)
  76. srv := initInvokeHttpServer(t, wgServer, wg)
  77. wgServer.Wait()
  78. response, err := api.PutJSON(putJsonMethodName, []byte(putJsonData), nil)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. assert.Equal(t, putJsonResponse, string(response), "响应不一致")
  83. daprResponse, err := daprAPI.PutJSON(putJsonMethodName, []byte(putJsonData), nil)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. assert.Equal(t, putJsonResponse, string(daprResponse), "响应不一致")
  88. wg.Wait()
  89. _ = srv.Shutdown(context.Background())
  90. }
  91. func TestPostWithoutHeader(t *testing.T) {
  92. api := invoke.NewAPI(baseUrl, 10*time.Second)
  93. defer invoke.DestroyAPI(api)
  94. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  95. defer invoke.DestroyAPI(daprAPI)
  96. wg := &sync.WaitGroup{}
  97. wg.Add(2)
  98. wgServer := &sync.WaitGroup{}
  99. wgServer.Add(1)
  100. srv := initInvokeHttpServer(t, wgServer, wg)
  101. wgServer.Wait()
  102. response, err := api.PostWithoutHeaders(postMethodName, []byte(postData))
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. assert.Equal(t, postResponse, string(response), "响应不一致")
  107. daprResponse, err := daprAPI.PostWithoutHeaders(postMethodName, []byte(postData))
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. assert.Equal(t, postResponse, string(daprResponse), "响应不一致")
  112. wg.Wait()
  113. _ = srv.Shutdown(context.Background())
  114. }
  115. func TestPutWithoutHeaders(t *testing.T) {
  116. api := invoke.NewAPI(baseUrl, 10*time.Second)
  117. defer invoke.DestroyAPI(api)
  118. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  119. defer invoke.DestroyAPI(daprAPI)
  120. wg := &sync.WaitGroup{}
  121. wg.Add(2)
  122. wgServer := &sync.WaitGroup{}
  123. wgServer.Add(1)
  124. srv := initInvokeHttpServer(t, wgServer, wg)
  125. wgServer.Wait()
  126. response, err := api.PutWithoutHeaders(putMethodName, []byte(putData))
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. assert.Equal(t, putResponse, string(response), "响应不一致")
  131. daprResponse, err := daprAPI.PutWithoutHeaders(putMethodName, []byte(putData))
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. assert.Equal(t, putResponse, string(daprResponse), "响应不一致")
  136. wg.Wait()
  137. _ = srv.Shutdown(context.Background())
  138. }
  139. func TestDeleteWithoutHeaders(t *testing.T) {
  140. api := invoke.NewAPI(baseUrl, 10*time.Second)
  141. defer invoke.DestroyAPI(api)
  142. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  143. defer invoke.DestroyAPI(daprAPI)
  144. wg := &sync.WaitGroup{}
  145. wg.Add(2)
  146. wgServer := &sync.WaitGroup{}
  147. wgServer.Add(1)
  148. srv := initInvokeHttpServer(t, wgServer, wg)
  149. wgServer.Wait()
  150. response, err := api.DeleteWithoutHeaders(deleteJsonMethodName, nil)
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. assert.Equal(t, deleteJsonResponse, string(response), "响应不一致")
  155. daprResponse, err := daprAPI.DeleteWithoutHeaders(deleteJsonMethodName, nil)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. assert.Equal(t, deleteJsonResponse, string(daprResponse), "响应不一致")
  160. wg.Wait()
  161. _ = srv.Shutdown(context.Background())
  162. }
  163. func TestGetWithoutHeaaders(t *testing.T) {
  164. api := invoke.NewAPI(baseUrl, 10*time.Second)
  165. defer invoke.DestroyAPI(api)
  166. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  167. defer invoke.DestroyAPI(daprAPI)
  168. wg := &sync.WaitGroup{}
  169. wg.Add(2)
  170. wgServer := &sync.WaitGroup{}
  171. wgServer.Add(1)
  172. srv := initInvokeHttpServer(t, wgServer, wg)
  173. wgServer.Wait()
  174. response, err := api.GetWithoutHeaders(getJsonMethodName, nil)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. assert.Equal(t, getJsonResponse, string(response), "响应不一致")
  179. daprResponse, err := daprAPI.GetWithoutHeaders(getJsonMethodName, nil)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. assert.Equal(t, getJsonResponse, string(daprResponse), "响应不一致")
  184. wg.Wait()
  185. _ = srv.Shutdown(context.Background())
  186. }
  187. func TestPost(t *testing.T) {
  188. api := invoke.NewAPI(baseUrl, 10*time.Second)
  189. defer invoke.DestroyAPI(api)
  190. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  191. defer invoke.DestroyAPI(daprAPI)
  192. wg := &sync.WaitGroup{}
  193. wg.Add(2)
  194. wgServer := &sync.WaitGroup{}
  195. wgServer.Add(1)
  196. srv := initInvokeHttpServer(t, wgServer, wg)
  197. wgServer.Wait()
  198. response, err := api.Post(postMethodName, contentTypeHeader, []byte(postData))
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. assert.Equal(t, postResponse, string(response), "响应不一致")
  203. daprResponse, err := daprAPI.Post(postMethodName, contentTypeHeader, []byte(postData))
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. assert.Equal(t, postResponse, string(daprResponse), "响应不一致")
  208. wg.Wait()
  209. _ = srv.Shutdown(context.Background())
  210. }
  211. func TestDelete(t *testing.T) {
  212. api := invoke.NewAPI(baseUrl, 10*time.Second)
  213. defer invoke.DestroyAPI(api)
  214. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  215. defer invoke.DestroyAPI(daprAPI)
  216. wg := &sync.WaitGroup{}
  217. wg.Add(2)
  218. wgServer := &sync.WaitGroup{}
  219. wgServer.Add(1)
  220. srv := initInvokeHttpServer(t, wgServer, wg)
  221. wgServer.Wait()
  222. response, err := api.Delete(deleteMethodName, nil, contentTypeHeader)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. assert.Equal(t, deleteResponse, string(response), "响应不一致")
  227. daprResponse, err := daprAPI.Delete(deleteMethodName, nil, contentTypeHeader)
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. assert.Equal(t, deleteResponse, string(daprResponse), "响应不一致")
  232. wg.Wait()
  233. _ = srv.Shutdown(context.Background())
  234. }
  235. func TestPut(t *testing.T) {
  236. api := invoke.NewAPI(baseUrl, 10*time.Second)
  237. defer invoke.DestroyAPI(api)
  238. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  239. defer invoke.DestroyAPI(daprAPI)
  240. wg := &sync.WaitGroup{}
  241. wg.Add(2)
  242. wgServer := &sync.WaitGroup{}
  243. wgServer.Add(1)
  244. srv := initInvokeHttpServer(t, wgServer, wg)
  245. wgServer.Wait()
  246. response, err := api.Put(putMethodName, contentTypeHeader, []byte(putData))
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. assert.Equal(t, putResponse, string(response), "响应不一致")
  251. daprResponse, err := daprAPI.Put(putMethodName, contentTypeHeader, []byte(putData))
  252. if err != nil {
  253. t.Fatal(err)
  254. }
  255. assert.Equal(t, putResponse, string(daprResponse), "响应不一致")
  256. wg.Wait()
  257. _ = srv.Shutdown(context.Background())
  258. }
  259. func TestGet(t *testing.T) {
  260. api := invoke.NewAPI(baseUrl, 10*time.Second)
  261. defer invoke.DestroyAPI(api)
  262. daprAPI := invoke.NewAPI(daprBaseUrl, 10*time.Second)
  263. defer invoke.DestroyAPI(daprAPI)
  264. wg := &sync.WaitGroup{}
  265. wg.Add(2)
  266. wgServer := &sync.WaitGroup{}
  267. wgServer.Add(1)
  268. srv := initInvokeHttpServer(t, wgServer, wg)
  269. wgServer.Wait()
  270. response, err := api.Get(getMethodName, nil, contentTypeHeader)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. assert.Equal(t, getResponse, string(response), "响应不一致")
  275. daprResponse, err := daprAPI.Get(getMethodName, nil, contentTypeHeader)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. assert.Equal(t, getResponse, string(daprResponse), "响应不一致")
  280. wg.Wait()
  281. _ = srv.Shutdown(context.Background())
  282. }
  283. func initInvokeHttpServer(t *testing.T, wgServer *sync.WaitGroup, wg *sync.WaitGroup) *http.Server {
  284. mux := http.NewServeMux()
  285. mux.HandleFunc("/post-json", func(w http.ResponseWriter, r *http.Request) {
  286. request, err := io.ReadAll(r.Body)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if string(request) != postJsonData {
  291. t.Fatal(err)
  292. }
  293. fmt.Println("post-json Receive:", string(request))
  294. w.WriteHeader(http.StatusOK)
  295. _, err = w.Write([]byte(postJsonResponse))
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. wg.Done()
  300. })
  301. mux.HandleFunc("/delete-json", func(w http.ResponseWriter, r *http.Request) {
  302. fmt.Println("delete-json")
  303. w.WriteHeader(http.StatusOK)
  304. _, err := w.Write([]byte(deleteJsonResponse))
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. wg.Done()
  309. })
  310. mux.HandleFunc("/put-json", func(w http.ResponseWriter, r *http.Request) {
  311. request, err := io.ReadAll(r.Body)
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. if string(request) != putJsonData {
  316. t.Fatal(err)
  317. }
  318. fmt.Println("put-json Receive:", string(request))
  319. w.WriteHeader(http.StatusOK)
  320. _, err = w.Write([]byte(putJsonResponse))
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. wg.Done()
  325. })
  326. mux.HandleFunc("/get-json", func(w http.ResponseWriter, r *http.Request) {
  327. fmt.Println("get-json")
  328. w.WriteHeader(http.StatusOK)
  329. _, err := w.Write([]byte(getJsonResponse))
  330. if err != nil {
  331. t.Fatal(err)
  332. }
  333. wg.Done()
  334. })
  335. mux.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request) {
  336. request, err := io.ReadAll(r.Body)
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. if string(request) != postData {
  341. t.Fatal(err)
  342. }
  343. fmt.Println("post Receive:", string(request))
  344. w.WriteHeader(http.StatusOK)
  345. _, err = w.Write([]byte(postResponse))
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. wg.Done()
  350. })
  351. mux.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) {
  352. fmt.Println("delete")
  353. w.WriteHeader(http.StatusOK)
  354. _, err := w.Write([]byte(deleteResponse))
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. wg.Done()
  359. })
  360. mux.HandleFunc("/put", func(w http.ResponseWriter, r *http.Request) {
  361. request, err := io.ReadAll(r.Body)
  362. if err != nil {
  363. t.Fatal(err)
  364. }
  365. if string(request) != putData {
  366. t.Fatal(err)
  367. }
  368. fmt.Println("put Receive:", string(request))
  369. w.WriteHeader(http.StatusOK)
  370. _, err = w.Write([]byte(putResponse))
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. wg.Done()
  375. })
  376. mux.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
  377. fmt.Println("get")
  378. w.WriteHeader(http.StatusOK)
  379. _, err := w.Write([]byte(getResponse))
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. wg.Done()
  384. })
  385. srv := &http.Server{
  386. Addr: invokeServerAddress,
  387. Handler: mux,
  388. }
  389. go func() {
  390. wgServer.Done()
  391. err := srv.ListenAndServe()
  392. if err != nil && err != http.ErrServerClosed {
  393. panic(err)
  394. }
  395. }()
  396. return srv
  397. }