privateTransactionManagerClient_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package ethclient
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/ethereum/go-ethereum/common"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. const (
  12. arbitraryBase64Data = "YXJiaXRyYXJ5IGRhdGE=" // = "arbitrary data"
  13. )
  14. func TestPrivateTransactionManagerClient_storeRaw(t *testing.T) {
  15. // mock tessera client
  16. expectedData := []byte("arbitrary data")
  17. expectedDataEPH := common.BytesToEncryptedPayloadHash(expectedData)
  18. arbitraryServer := newStoreRawServer()
  19. defer arbitraryServer.Close()
  20. testObject, err := newPrivateTransactionManagerClient(arbitraryServer.URL)
  21. assert.NoError(t, err)
  22. key, err := testObject.StoreRaw([]byte("arbitrary payload"), "arbitrary private from")
  23. assert.NoError(t, err)
  24. assert.Equal(t, expectedDataEPH, key)
  25. }
  26. func newStoreRawServer() *httptest.Server {
  27. arbitraryResponse := fmt.Sprintf(`
  28. {
  29. "key": "%s"
  30. }
  31. `, arbitraryBase64Data)
  32. mux := http.NewServeMux()
  33. mux.HandleFunc("/storeraw", func(w http.ResponseWriter, req *http.Request) {
  34. if req.Method == "POST" {
  35. // parse request
  36. var storeRawReq storeRawReq
  37. if err := json.NewDecoder(req.Body).Decode(&storeRawReq); err != nil {
  38. http.Error(w, err.Error(), http.StatusBadRequest)
  39. return
  40. }
  41. // send response
  42. _, _ = fmt.Fprintf(w, "%s", arbitraryResponse)
  43. } else {
  44. http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
  45. }
  46. })
  47. return httptest.NewServer(mux)
  48. }