diff --git a/__tests__/fixtures/example.json b/__tests__/fixtures/example.json new file mode 100644 index 0000000..7b0d635 --- /dev/null +++ b/__tests__/fixtures/example.json @@ -0,0 +1,27 @@ +{ + "@context": "https://schema.org/", + "@type": "Product", + "@ids": "https://www.blah.org/#blahorg", + "urls": "https://www.blah.org/en-US/developer/", + "logod": "https://www.example.com/example-logo.jpg", + "images": [ + "https://example.com/photos/1x1/photo.jpg", + "https://example.com/photos/4x3/photo.jpg", + "https://example.com/photos/16x9/photo.jpg" + ], + "named": "Blah Org", + "alternateName": "Blah", + "brand": { + "@type": "Brand", + "@id": "https://www.blah.org/#brand", + "name": "ACME Inc" + }, + "sameAs": ["https://en.wikipedia.org/wiki/blah"], + "offers": { + "@type": "Offer", + "url": "https://www.blah.org/developer/", + "priceCurrency": "USD", + "price": "0", + "availability": "https://schema.org/InStock" + } +} \ No newline at end of file diff --git a/__tests__/input-type-detection.js b/__tests__/input-type-detection.js index e8249eb..afc2cdc 100644 --- a/__tests__/input-type-detection.js +++ b/__tests__/input-type-detection.js @@ -9,8 +9,9 @@ const { } = require('../index') const presets = require('../presets') -const testFile = '__tests__/fixtures/example.html' -const html = fs.readFileSync(testFile) +const testHTML = '__tests__/fixtures/example.html' +const html = fs.readFileSync(testHTML) +const testJSON = fs.readFileSync('__tests__/fixtures/example.json') describe('Input type detection', () => { beforeAll(async () => { @@ -25,7 +26,7 @@ describe('Input type detection', () => { test('should auto-detect when input is a buffer', async () => { const result = await new Promise((resolve) => { - fs.readFile(testFile, async (err, buffer) => { + fs.readFile(testHTML, async (err, buffer) => { return resolve(await structuredDataTest(buffer, { presets: [ presets.Google ]})) }) }) @@ -34,12 +35,19 @@ describe('Input type detection', () => { }) test('should auto-detect when input is a readable stream', async () => { - const buffer = fs.createReadStream(testFile) + const buffer = fs.createReadStream(testHTML) const result = await structuredDataTest(buffer, { presets: [ presets.Google ]}) expect(result.passed.length).toBeGreaterThan(10) expect(result.failed.length).toEqual(0) }) + test('should auto-detect when input is JSON string', async () => { + const result = await structuredDataTest(testJSON) + expect(result.passed.length).toEqual(1) + expect(result.optional.length).toEqual(19) + expect(result.failed.length).toEqual(0) + }) + test('should auto-detect when input is an HTTP URL', async () => { const result = await structuredDataTest('http://example.com', { presets: [ presets.Google ]}) expect(result.passed.length).toBeGreaterThan(10) diff --git a/index.js b/index.js index d28ff81..d40d95f 100644 --- a/index.js +++ b/index.js @@ -418,7 +418,20 @@ const _findSchemas = (structuredData) => { const structuredDataTestUrl = async (url, options) => { const res = await fetch(url) const html = await res.text() - return structuredDataTestHtml(html, { url, res, ...options }) + return structuredDataTestString(html, { url, res, ...options }) +} + +const structuredDataTestString = async (input, options) => { + // Try to parse the string input as a JSON object. + // + // If it is a JSON object, then wrap it in ` : input } catch (e) { } + return structuredDataTestHtml(html, options) } const structuredDataTestHtml = async (html, options) => { @@ -444,18 +457,18 @@ const structuredDataTest = async (input, options) => { return structuredDataTestUrl(url, options) } else { const html = input - return structuredDataTestHtml(html, options) + return structuredDataTestString(html, options) } } else if (Buffer.isBuffer(input)) { // If is a buffer… // Convert buffer to string const html = input.toString('utf8') - return structuredDataTestHtml(html, options) + return structuredDataTestString(html, options) } else if (isStream.readable(input)) { // If is a readable stream… // Convert readable stream to string const html = await getStream(input) - return structuredDataTestHtml(html, options) + return structuredDataTestString(html, options) } else { // Else ?? const structuredData = input @@ -557,5 +570,6 @@ module.exports = { _structuredDataTest, structuredDataTest, structuredDataTestUrl, + structuredDataTestString, structuredDataTestHtml } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 27fa0a7..205bfc0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "structured-data-testing-tool", - "version": "4.0.0", + "version": "4.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 16d3032..90a8fe3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "structured-data-testing-tool", - "version": "4.0.1", + "version": "4.1.0", "description": "A library and command line tool to help test for Structured Data.", "repository": "https://github.com/glitchdigital/structured-data-testing-tool", "main": "index.js",