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