service_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/tls"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. api "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/api"
  10. )
  11. const baseURL = "http://127.0.0.1:9080"
  12. const baseSslURL = "https://127.0.0.1:9443"
  13. const restEndpoints = "/api/v1/"
  14. const username = "admin"
  15. const passwd = "admin"
  16. const systemID = "autorest-srv"
  17. var AdminUser = BasicUser{name: "admin", pwd: "admin"}
  18. var EditorUser = BasicUser{name: "editor", pwd: "editor"}
  19. var GuestUser = BasicUser{name: "guest", pwd: "guest"}
  20. type BasicUser struct {
  21. name string
  22. pwd string
  23. }
  24. func getClient() http.Client {
  25. tr := &http.Transport{
  26. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  27. }
  28. return http.Client{Transport: tr}
  29. }
  30. func getTestApikey() string {
  31. value := fmt.Sprintf("%s_%s", servicename, systemID)
  32. apikey := fmt.Sprintf("%x", md5.Sum([]byte(value)))
  33. return strings.ToLower(apikey)
  34. }
  35. func getGetRequest(url string, user BasicUser) (*http.Response, error) {
  36. client := getClient()
  37. req, err := http.NewRequest("GET", url, nil)
  38. if err != nil {
  39. return nil, err
  40. }
  41. req.SetBasicAuth(user.name, user.pwd)
  42. req.Header.Add(api.APIKeyHeader, getTestApikey())
  43. req.Header.Add(api.SystemHeader, systemID)
  44. return client.Do(req)
  45. }
  46. func getPostRequest(url string, user BasicUser, payload []byte) (*http.Response, error) {
  47. client := getClient()
  48. req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
  49. if err != nil {
  50. return nil, err
  51. }
  52. req.SetBasicAuth(user.name, user.pwd)
  53. req.Header.Add(api.APIKeyHeader, getTestApikey())
  54. req.Header.Add(api.SystemHeader, systemID)
  55. req.Header.Set("Content-Type", "application/json")
  56. return client.Do(req)
  57. }
  58. func getDeleteRequest(url string, user BasicUser) (*http.Response, error) {
  59. client := getClient()
  60. req, err := http.NewRequest("DELETE", url, nil)
  61. if err != nil {
  62. return nil, err
  63. }
  64. req.SetBasicAuth(user.name, user.pwd)
  65. req.Header.Add(api.APIKeyHeader, getTestApikey())
  66. req.Header.Add(api.SystemHeader, systemID)
  67. return client.Do(req)
  68. }