add more checks in VersionDataLoader.LoadServers, add more tests
This commit is contained in:
parent
4de5ad58d8
commit
232d9caad3
|
@ -59,11 +59,21 @@ func (dl *VersionDataLoader) LoadServers() ([]*Server, error) {
|
||||||
}
|
}
|
||||||
return nil, fmtedErr
|
return nil, fmtedErr
|
||||||
}
|
}
|
||||||
|
bodyMap, ok := body.(map[interface{}]interface{})
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.Errorf("expected map, got %T", body)
|
||||||
|
}
|
||||||
|
|
||||||
var servers []*Server
|
var servers []*Server
|
||||||
for serverKey, url := range body.(map[interface{}]interface{}) {
|
for serverKey, url := range bodyMap {
|
||||||
serverKeyStr := serverKey.(string)
|
serverKeyStr, ok := serverKey.(string)
|
||||||
urlStr := url.(string)
|
if !ok {
|
||||||
|
return nil, errors.Errorf("expected string as the key of the map, got %T", serverKey)
|
||||||
|
}
|
||||||
|
urlStr, ok := url.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.Errorf("expected string as the value of the map, got %T", url)
|
||||||
|
}
|
||||||
if serverKeyStr != "" && urlStr != "" {
|
if serverKeyStr != "" && urlStr != "" {
|
||||||
servers = append(servers, &Server{
|
servers = append(servers, &Server{
|
||||||
Key: serverKeyStr,
|
Key: serverKeyStr,
|
||||||
|
|
|
@ -26,19 +26,49 @@ func prepareTestServer(resp string) *httptest.Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadServers(t *testing.T) {
|
func TestLoadServers(t *testing.T) {
|
||||||
t.Run("invalid payload", func(t *testing.T) {
|
t.Run("invalid response", func(t *testing.T) {
|
||||||
resp := `:"https://pl165.plemiona.pl";s:5:"pl166";s:25:"https://pl166.plemiona.pl";s:5:"pl167";s:25:"https://pl167.plemiona.pl";}`
|
type scenario struct {
|
||||||
ts := prepareTestServer(resp)
|
resp string
|
||||||
defer ts.Close()
|
expectedErrMsg string
|
||||||
|
}
|
||||||
|
|
||||||
dl := NewVersionDataLoader(&VersionDataLoaderConfig{
|
scenarios := []scenario{
|
||||||
Host: strings.ReplaceAll(ts.URL, "https://", ""),
|
{
|
||||||
Client: ts.Client(),
|
resp: `:"https://pl165.plemiona.pl";s:5:"pl166";s:25:"https://pl166.plemiona.pl";s:5:"pl167";s:25:"https://pl167.plemiona.pl";}`,
|
||||||
})
|
expectedErrMsg: "couldn't decode the response body into a go value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resp: `a:19:{s:5:"pl150"s:25"https://pl150.plemiona.pl";}`,
|
||||||
|
expectedErrMsg: "expected string as the value of the map, got <nil>",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resp: "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}",
|
||||||
|
expectedErrMsg: "expected string as the key of the map, got int64",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resp: `O:8:"stdClass":0:{}`,
|
||||||
|
expectedErrMsg: "expected map, got *phpserialize.PhpObject",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resp: `a:2:{s:3:"asd";i:123;s:4:"asd2";i:123;}`,
|
||||||
|
expectedErrMsg: "expected string as the value of the map, got int64",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
_, err := dl.LoadServers()
|
for _, scenario := range scenarios {
|
||||||
assert.NotNil(t, err)
|
ts := prepareTestServer(scenario.resp)
|
||||||
assert.Contains(t, err.Error(), "couldn't decode the response body into a go value")
|
|
||||||
|
dl := NewVersionDataLoader(&VersionDataLoaderConfig{
|
||||||
|
Host: strings.ReplaceAll(ts.URL, "https://", ""),
|
||||||
|
Client: ts.Client(),
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := dl.LoadServers()
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
assert.Contains(t, err.Error(), scenario.expectedErrMsg)
|
||||||
|
|
||||||
|
ts.Close()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("success", func(t *testing.T) {
|
t.Run("success", func(t *testing.T) {
|
||||||
|
|
Reference in New Issue