Skip to content

Commit

Permalink
feat(smartcar-error): retryAfter (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbry authored Apr 3, 2023
1 parent 72e06d7 commit 230e41e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/smartcar-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SmartcarError extends Error {
super(`${body.type}:${body.code} - ${body.description}`);
}

if (headers['retry-after']) {
this.retryAfter = headers['retry-after'];
}

this.statusCode = status;
this.requestId = body.requestId || headers['sc-request-id'];
if (typeof body.resolution === 'string') {
Expand Down
24 changes: 19 additions & 5 deletions test/end-to-end/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,33 @@ helpers.runAuthFlow = async function(
.build();

await driver.get(authUrl);

// Preamble
await driver.findElement(By.css('button#continue-button')).click();
const continueButton = await driver.wait(
until.elementLocated(
By.css('button#continue-button'),
),
);
await continueButton.click();

// Brand Selector
await driver
.findElement(By.css(`button.brand-selector-button[data-make="${brand}"]`))
.click();
const brandButton = await driver.wait(
until.elementLocated(
By.css(`button#${brand.toUpperCase()}.brand-list-item`),
),
);
brandButton.click();

// Login
const signInButton = await driver.wait(
until.elementLocated(
By.id('sign-in-button'),
),
);
email = email || `${uuid()}@email.com`;
await driver.findElement(By.css('input[id=username]')).sendKeys(email);
await driver.findElement(By.css('input[id=password')).sendKeys('password');
await driver.findElement(By.css('button[id=sign-in-button]')).click();
await signInButton.click();

// Permissions
await driver.sleep(5000);
Expand Down
13 changes: 13 additions & 0 deletions test/unit/lib/vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,16 @@ test('request - override non-sc headers', async function(t) {
t.is(response.body.distance, 10);
t.is(response.meta.requestId, 'requestId');
});

test('request - rate limit ', async function(t) {
const retryAfter = new Date().valueOf();
t.context.n = nock(
`https://api.smartcar.com/v${vehicle.version}/vehicles/${VID}`,
)
.matchHeader('Authorization', `Bearer ${TOKEN}`)
.get('/odometer')
.reply(429, {error: 'RATE_LIMIT'}, {'retry-after': retryAfter});

const error = await t.throwsAsync(vehicle.odometer());
t.is(error.retryAfter, String(retryAfter));
});

0 comments on commit 230e41e

Please sign in to comment.