Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package fileUtil
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// 对文件进行gzip压缩
|
||||
// source:源文件完整路径
|
||||
// target:目标文件文件夹(如果传空字符串,则为当前文件夹)
|
||||
// 返回值
|
||||
// 错误对象
|
||||
func Gzip(source, target string) error {
|
||||
reader, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
// 给目标文件夹赋值,如果传空,则默认为当前文件夹
|
||||
if target == "" {
|
||||
target = filepath.Dir(source)
|
||||
}
|
||||
fileName := filepath.Base(source)
|
||||
|
||||
targetFilePath := filepath.Join(target, fmt.Sprintf("%s.gz", fileName))
|
||||
writer, err := os.Create(targetFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer writer.Close()
|
||||
|
||||
archiver := gzip.NewWriter(writer)
|
||||
archiver.Name = fileName
|
||||
defer archiver.Close()
|
||||
|
||||
_, err = io.Copy(archiver, reader)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 对文件进行gzip解压缩
|
||||
// source:源文件完整路径
|
||||
// target:目标文件文件夹(解压缩文件的名字是内部自动赋值)
|
||||
// 返回值
|
||||
// 错误对象
|
||||
func UnGzip(source, target string) error {
|
||||
reader, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
archive, err := gzip.NewReader(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer archive.Close()
|
||||
|
||||
// 给目标文件夹赋值,如果传空,则默认为当前文件夹
|
||||
if target == "" {
|
||||
target = filepath.Dir(source)
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(target, archive.Name)
|
||||
writer, err := os.Create(targetFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer writer.Close()
|
||||
|
||||
_, err = io.Copy(writer, archive)
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user