Added httptest.NewServer to DecodeFromURL and CryptoOnRamp tests

This commit is contained in:
Samuel Hawksby-Robinson 2022-08-09 23:54:44 +01:00
parent 399090a44b
commit 41cb9d7bea
5 changed files with 69 additions and 22 deletions

47
_assets/tests/ramps.json Normal file
View File

@ -0,0 +1,47 @@
[
{
"name": "Wyre",
"description": "A secure bridge for fiat and crypto",
"fees": "from 2.9%",
"region": "US & Europe",
"logoUrl":"https://www.sendwyre.com/favicon.ico",
"siteUrl": "https://pay.sendwyre.com/purchase",
"hostname": "sendwyre.com"
},
{
"name": "MoonPay",
"description": "The new standard for fiat to crypto",
"fees": "1%-4.5%",
"region": "US & Europe",
"logoUrl":"https://buy.moonpay.com/favicon-32x32.png",
"siteUrl": "https://buy.moonpay.com",
"hostname": "moonpay.com"
},
{
"name": "Transak",
"description": "Global fiat <-> crypto payment gateway",
"fees": "1%-4.5%",
"region": "Global",
"logoUrl":"https://global.transak.com/favicon.png",
"siteUrl": "https://global.transak.com",
"hostname": "transak.com"
},
{
"name": "Ramp",
"description": "Global crypto to fiat flow",
"fees": "1.5%",
"region": "Global",
"logoUrl":"https://ramp.network/assets/favicons/favicon-32x32.png",
"siteUrl": "https://ramp.network/buy/",
"hostname": "ramp.network"
},
{
"name": "LocalCryptos",
"description": "Non-custodial crypto marketplace",
"fees": "1.5%",
"region": "Global",
"logoUrl":"https://localcryptos.com/images/favicon.png",
"siteUrl": "https://localcryptos.com",
"hostname": "localcryptos.com"
}
]

View File

@ -4,9 +4,8 @@ import (
"errors"
"image"
"net/http"
"net/url"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
@ -88,16 +87,8 @@ func TestDecode(t *testing.T) {
}
func TestDecodeFromURL(t *testing.T) {
u := url.URL{
Scheme: "http",
Host: "localhost:8089",
}
go func() {
err := http.ListenAndServe(u.Host, http.FileServer(http.Dir("../_assets/tests")))
require.NoError(t, err)
}()
time.Sleep(100 * time.Millisecond)
s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close()
cs := []struct {
Filepath string
@ -105,7 +96,7 @@ func TestDecodeFromURL(t *testing.T) {
Bounds image.Rectangle
}{
{
u.String() + "/2x1.png",
s.URL + "/2x1.png",
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
@ -113,7 +104,7 @@ func TestDecodeFromURL(t *testing.T) {
},
},
{
u.String() + "/1.jpg",
s.URL + "/1.jpg",
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
@ -121,7 +112,7 @@ func TestDecodeFromURL(t *testing.T) {
},
},
{
u.String() + "/1.gif",
s.URL + "/1.gif",
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
@ -129,7 +120,7 @@ func TestDecodeFromURL(t *testing.T) {
},
},
{
u.String() + "/1.webp",
s.URL + "/1.webp",
false,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},
@ -137,7 +128,7 @@ func TestDecodeFromURL(t *testing.T) {
},
},
{
u.String() + "/1.webp",
s.URL + "/1.webp",
true,
image.Rectangle{
Min: image.Point{X: 0, Y: 0},

View File

@ -119,7 +119,6 @@ func LinkPreviewWhitelist() []Site {
}
func GetURLContent(url string) (data []byte, err error) {
// nolint: gosec
response, err := httpClient.Get(url)
if err != nil {
return data, fmt.Errorf("can't get content from link %s", url)

View File

@ -10,8 +10,6 @@ import (
)
const (
cryptoOnRampsData = "https://raw.githubusercontent.com/status-im/crypto-on-ramps/master/ramps.json"
DataSourceHTTP DataSourceType = iota + 1
DataSourceStatic
)

View File

@ -1,18 +1,27 @@
package wallet
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
path = "../../_assets/tests/"
)
func TestCryptoOnRamps_Get(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close()
cs := []*CryptoOnRampManager{
{options: &CryptoOnRampOptions{dataSourceType: DataSourceStatic}},
{options: &CryptoOnRampOptions{
dataSourceType: DataSourceHTTP,
dataSource: cryptoOnRampsData,
dataSource: s.URL + "/ramps.json",
}},
}
@ -26,9 +35,12 @@ func TestCryptoOnRamps_Get(t *testing.T) {
}
func TestCryptoOnRampManager_hasCacheExpired(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close()
corm := NewCryptoOnRampManager(&CryptoOnRampOptions{
dataSourceType: DataSourceHTTP,
dataSource: cryptoOnRampsData,
dataSource: s.URL + "/ramps.json",
})
nt := time.Time{}.Add(30 * time.Minute)