forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tm2): make HTTPClient support https (gnolang#1158)
Co-authored-by: Manfred Touron <[email protected]>
- Loading branch information
Showing
2 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package rpcclient | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jaekwon/testify/assert" | ||
) | ||
|
||
func Test_parseRemoteAddr(t *testing.T) { | ||
tt := []struct { | ||
remoteAddr string | ||
network, s, errContains string | ||
}{ | ||
{"127.0.0.1", "tcp", "127.0.0.1", ""}, | ||
{"https://example.com", "https", "example.com", ""}, | ||
{"wss://[::1]", "wss", "[::1]", ""}, | ||
// no error cases - they cannot happen! | ||
} | ||
|
||
for _, tc := range tt { | ||
n, s, err := parseRemoteAddr(tc.remoteAddr) | ||
if tc.errContains != "" { | ||
_ = assert.NotNil(t, err) && assert.Contains(t, err.Error(), tc.errContains) | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, n, tc.network) | ||
assert.Equal(t, s, tc.s) | ||
} | ||
} | ||
|
||
// Following tests check that we correctly translate http/https to tcp, | ||
// and other protocols are left intact from parseRemoteAddr() | ||
|
||
func Test_makeHTTPDialer(t *testing.T) { | ||
dl := makeHTTPDialer("https://.") | ||
_, err := dl("hello", "world") | ||
if assert.NotNil(t, err) { | ||
e := err.Error() | ||
assert.Contains(t, e, "dial tcp:", "should convert https to tcp") | ||
assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)") | ||
} | ||
} | ||
|
||
func Test_makeHTTPDialer_noConvert(t *testing.T) { | ||
dl := makeHTTPDialer("udp://.") | ||
_, err := dl("hello", "world") | ||
if assert.NotNil(t, err) { | ||
e := err.Error() | ||
assert.Contains(t, e, "dial udp:", "udp protocol should remain the same") | ||
assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)") | ||
} | ||
} |