|
4 | 4 | reverseDnsLookup, |
5 | 5 | parseWhoisResponse, |
6 | 6 | detectRir, |
| 7 | + extractIpFromValue, |
7 | 8 | } from '../src/ipinfo/index.js'; |
8 | 9 |
|
9 | 10 | describe('IP Info Module', () => { |
@@ -164,6 +165,26 @@ netname: RIPE-NCC |
164 | 165 | }); |
165 | 166 | }); |
166 | 167 |
|
| 168 | + describe('extractIpFromValue', () => { |
| 169 | + it('should extract IP from CIDR notation', () => { |
| 170 | + expect(extractIpFromValue('192.168.1.0/24')).toBe('192.168.1.0'); |
| 171 | + expect(extractIpFromValue('10.0.0.0/8')).toBe('10.0.0.0'); |
| 172 | + expect(extractIpFromValue('2001:db8::/32')).toBe('2001:db8::'); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should return IP as-is when no CIDR', () => { |
| 176 | + expect(extractIpFromValue('192.168.1.1')).toBe('192.168.1.1'); |
| 177 | + expect(extractIpFromValue('10.0.0.1')).toBe('10.0.0.1'); |
| 178 | + expect(extractIpFromValue('2001:db8::1')).toBe('2001:db8::1'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle edge cases', () => { |
| 182 | + expect(extractIpFromValue('')).toBe(''); |
| 183 | + expect(extractIpFromValue('/')).toBe(''); |
| 184 | + expect(extractIpFromValue('invalid')).toBe('invalid'); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
167 | 188 | describe('getIPInfo', () => { |
168 | 189 | it('should return error for invalid IP address', async () => { |
169 | 190 | const result = await getIPInfo('not-an-ip'); |
@@ -195,6 +216,20 @@ netname: RIPE-NCC |
195 | 216 | expect(result.ip).toBe('::1'); |
196 | 217 | expect(result.error).toBeUndefined(); |
197 | 218 | }); |
| 219 | + |
| 220 | + it('should accept CIDR notation and extract base IP', async () => { |
| 221 | + const result = await getIPInfo('192.168.1.0/24'); |
| 222 | + |
| 223 | + expect(result.ip).toBe('192.168.1.0/24'); |
| 224 | + expect(result.error).toBeUndefined(); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should return error for invalid CIDR notation', async () => { |
| 228 | + const result = await getIPInfo('not-an-ip/24'); |
| 229 | + |
| 230 | + expect(result.ip).toBe('not-an-ip/24'); |
| 231 | + expect(result.error).toBe('Invalid IP address'); |
| 232 | + }); |
198 | 233 | }); |
199 | 234 |
|
200 | 235 | describe('reverseDnsLookup', () => { |
|
0 commit comments