Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate the cc when using city params #175

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Server struct {
DLSpeed float64 `json:"dl_speed"`
ULSpeed float64 `json:"ul_speed"`
TestDuration TestDuration `json:"test_duration"`
CC string `json:"cc"`

Context *Speedtest `json:"-"`
}
Expand Down Expand Up @@ -254,6 +255,15 @@ func (s *Speedtest) FetchServerListContext(ctx context.Context) (Servers, error)
if err = decoder.Decode(&servers); err != nil {
return servers, err
}
if s.config.Location != nil {
var tmpServers Servers
for _, server := range servers {
if server.CC == strings.ToUpper(s.config.Location.CC) {
tmpServers = append(tmpServers, server)
}
}
servers = tmpServers
}
case typeXMLPayload:
var list ServerList
// Decode xml
Expand Down
17 changes: 17 additions & 0 deletions speedtest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package speedtest
import (
"math"
"math/rand"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -195,3 +196,19 @@ func TestTotalDurationCount(t *testing.T) {
t.Error("addition in testDurationTotalCount didn't work")
}
}

func TestCityFlag(t *testing.T) {
client := New(WithUserConfig(&UserConfig{CityFlag: "yishun"}))
servers, err := client.FetchServers()
if err != nil {
t.Errorf(err.Error())
}
row := 1
for _, server := range servers {

if server.CC != strings.ToUpper(client.config.Location.CC) {
t.Error("server country code does not match client country code")
}
row++
}
}
gandol marked this conversation as resolved.
Show resolved Hide resolved