Skip to content

Commit bb23eb3

Browse files
committed
Fix localhost URL support and refector tests
* Fixes #13 (http://localhost URLs not working) * Refactors tests by breaking up large test file
1 parent 83ccefc commit bb23eb3

File tree

6 files changed

+87
-76
lines changed

6 files changed

+87
-76
lines changed

__tests__/input-type-detection.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Test dectection of different types of input
2+
const fs = require('fs')
3+
const fetch = require('node-fetch')
4+
5+
const {
6+
structuredDataTest,
7+
structuredDataTestUrl,
8+
structuredDataTestHtml
9+
} = require('../index')
10+
const presets = require('../presets')
11+
12+
const testFile = '__tests__/fixtures/example.html'
13+
const html = fs.readFileSync(testFile)
14+
15+
describe('Input type detection', () => {
16+
beforeAll(async () => {
17+
// Mock fetch request
18+
fetch.resetMocks()
19+
fetch.mockResponse(html);
20+
})
21+
22+
afterAll(() => {
23+
fetch.resetMocks()
24+
})
25+
26+
test('should auto-detect when input is a buffer', async () => {
27+
const result = await new Promise((resolve) => {
28+
fs.readFile(testFile, async (err, buffer) => {
29+
return resolve(await structuredDataTest(buffer, { presets: [ presets.Google ]}))
30+
})
31+
})
32+
expect(result.passed.length).toBeGreaterThan(10)
33+
expect(result.failed.length).toEqual(0)
34+
})
35+
36+
test('should auto-detect when input is a readable stream', async () => {
37+
const buffer = fs.createReadStream(testFile)
38+
const result = await structuredDataTest(buffer, { presets: [ presets.Google ]})
39+
expect(result.passed.length).toBeGreaterThan(10)
40+
expect(result.failed.length).toEqual(0)
41+
})
42+
43+
test('should auto-detect when input is an HTTP URL', async () => {
44+
const result = await structuredDataTest('http://example.com', { presets: [ presets.Google ]})
45+
expect(result.passed.length).toBeGreaterThan(10)
46+
expect(result.failed.length).toEqual(0)
47+
})
48+
49+
test('should auto-detect when input is an HTTPS URL', async () => {
50+
const result = await structuredDataTest('https://example.com', { presets: [ presets.Google ]})
51+
expect(result.passed.length).toBeGreaterThan(10)
52+
expect(result.failed.length).toEqual(0)
53+
})
54+
55+
test('should work when explicitly invoked with HTML', async () => {
56+
const result = await structuredDataTestHtml(html, { presets: [ presets.Google ]})
57+
expect(result.passed.length).toBeGreaterThan(10)
58+
expect(result.failed.length).toEqual(0)
59+
})
60+
61+
test('should work when explicitly invoked with a URL', async () => {
62+
const result = await structuredDataTestUrl('https://example.com', { presets: [ presets.Google ]})
63+
expect(result.passed.length).toBeGreaterThan(10)
64+
expect(result.failed.length).toEqual(0)
65+
})
66+
67+
test('should support http://localhost URLs', async () => {
68+
// Tests fix for bug where http://localhost URLs did not always work
69+
// https://github.com/glitchdigital/structured-data-testing-tool/issues/13
70+
const result = await structuredDataTest('http://localhost', { presets: [ presets.Google ] })
71+
expect(result.passed.length).toBeGreaterThan(1)
72+
expect(result.failed.length).toEqual(0)
73+
})
74+
})

__tests__/index.js renamed to __tests__/parsing-and-options.js

+9-73
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,37 @@
1-
21
const fs = require('fs')
32
const fetch = require('node-fetch')
3+
44
const {
5-
_structuredDataTest,
65
structuredDataTest,
7-
structuredDataTestUrl,
8-
structuredDataTestHtml
6+
structuredDataTestUrl
97
} = require('../index')
108
const presets = require('../presets')
119

12-
13-
// @FIXME Split out this single catch-all example file to test multiple scenarios separately
1410
const testFile = '__tests__/fixtures/example.html'
1511
const html = fs.readFileSync(testFile)
1612

13+
// Used to hold the test result that is used across these tests
1714
let structuredDataTestResult = null
1815

19-
describe('Structured Data parsing', () => {
16+
describe('Parsing and options', () => {
2017
beforeAll(async () => {
2118
// Mock fetch request
2219
fetch.resetMocks()
2320
fetch.mockResponse(html);
2421

25-
// Current test uses single example file
22+
// @TODO This test uses a single fixture for all tests
2623
// Ideally there would be multiple different fixtures that more robustly
27-
// test different scenarios, but this is is a practical approach that
28-
// improves coverage easily for now with minimal effort
24+
// test different scenarios, but this is is a practical approach that gives
25+
// good coverage with minimal effort.
2926
await structuredDataTest(html, { presets: [ presets.Google, presets.Twitter ]})
30-
.then(response => {
31-
structuredDataTestResult = response
32-
})
33-
.catch(err => {
34-
structuredDataTestResult = err.res
35-
})
27+
.then(response => { structuredDataTestResult = response })
28+
.catch(err => { structuredDataTestResult = err.res })
3629
})
3730

3831
afterAll(() => {
3932
fetch.resetMocks()
4033
})
4134

42-
test('should auto-detect and return structured data schemas found', async () => {
43-
expect(structuredDataTestResult.schemas.length).toEqual(4)
44-
expect(structuredDataTestResult.failed.length).toEqual(0)
45-
})
46-
47-
test('should auto-detect when input is HTML and validate all schemas found', async () => {
48-
expect(structuredDataTestResult.passed.length).toBeGreaterThan(10)
49-
expect(structuredDataTestResult.failed.length).toEqual(0)
50-
})
51-
52-
test('should auto-detect when input is a string', async () => {
53-
const result = await structuredDataTest(html.toString(), { presets: [ presets.Google ]})
54-
expect(result.passed.length).toBeGreaterThan(10)
55-
expect(result.failed.length).toEqual(0)
56-
})
57-
58-
test('should auto-detect when input is a buffer', async () => {
59-
const result = await new Promise((resolve) => {
60-
fs.readFile(testFile, async (err, buffer) => {
61-
return resolve(await structuredDataTest(buffer, { presets: [ presets.Google ]}))
62-
})
63-
})
64-
expect(result.passed.length).toBeGreaterThan(10)
65-
expect(result.failed.length).toEqual(0)
66-
})
67-
68-
test('should auto-detect when input is a readable stream', async () => {
69-
const buffer = fs.createReadStream(testFile)
70-
const result = await structuredDataTest(buffer, { presets: [ presets.Google ]})
71-
expect(result.passed.length).toBeGreaterThan(10)
72-
expect(result.failed.length).toEqual(0)
73-
})
74-
75-
test('should auto-detect when input is an HTTP URL', async () => {
76-
const result = await structuredDataTest('http://example.com', { presets: [ presets.Google ]})
77-
expect(result.passed.length).toBeGreaterThan(10)
78-
expect(result.failed.length).toEqual(0)
79-
})
80-
81-
test('should auto-detect when input is an HTTPS URL', async () => {
82-
const result = await structuredDataTest('https://example.com', { presets: [ presets.Google ]})
83-
expect(result.passed.length).toBeGreaterThan(10)
84-
expect(result.failed.length).toEqual(0)
85-
})
86-
87-
test('should work when explicitly invoked with HTML', async () => {
88-
const result = await structuredDataTestHtml(html, { presets: [ presets.Google ]})
89-
expect(result.passed.length).toBeGreaterThan(10)
90-
expect(result.failed.length).toEqual(0)
91-
})
92-
93-
test('should work when explicitly invoked with a URL', async () => {
94-
const result = await structuredDataTestUrl('https://example.com', { presets: [ presets.Google ]})
95-
expect(result.passed.length).toBeGreaterThan(10)
96-
expect(result.failed.length).toEqual(0)
97-
})
98-
9935
// @FIXME This test covers too much at once, should split out error handling checks
10036
// @FIXME This test also tests ever preset for coverage - that should move to the presets test file
10137
test('should validate all structured data schemas found as well as any presets specified and handle errors correctly', async () => {

__tests__/presets.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
const fs = require('fs')
3+
34
const { structuredDataTest } = require('../index')
45
const presets = require('../presets')
56

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ const structuredDataTest = async (input, options) => {
431431
if (typeof(input) === 'string') {
432432
// If is a string…
433433
// Assumed to be URL or HTML
434-
if (validator.isURL(input)) {
434+
if (validator.isURL(input, { require_tld: false })) {
435435
// @TODO Improve URL error handling
436436
const url = input
437437
const res = await fetch(url)

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "structured-data-testing-tool",
3-
"version": "3.5.1",
3+
"version": "3.6.0",
44
"description": "A library and command line tool to help test for Structured Data.",
55
"repository": "https://github.com/glitchdigital/structured-data-testing-tool",
66
"main": "index.js",

0 commit comments

Comments
 (0)