Skip to content

Commit dc40c32

Browse files
committed
Fix "no uri supplied in argument" error
* Type check was erroneously not finding uri * Add tests for both with and without headers * Add tests for await pattern Fixes #121
1 parent 64bd6b1 commit dc40c32

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ exports = module.exports = function ( urlOrOpts ) {
3030
url = urlOrOpts.uri;
3131
}
3232
opts = urlOrOpts;
33-
} else if ( typeof urlOrOpts === String ) {
33+
} else if ( typeof urlOrOpts === 'string' ) {
3434
url = urlOrOpts;
3535
}
3636
if ( !url ) {
37-
reject( 'No uri supplied in argument' );
37+
reject( new Error( 'No uri supplied in argument' ) );
3838
} else {
3939
resolve(
4040
// eslint-disable-next-line n/no-unsupported-features/node-builtins

package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-metadata",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Scrapes metadata of several different standards",
55
"main": "index.js",
66
"dependencies": {

test/scraping.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe( 'scraping', function () {
2727
}
2828

2929
describe( 'parseAll function', () => {
30-
it( 'should resolve promise from woorank', () => {
30+
it( 'should resolve promise from woorank with headers', () => {
3131
const url = 'https://www.woorank.com/en/blog/dublin-core-metadata-for-seo-and-usability';
3232
return meta( { uri: url, headers: { 'User-Agent': userAgent, Accept: acceptHeader } } )
3333
.then( ( result ) => {
@@ -39,9 +39,9 @@ describe( 'scraping', function () {
3939
} );
4040
} );
4141

42-
it( 'should resolve promise from blog.schema.org', () => {
42+
it( 'should resolve promise from blog.schema.org without headers', () => {
4343
const url = 'http://blog.schema.org';
44-
return meta( { uri: url, headers: { 'User-Agent': userAgent, Accept: acceptHeader } } )
44+
return meta( url )
4545
.then( ( result ) => {
4646
assert.ok( result, 'Expected result to be truthy' );
4747
} )
@@ -50,6 +50,39 @@ describe( 'scraping', function () {
5050
throw e;
5151
} );
5252
} );
53+
54+
it( 'should throw error if no uri supplied', () => meta()
55+
.then( () => {
56+
assert.fail( 'Should have rejected the promise' );
57+
} )
58+
.catch( ( e ) => {
59+
assert.ok( e instanceof Error, 'Error should be an Error object' );
60+
assert.strictEqual( e.message, 'No uri supplied in argument', 'Error message should match expected message' );
61+
} )
62+
);
63+
64+
it( 'should support await implementation with headers', async () => {
65+
const url = 'http://blog.schema.org';
66+
const result = await meta( { uri: url, headers: { 'User-Agent': userAgent, Accept: acceptHeader } } );
67+
assert.ok( result, 'Expected result to be truthy' );
68+
} );
69+
70+
it( 'should support await implementation without headers', async () => {
71+
const url = 'http://blog.schema.org';
72+
const result = await meta( url );
73+
assert.ok( result, 'Expected result to be truthy' );
74+
} );
75+
76+
it( 'should throw error if no uri is supplied with async/await', async () => {
77+
try {
78+
await meta();
79+
assert.fail( 'Should have thrown an error' );
80+
} catch ( e ) {
81+
assert.ok( e instanceof Error, 'Error should be an Error object' );
82+
assert.strictEqual( e.message, 'No uri supplied in argument', 'Error message should match expected message' );
83+
}
84+
} );
85+
5386
} );
5487

5588
describe( 'parseBEPress function', () => {

0 commit comments

Comments
 (0)