-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathipinfo_core_test.rb
More file actions
126 lines (108 loc) · 4.87 KB
/
ipinfo_core_test.rb
File metadata and controls
126 lines (108 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
require_relative 'test_helper'
class IPinfoCoreTest < Minitest::Test
TEST_IPV4 = '8.8.8.8'
TEST_IPV6 = '2001:4860:4860::8888'
def test_set_adapter
ipinfo = IPinfoCore.create(
ENV.fetch('IPINFO_TOKEN', nil),
{ 'http_client' => :excon }
)
assert(ipinfo.httpc = :excon)
end
def test_lookup_ip4
ipinfo = IPinfoCore.create(ENV.fetch('IPINFO_TOKEN', nil))
# multiple checks for cache
(0...5).each do |_|
resp = ipinfo.details(TEST_IPV4)
# Basic fields
assert_equal(resp.ip, TEST_IPV4)
assert_equal(resp.ip_address, IPAddr.new(TEST_IPV4))
assert_equal(resp.hostname, 'dns.google')
# Geo object assertions
assert(resp.geo.is_a?(Hash))
refute_nil(resp.geo[:city])
refute_nil(resp.geo[:region])
refute_nil(resp.geo[:region_code])
assert_equal(resp.geo[:country_code], 'US')
assert_equal(resp.geo[:country], 'United States')
assert_equal(resp.geo[:country_name], 'United States')
assert_equal(resp.geo[:is_eu], false)
refute_nil(resp.geo[:continent])
refute_nil(resp.geo[:continent_code])
refute_nil(resp.geo[:latitude])
refute_nil(resp.geo[:longitude])
refute_nil(resp.geo[:timezone])
refute_nil(resp.geo[:postal_code])
assert_equal(resp.geo[:country_flag]['emoji'], '🇺🇸')
assert_equal(resp.geo[:country_flag]['unicode'], 'U+1F1FA U+1F1F8')
assert_equal(resp.geo[:country_flag_url], 'https://cdn.ipinfo.io/static/images/countries-flags/US.svg')
assert_equal(resp.geo[:country_currency]['code'], 'USD')
assert_equal(resp.geo[:country_currency]['symbol'], '$')
assert_equal(resp.geo[:continent]['code'], 'NA')
assert_equal(resp.geo[:continent]['name'], 'North America')
# AS object assertions
assert(resp.as.is_a?(Hash))
assert_equal(resp.as[:asn], 'AS15169')
assert(resp.as[:name].is_a?(String))
assert(resp.as[:domain].is_a?(String))
assert(resp.as[:type].is_a?(String))
# Network flags
assert_equal(resp.is_anonymous, false)
assert_equal(resp.is_anycast, true)
assert_equal(resp.is_hosting, true)
assert_equal(resp.is_mobile, false)
assert_equal(resp.is_satellite, false)
end
end
def test_lookup_ip6
ipinfo = IPinfoCore.create(ENV.fetch('IPINFO_TOKEN', nil))
# multiple checks for cache
(0...5).each do |_|
resp = ipinfo.details(TEST_IPV6)
# Basic fields
assert_equal(resp.ip, TEST_IPV6)
assert_equal(resp.ip_address, IPAddr.new(TEST_IPV6))
# Geo object assertions
assert(resp.geo.is_a?(Hash))
refute_nil(resp.geo[:city])
refute_nil(resp.geo[:region])
refute_nil(resp.geo[:region_code])
assert_equal(resp.geo[:country_code], 'US')
assert_equal(resp.geo[:country], 'United States')
assert_equal(resp.geo[:country_name], 'United States')
assert_equal(resp.geo[:is_eu], false)
refute_nil(resp.geo[:continent])
refute_nil(resp.geo[:continent_code])
refute_nil(resp.geo[:latitude])
refute_nil(resp.geo[:longitude])
refute_nil(resp.geo[:timezone])
refute_nil(resp.geo[:postal_code])
assert_equal(resp.geo[:country_flag]['emoji'], '🇺🇸')
assert_equal(resp.geo[:country_flag]['unicode'], 'U+1F1FA U+1F1F8')
assert_equal(resp.geo[:country_flag_url], 'https://cdn.ipinfo.io/static/images/countries-flags/US.svg')
assert_equal(resp.geo[:country_currency]['code'], 'USD')
assert_equal(resp.geo[:country_currency]['symbol'], '$')
assert_equal(resp.geo[:continent]['code'], 'NA')
assert_equal(resp.geo[:continent]['name'], 'North America')
# AS object assertions
assert(resp.as.is_a?(Hash))
assert_equal(resp.as[:asn], 'AS15169')
assert(resp.as[:name].is_a?(String))
assert(resp.as[:domain].is_a?(String))
assert(resp.as[:type].is_a?(String))
# Network flags
assert_equal(resp.is_anonymous, false)
assert_equal(resp.is_anycast, true)
assert_equal(resp.is_hosting, true)
assert_equal(resp.is_mobile, false)
assert_equal(resp.is_satellite, false)
end
end
def test_bogon_ip
ipinfo = IPinfoCore.create(ENV.fetch('IPINFO_TOKEN', nil))
resp = ipinfo.details('192.168.1.1')
assert_equal(resp.bogon, true)
assert_equal(resp.ip, '192.168.1.1')
end
end