github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/scrape/notify_test.go (about) 1 package scrapePkg 2 3 import ( 4 "encoding/json" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "sync" 9 "testing" 10 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/notify" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 13 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/uniq" 14 ) 15 16 func TestNotify(t *testing.T) { 17 results := make([]string, 0) 18 var mutex sync.Mutex 19 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 20 body, err := io.ReadAll(r.Body) 21 if err != nil { 22 t.Fatal(err) 23 } 24 defer r.Body.Close() 25 26 mutex.Lock() 27 results = append(results, string(body)) 28 mutex.Unlock() 29 })) 30 defer ts.Close() 31 32 newAppNotification := notify.Notification[[]notify.NotificationPayloadAppearance]{ 33 Msg: notify.MessageAppearance, 34 Meta: &types.MetaData{Chain: "ethereum"}, 35 Payload: []notify.NotificationPayloadAppearance{ 36 { 37 Address: "0xfffd8963efd1fc6a506488495d951d5263988d25", 38 BlockNumber: "18509161", 39 TransactionIndex: 132, 40 }, 41 }, 42 } 43 44 if err := notifyEndpoint(ts.URL, newAppNotification); err != nil { 45 t.Fatal(err) 46 } 47 48 result := results[0] 49 expected, err := json.Marshal(newAppNotification) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if result != string(expected) { 55 t.Fatalf("wrong result: %+v", result) 56 } 57 } 58 59 func TestNotificationDataAppearance_FromString(t *testing.T) { 60 addrMap := make(uniq.AddressBooleanMap, 0) 61 key := addrMap.Insert( 62 "0xfffd8963efd1fc6a506488495d951d5263988d25", 63 18509161, 64 132, 65 ) 66 67 n := ¬ify.NotificationPayloadAppearance{} 68 if err := n.FromString(key); err != nil { 69 t.Fatal(err) 70 } 71 72 if addr := n.Address; addr != "0xfffd8963efd1fc6a506488495d951d5263988d25" { 73 t.Fatal("address", addr) 74 } 75 if bn := n.BlockNumber; bn != "18509161" { 76 t.Fatal("wrong block number", bn) 77 } 78 if txid := n.TransactionIndex; txid != 132 { 79 t.Fatal("wrong transaction id", txid) 80 } 81 }