http.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package base
  18. import (
  19. "bytes"
  20. "encoding/json"
  21. "io"
  22. "io/ioutil"
  23. "mime/multipart"
  24. "net/http"
  25. "net/url"
  26. "os"
  27. "path/filepath"
  28. "strings"
  29. "github.com/stretchr/testify/assert"
  30. )
  31. type UploadFile struct {
  32. Name string
  33. Filepath string
  34. }
  35. func HttpGet(url string, headers map[string]string) ([]byte, int, error) {
  36. return httpRequest(http.MethodGet, url, headers, "")
  37. }
  38. func HttpDelete(url string, headers map[string]string) ([]byte, int, error) {
  39. return httpRequest(http.MethodDelete, url, headers, "")
  40. }
  41. func HttpPut(url string, headers map[string]string, reqBody string) ([]byte, int, error) {
  42. return httpRequest(http.MethodPut, url, headers, reqBody)
  43. }
  44. func HttpPost(url string, headers map[string]string, reqBody string) ([]byte, int, error) {
  45. return httpRequest(http.MethodPost, url, headers, reqBody)
  46. }
  47. func httpRequest(method, url string, headers map[string]string, reqBody string) ([]byte, int, error) {
  48. var requestBody = new(bytes.Buffer)
  49. if reqBody != "" {
  50. requestBody = bytes.NewBuffer([]byte(reqBody))
  51. }
  52. req, err := http.NewRequest(method, url, requestBody)
  53. if err != nil {
  54. return nil, 0, err
  55. }
  56. req.Close = true
  57. // set header
  58. for key, val := range headers {
  59. req.Header.Add(key, val)
  60. }
  61. client := &http.Client{}
  62. resp, err := client.Do(req)
  63. if err != nil {
  64. return nil, 0, err
  65. }
  66. defer resp.Body.Close()
  67. body, err := ioutil.ReadAll(resp.Body)
  68. if err != nil {
  69. return nil, 0, err
  70. }
  71. return body, resp.StatusCode, nil
  72. }
  73. func BatchTestServerPort(times int, headers map[string]string, queryString string) map[string]int {
  74. t := getTestingHandle()
  75. url := APISIXSingleWorkerHost + "/server_port"
  76. if queryString != "" {
  77. url = url + "?" + queryString
  78. }
  79. res := map[string]int{}
  80. for i := 0; i < times; i++ {
  81. bodyByte, status, err := HttpGet(url, headers)
  82. assert.Nil(t, err)
  83. assert.Equal(t, 200, status)
  84. body := string(bodyByte)
  85. if _, ok := res[body]; !ok {
  86. res[body] = 1
  87. } else {
  88. res[body] += 1
  89. }
  90. }
  91. return res
  92. }
  93. func GetReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string, error) {
  94. if strings.Index(contentType, "json") > -1 {
  95. bytesData, _ := json.Marshal(reqParams)
  96. return bytes.NewReader(bytesData), contentType, nil
  97. }
  98. if files != nil {
  99. body := &bytes.Buffer{}
  100. writer := multipart.NewWriter(body)
  101. for _, uploadFile := range files {
  102. file, err := os.Open(uploadFile.Filepath)
  103. if err != nil {
  104. return nil, "", err
  105. }
  106. defer file.Close()
  107. part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath))
  108. if err != nil {
  109. return nil, "", err
  110. }
  111. _, err = io.Copy(part, file)
  112. }
  113. for k, v := range reqParams {
  114. if err := writer.WriteField(k, v); err != nil {
  115. return nil, "", err
  116. }
  117. }
  118. if err := writer.Close(); err != nil {
  119. return nil, "", err
  120. }
  121. return body, writer.FormDataContentType(), nil
  122. }
  123. urlValues := url.Values{}
  124. for key, val := range reqParams {
  125. urlValues.Set(key, val)
  126. }
  127. reqBody := urlValues.Encode()
  128. return strings.NewReader(reqBody), contentType, nil
  129. }