Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid feed format handling #54

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ denon benchmark

Using Deno
```shell
deno run --allow-read ./benchmark.ts
deno bench --allow-read bench.ts
```

### Memory footprint test
Expand Down
25 changes: 25 additions & 0 deletions bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { parseFeed } from "./mod.ts";

Deno.bench('Parse RSS1', async (b) => {
let source = await Deno.readTextFile(`./samples/rss1.xml`);
b.start();
await parseFeed(source);
b.end();
source = '';
});

Deno.bench('Parse RSS2', async (b) => {
let source = await Deno.readTextFile(`./samples/rss2.xml`);
b.start();
await parseFeed(source);
b.end();
source = '';
});

Deno.bench('Parse ATOM', async (b) => {
let source = await Deno.readTextFile(`./samples/atom.xml`);
b.start();
await parseFeed(source);
b.end();
source = '';
});
29 changes: 0 additions & 29 deletions benchmark.ts

This file was deleted.

2 changes: 1 addition & 1 deletion scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "deno run dev.ts atom",
"test": "deno test",
"benchmark": "deno run benchmark.ts",
"benchmark": "deno bench bench.ts",
"memory": "deno run dev_memory_usage.ts"
}
}
14 changes: 14 additions & 0 deletions src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ const parse = (input: string) =>
parser.onopentag = onOpenTag;
};

parser.onend = () => {
if (!feedType) {
Object.assign(parser, {
onopentag: undefined,
onclosetag: undefined,
ontext: undefined,
oncdata: undefined,
onend: undefined
});

reject(new Error(`Invalid or unsupported feed format`));
}
}

parser
.write(input)
.close()
Expand Down
44 changes: 43 additions & 1 deletion src/deserializer_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertNotEquals } from "../test_deps.ts";
import { assertEquals, assertNotEquals, assertRejects } from "../test_deps.ts";
import { parseFeed } from "./deserializer.ts";
import { Feed, MediaRss } from "../mod.ts";
import { FeedType } from "../mod.ts";
Expand Down Expand Up @@ -726,3 +726,45 @@ const rss2DublinCoreTestSample = await Deno.readTextFile("./samples/rss2_dublin-
});
});
});

Deno.test('Should throw error on invalid feed format', async () => {
await assertRejects(() => parseFeed('Invalid feed string'), Error, "Invalid or unsupported feed format");
});

Deno.test('Should throw error on unsupported feed format', async () => {
const futureRSSFormat = `
<?xml version="1.0" encoding="UTF-8"?>
<xrss version="X.0" xmlns:media="http://schema.loremipsumuru.com/xrss/">
<channel>
<title>Future RSS Feed</title>
<link>https://example.com</link>
<description>An RSS feed from the future</description>
<language>en-us</language>
<lastBuildDate>[Last updated timestamp]</lastBuildDate>
<generator>FutureRSSGenerator v1.0</generator>
<pubDate>[Publication timestamp]</pubDate>
<copyright>Copyright © [Year] by [Your Organization]</copyright>
<managingEditor>[Editor's email]</managingEditor>
<webMaster>[Webmaster's email]</webMaster>
<image>
<url>https://example.com/logo.png</url>
<title>Future RSS Feed</title>
<link>https://example.com</link>
</image>
<item>
<title>[Title of the article]</title>
<link>[Link to the article]</link>
<description>[Description of the article]</description>
<author>[Author's name]</author>
<category>[Category of the article]</category>
<pubDate>[Publication timestamp]</pubDate>
<media:content url="[URL to media file]" type="[Media type]" vr="true" />
<media:vrFormat>360-degree video</media:vrFormat>
<media:vrPlatform>Oculus Rift</media:vrPlatform>
</item>
<!-- Additional items go here -->
</channel>
</xrss>
`;
await assertRejects(() => parseFeed(futureRSSFormat), Error, "Type xrss is not supported");
});
8 changes: 2 additions & 6 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ export {
assert,
assertEquals,
assertNotEquals,
assertThrowsAsync,
} from "https://deno.land/[email protected]/testing/asserts.ts";
export {
bench,
runBenchmarks,
} from "https://deno.land/[email protected]/testing/bench.ts";
assertRejects
} from "https://deno.land/[email protected]/assert/mod.ts";
Loading