@@ -6,13 +6,14 @@ package hiero
66// SPDX-License-Identifier: Apache-2.0
77
88import (
9+ "encoding/hex"
10+ "encoding/json"
11+ "net/http"
12+ "net/http/httptest"
913 "strings"
1014 "testing"
1115
1216 "github.com/stretchr/testify/assert"
13-
14- "encoding/hex"
15-
1617 "github.com/stretchr/testify/require"
1718)
1819
@@ -276,3 +277,125 @@ func TestUnitAccountIDToEvmAddress(t *testing.T) {
276277 id = AccountID {Shard : 1 , Realm : 1 , AliasEvmAddress : & bytes }
277278 require .Equal (t , expected , id .ToEvmAddress ())
278279}
280+
281+ func TestUnitAccountIDPopulateWithDifferentPorts (t * testing.T ) {
282+ // Note: Not running in parallel since we modify global http.DefaultTransport
283+
284+ tests := []struct {
285+ name string
286+ domain string
287+ expectedScheme string
288+ description string
289+ }{
290+ {
291+ name : "port 80 uses HTTP" ,
292+ domain : "mirror80.example.com:80" ,
293+ expectedScheme : "http" ,
294+ description : "Port 80 should use HTTP scheme" ,
295+ },
296+ {
297+ name : "port 443 uses HTTPS" ,
298+ domain : "mirror443.example.com:443" ,
299+ expectedScheme : "https" ,
300+ description : "Port 443 should use HTTPS scheme" ,
301+ },
302+ {
303+ name : "port 8443 uses HTTPS" ,
304+ domain : "mirror8443.example.com:8443" ,
305+ expectedScheme : "https" ,
306+ description : "Other ports should use HTTPS scheme for security" ,
307+ },
308+ {
309+ name : "port 9999 uses HTTPS" ,
310+ domain : "mirror9999.example.com:9999" ,
311+ expectedScheme : "https" ,
312+ description : "Any non-standard port should use HTTPS scheme" ,
313+ },
314+ }
315+
316+ for _ , test := range tests {
317+ t .Run (test .name , func (t * testing.T ) {
318+ t .Run ("PopulateAccount" , func (t * testing.T ) {
319+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
320+ assert .Contains (t , r .URL .Path , "accounts" )
321+
322+ response := map [string ]interface {}{
323+ "account" : "0.0.12345" ,
324+ }
325+ w .Header ().Set ("Content-Type" , "application/json" )
326+ err := json .NewEncoder (w ).Encode (response )
327+ require .NoError (t , err )
328+ }))
329+ defer server .Close ()
330+
331+ // Setup mock transport
332+ cleanup := SetupMockTransportForDomain (test .domain , server .URL )
333+ defer cleanup ()
334+
335+ // Setup client with the test domain as the mirror network
336+ client , err := _NewMockClient ()
337+ require .NoError (t , err )
338+ client .SetLedgerID (* NewLedgerIDTestnet ())
339+ client .SetMirrorNetwork ([]string {test .domain })
340+
341+ // Create an account ID with EVM address
342+ evmAddressBytes , err := hex .DecodeString (evmAddress )
343+ require .NoError (t , err )
344+ accountID := AccountID {
345+ Shard : 0 ,
346+ Realm : 0 ,
347+ Account : 0 ,
348+ AliasEvmAddress : & evmAddressBytes ,
349+ }
350+
351+ // Test PopulateAccount
352+ err = accountID .PopulateAccount (client )
353+ require .NoError (t , err , "PopulateAccount should succeed for %s" , test .description )
354+ assert .Equal (t , uint64 (12345 ), accountID .Account )
355+ })
356+
357+ // Test PopulateEvmAddress
358+ t .Run ("PopulateEvmAddress" , func (t * testing.T ) {
359+ // Create a mock server that responds with EVM address data
360+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
361+ // Verify the request path
362+ assert .Contains (t , r .URL .Path , "accounts/0.0.789" )
363+
364+ response := map [string ]interface {}{
365+ "evm_address" : "0x" + evmAddress ,
366+ }
367+ w .Header ().Set ("Content-Type" , "application/json" )
368+ err := json .NewEncoder (w ).Encode (response )
369+ require .NoError (t , err )
370+ }))
371+ defer server .Close ()
372+
373+ // Setup mock transport
374+ cleanup := SetupMockTransportForDomain (test .domain , server .URL )
375+ defer cleanup ()
376+
377+ // Setup client with the test domain as the mirror network
378+ client , err := _NewMockClient ()
379+ require .NoError (t , err )
380+ client .SetLedgerID (* NewLedgerIDTestnet ())
381+ client .SetMirrorNetwork ([]string {test .domain })
382+
383+ // Create an account ID with account number
384+ accountID := AccountID {
385+ Shard : 0 ,
386+ Realm : 0 ,
387+ Account : 789 ,
388+ }
389+
390+ // Test PopulateEvmAddress
391+ err = accountID .PopulateEvmAddress (client )
392+ require .NoError (t , err , "PopulateEvmAddress should succeed for %s" , test .description )
393+ require .NotNil (t , accountID .AliasEvmAddress )
394+
395+ expectedBytes , err := hex .DecodeString (evmAddress )
396+ require .NoError (t , err )
397+ assert .Equal (t , expectedBytes , * accountID .AliasEvmAddress )
398+ })
399+ })
400+ }
401+ }
0 commit comments