class_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "io"
  8. "net/http"
  9. "regexp"
  10. "testing"
  11. "time"
  12. )
  13. func Test_send(t *testing.T) {
  14. var classReq []ClassReq
  15. classReq = append(classReq, ClassReq{
  16. SeriesName: "故宫博物院11",
  17. SeriesIssuer: "海南边界智能科技有限公司",
  18. ExternalUrl: "http://github.com",
  19. SeriesDes: "",
  20. SeriesId: []string{"0xF3C2fFA43d20599b1a4163CB3cCB61B57D0624CB"},
  21. TotalDNA: 100,
  22. AssetContracts: "did:bid:ef28nLJWiyEX8x61qXohtZhSadwEuZD28",
  23. })
  24. objJson, err := json.Marshal(&Req{
  25. Data: classReq,
  26. })
  27. if err != nil {
  28. t.Log(err)
  29. return
  30. }
  31. // payload := bytes.NewReader(objJson)
  32. // req, err := http.Post("https://test-dna.bitfactory.cn/auth/api/v1/series", "application/json", bytes.NewBuffer(objJson))
  33. req, err := http.NewRequest("POST", "https://test-dna.bitfactory.cn/auth/api/v1/series", bytes.NewBuffer(objJson))
  34. if err != nil {
  35. t.Log(err)
  36. return
  37. }
  38. req.Header.Add("Content-Type", "application/json")
  39. req.Header.Add("accessToken", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBcHBJZCI6IjFiNTQyNWExLTBlYjAtNDM3Yy1iZjU2LWEyZTZkMDhkOTUyZiIsIkJpZlVzZXJCaWQiOiJkaWQ6YmlkOmVmWmR2NXJKTERiU0xGcFFwUENydDkzZXhGOFU2MnR3IiwiZXhwIjoxNzA2MjcxOTM3LCJpYXQiOjE3MDYyNjQ3Mzd9.RybVRFHzH4w7EiAF3vF-gkz6dPmTi0qyJZvg4fGPNPQ")
  40. client := &http.Client{Timeout: time.Duration(3) * time.Second}
  41. res, err := client.Do(req)
  42. if err != nil {
  43. t.Log(err)
  44. return
  45. }
  46. body, err := io.ReadAll(res.Body)
  47. if err != nil {
  48. t.Log(err)
  49. return
  50. }
  51. t.Log(string(body))
  52. // var resp Resp
  53. // err = json.Unmarshal(body, &resp)
  54. // if err != nil {
  55. // t.Log(err)
  56. // return
  57. // }
  58. // if resp.RetCode != http.StatusOK || resp.RetMsg != "ok" {
  59. // t.Log(resp.RetCode, resp.RetMsg)
  60. // return
  61. // }
  62. }
  63. func Test_hash(t *testing.T) {
  64. data := []byte("http://github.com")
  65. // 使用SHA-256哈希算法计算哈希值
  66. hash := sha256.Sum256(data)
  67. // 将哈希值转换为十六进制字符串
  68. hashStr := hex.EncodeToString(hash[:])
  69. t.Log(hashStr)
  70. }
  71. func Test_Reg(t *testing.T) {
  72. pattern := `^tokenBID:did:bid:.*重复$`
  73. // 编译正则表达式
  74. regex, err := regexp.Compile(pattern)
  75. if err != nil {
  76. t.Log("正则表达式编译失败:", err)
  77. return
  78. }
  79. if regex.MatchString("tokenBID:did:bid:efLqdkWTLUFFKaDCRWFrr8j5RUB1euka重复") {
  80. t.Log("匹配成功!")
  81. } else {
  82. t.Log("未匹配到符合条件的字符串.")
  83. }
  84. }