github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/courier/client/client_test.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "testing" 6 "time" 7 8 "github.com/johnnyeven/libtools/courier" 9 10 "github.com/davecgh/go-spew/spew" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/johnnyeven/libtools/courier/status_error" 14 ) 15 16 type IpInfo struct { 17 Country string `json:"country"` 18 CountryCode string `json:"countryCode"` 19 } 20 21 func TestClient_DoRequest(t *testing.T) { 22 tt := assert.New(t) 23 24 ipInfoClient := Client{ 25 Service: "test", 26 Mode: "http", 27 Host: "ip-api.com", 28 Timeout: 100 * time.Second, 29 } 30 31 { 32 ipInfo := IpInfo{} 33 err := ipInfoClient. 34 Request("id", "GET", "/json", nil). 35 Do(). 36 Into(&ipInfo) 37 38 if err == nil { 39 tt.Equal("China", ipInfo.Country) 40 tt.Equal("CN", ipInfo.CountryCode) 41 } else { 42 spew.Dump(err.(*status_error.StatusError)) 43 } 44 } 45 { 46 data, _ := json.Marshal(IpInfo{ 47 Country: "USA", 48 CountryCode: "US", 49 }) 50 51 ipInfo := IpInfo{} 52 53 mockData := MetadataWithMocker(Mock(ipInfoClient.Service).For("id", MockData{ 54 Data: data, 55 })) 56 57 err := ipInfoClient.Request("id", "GET", "/json", nil, mockData).Do().Into(&ipInfo) 58 59 tt.Nil(err) 60 tt.Equal("USA", ipInfo.Country) 61 tt.Equal("US", ipInfo.CountryCode) 62 } 63 } 64 65 func TestClient_DoRequestWithMetadata(t *testing.T) { 66 tt := assert.New(t) 67 68 ipInfoClient := Client{ 69 Service: "test", 70 Mode: "http", 71 Host: "ip-api.com", 72 Timeout: 100 * time.Second, 73 } 74 75 ipInfo := IpInfo{} 76 77 err := ipInfoClient.Request("id", "GET", "/json", nil, courier.MetadataWithVersionSwitch("VERSION")). 78 Do(). 79 Into(&ipInfo) 80 81 tt.Nil(err) 82 83 spew.Dump(ipInfo) 84 }