package ipMgr import ( "fmt" "testing" ) func TestQuery(t *testing.T) { IP_SERVICE_URL = "http://ipip.7qule.com/query" appId := "unittest" appId_wrong := "wrong" appSecret := "c5746980-5d52-4ba9-834f-13d0066ad1d0" appSecret_wrong := "wrong" ip := "117.139.247.210" ip_wrong := "wrong" isDomestic := true continent := "" country := "中国" region := "四川" city := "成都" timeout := 3 // Test with wrong AppId ipInfoObj, err := Query(appId_wrong, appSecret, ip, isDomestic, timeout) if err == nil { t.Errorf("There should be an error, but now there is no error") return } // Test with wrong AppSecret ipInfoObj, err = Query(appId, appSecret_wrong, ip, isDomestic, timeout) if err == nil { t.Errorf("There should be an error, but now there is no error") return } // Test with wrong IP ipInfoObj, err = Query(appId, appSecret, ip_wrong, isDomestic, timeout) if err == nil { t.Errorf("There should be an error, but now there is no error") return } // Test with correct information and domestic ip ipInfoObj, err = Query(appId, appSecret, ip, isDomestic, timeout) if err != nil { t.Errorf("There should be no error, but now there is one:%s", err) return } fmt.Printf("ipInfoObj:%v\n", ipInfoObj) if ipInfoObj.Continent != continent { t.Errorf("Expected continent %s, but got %s", continent, ipInfoObj.Continent) } if ipInfoObj.Country != country { t.Errorf("Expected country %s, but got %s", country, ipInfoObj.Country) } if ipInfoObj.Region != region { t.Errorf("Expected region %s, but got %s", region, ipInfoObj.Region) } if ipInfoObj.City != city { t.Errorf("Expected city %s, but got %s", city, ipInfoObj.City) } // Test with correct information and foreign ip isDomestic = false continent = "亚洲" country = "中国" region = "四川省" city = "成都" ipInfoObj, err = Query(appId, appSecret, ip, isDomestic, timeout) if err != nil { t.Errorf("There should be no error, but now there is one:%s", err) return } fmt.Printf("ipInfoObj:%v\n", ipInfoObj) if ipInfoObj.Continent != continent { t.Errorf("Expected continent %s, but got %s", continent, ipInfoObj.Continent) } if ipInfoObj.Country != country { t.Errorf("Expected country %s, but got %s", country, ipInfoObj.Country) } if ipInfoObj.Region != region { t.Errorf("Expected region %s, but got %s", region, ipInfoObj.Region) } if ipInfoObj.City != city { t.Errorf("Expected city %s, but got %s", city, ipInfoObj.City) } }