|
| 1 | +/* |
| 2 | + * Copyright 2019 Ipregistry (https://ipregistry.co). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// The shared scenario behind the runtime smoke tests. Stubs globalThis.fetch |
| 18 | +// (restoring it afterwards) and drives lookups, caching, retries, and error |
| 19 | +// mapping through the built ESM bundle using only web-standard APIs. |
| 20 | + |
| 21 | +import { |
| 22 | + ApiError, |
| 23 | + InMemoryCache, |
| 24 | + IpregistryClient, |
| 25 | + IpregistryConfigBuilder, |
| 26 | +} from '../dist/index.mjs' |
| 27 | + |
| 28 | +function assertEqual(actual, expected, label) { |
| 29 | + if (actual !== expected) { |
| 30 | + throw new Error(`${label}: expected ${expected}, got ${actual}`) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function jsonResponse(body, status = 200, headers = {}) { |
| 35 | + return new Response(JSON.stringify(body), { |
| 36 | + status, |
| 37 | + headers: { 'content-type': 'application/json', ...headers }, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +export async function runSmokeTest() { |
| 42 | + const originalFetch = globalThis.fetch |
| 43 | + let requests = 0 |
| 44 | + |
| 45 | + globalThis.fetch = async url => { |
| 46 | + requests++ |
| 47 | + const path = new URL(url).pathname |
| 48 | + |
| 49 | + if (path === '/1.2.3.4') { |
| 50 | + return jsonResponse( |
| 51 | + { ip: '1.2.3.4', location: { country: { code: 'AU' } } }, |
| 52 | + 200, |
| 53 | + { 'ipregistry-credits-consumed': '1' }, |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + if (path === '/5.6.7.8') { |
| 58 | + // Fail once with a 503 to exercise the retry path. |
| 59 | + return requests % 2 === 0 |
| 60 | + ? jsonResponse({ ip: '5.6.7.8' }) |
| 61 | + : jsonResponse( |
| 62 | + { code: 'INTERNAL', message: 'oops', resolution: '-' }, |
| 63 | + 503, |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + return jsonResponse( |
| 68 | + { |
| 69 | + code: 'INVALID_IP_ADDRESS', |
| 70 | + message: 'Invalid IP', |
| 71 | + resolution: '-', |
| 72 | + }, |
| 73 | + 400, |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const client = new IpregistryClient( |
| 79 | + new IpregistryConfigBuilder('tryout').withRetryInterval(1).build(), |
| 80 | + new InMemoryCache(), |
| 81 | + ) |
| 82 | + |
| 83 | + // Plain lookup, response headers, and caching. |
| 84 | + const first = await client.lookupIp('1.2.3.4') |
| 85 | + assertEqual(first.data.location.country.code, 'AU', 'country code') |
| 86 | + assertEqual(first.credits.consumed, 1, 'credits consumed') |
| 87 | + |
| 88 | + const requestsBeforeCachedCall = requests |
| 89 | + const second = await client.lookupIp('1.2.3.4') |
| 90 | + assertEqual(second.data.ip, '1.2.3.4', 'cached ip') |
| 91 | + assertEqual(requests, requestsBeforeCachedCall, 'cache hit') |
| 92 | + |
| 93 | + // Retry on server error. |
| 94 | + const retried = await client.lookupIp('5.6.7.8') |
| 95 | + assertEqual(retried.data.ip, '5.6.7.8', 'retried ip') |
| 96 | + |
| 97 | + // API error mapping. |
| 98 | + try { |
| 99 | + await client.lookupIp('not-an-ip') |
| 100 | + throw new Error('expected lookupIp to throw an ApiError') |
| 101 | + } catch (error) { |
| 102 | + if (!(error instanceof ApiError)) { |
| 103 | + throw error |
| 104 | + } |
| 105 | + assertEqual(error.code, 'INVALID_IP_ADDRESS', 'error code') |
| 106 | + } |
| 107 | + } finally { |
| 108 | + globalThis.fetch = originalFetch |
| 109 | + } |
| 110 | +} |
0 commit comments