···
-
(** Example: Validating JSON Feeds
-
- Validating feed structure
-
- Testing various edge cases
-
- Handling invalid feeds
-
- Best practices for feed construction *)
-
let test_valid_minimal_feed () =
-
Format.printf "=== Test: Minimal Valid Feed ===\n";
-
let feed = Jsonfeed.create
-
match Jsonfeed.validate feed with
-
| Ok () -> Format.printf "✓ Minimal feed is valid\n\n"
-
Format.printf "✗ Minimal feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_valid_complete_feed () =
-
Format.printf "=== Test: Complete Valid Feed ===\n";
-
let author = Author.create
-
~url:"https://example.com/author"
-
~avatar:"https://example.com/avatar.png"
-
let attachment = Attachment.create
-
~url:"https://example.com/file.mp3"
-
~mime_type:"audio/mpeg"
-
~duration_in_seconds:60
-
~id:"https://example.com/items/1"
-
~url:"https://example.com/items/1"
-
~content:(`Both ("<p>HTML content</p>", "Text content"))
-
~image:"https://example.com/image.jpg"
-
~banner_image:"https://example.com/banner.jpg"
-
~date_published:(Jsonfeed.Rfc3339.parse "2024-11-01T10:00:00Z" |> Option.get)
-
~date_modified:(Jsonfeed.Rfc3339.parse "2024-11-01T15:00:00Z" |> Option.get)
-
~tags:["test"; "example"]
-
~attachments:[attachment]
-
~url:"https://pubsubhubbub.appspot.com/"
-
let feed = Jsonfeed.create
-
~home_page_url:"https://example.com"
-
~feed_url:"https://example.com/feed.json"
-
~description:"A complete test feed"
-
~user_comment:"This is a test feed"
-
~icon:"https://example.com/icon.png"
-
~favicon:"https://example.com/favicon.ico"
-
match Jsonfeed.validate feed with
-
| Ok () -> Format.printf "✓ Complete feed is valid\n\n"
-
Format.printf "✗ Complete feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_feed_with_multiple_items () =
-
Format.printf "=== Test: Feed with Multiple Items ===\n";
-
let items = List.init 10 (fun i ->
-
~id:(Printf.sprintf "https://example.com/items/%d" i)
-
~content:(`Text (Printf.sprintf "Item %d content" i))
-
~title:(Printf.sprintf "Item %d" i)
-
~date_published:(Jsonfeed.Rfc3339.parse
-
(Printf.sprintf "2024-11-%02dT10:00:00Z" (i + 1)) |> Option.get)
-
let feed = Jsonfeed.create
-
~title:"Multi-item Feed"
-
match Jsonfeed.validate feed with
-
Format.printf "✓ Feed with %d items is valid\n\n" (List.length items)
-
Format.printf "✗ Multi-item feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_podcast_feed () =
-
Format.printf "=== Test: Podcast Feed ===\n";
-
let host = Author.create
-
~url:"https://podcast.example.com/host"
-
let episode1 = Attachment.create
-
~url:"https://podcast.example.com/ep1.mp3"
-
~mime_type:"audio/mpeg"
-
~size_in_bytes:20_971_520L (* 20 MB *)
-
~duration_in_seconds:1800 (* 30 minutes *)
-
(* Alternate format of the same episode *)
-
let episode1_aac = Attachment.create
-
~url:"https://podcast.example.com/ep1.aac"
-
~size_in_bytes:16_777_216L
-
~duration_in_seconds:1800
-
~id:"https://podcast.example.com/episodes/1"
-
~url:"https://podcast.example.com/episodes/1"
-
~title:"Episode 1: Introduction"
-
~content:(`Html "<p>Welcome to the first episode!</p>")
-
~date_published:(Jsonfeed.Rfc3339.parse "2024-11-01T12:00:00Z" |> Option.get)
-
~attachments:[episode1; episode1_aac]
-
~image:"https://podcast.example.com/ep1-cover.jpg"
-
let feed = Jsonfeed.create
-
~title:"Example Podcast"
-
~home_page_url:"https://podcast.example.com"
-
~feed_url:"https://podcast.example.com/feed.json"
-
match Jsonfeed.validate feed with
-
| Ok () -> Format.printf "✓ Podcast feed is valid\n\n"
-
Format.printf "✗ Podcast feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_microblog_feed () =
-
Format.printf "=== Test: Microblog Feed (no titles) ===\n";
-
let author = Author.create
-
~url:"https://micro.example.com"
-
~id:"https://micro.example.com/1"
-
~content:(`Text "Just posted a new photo!")
-
~date_published:(Jsonfeed.Rfc3339.parse "2024-11-01T08:00:00Z" |> Option.get)
-
~id:"https://micro.example.com/2"
-
~content:(`Text "Having a great day! ☀️")
-
~date_published:(Jsonfeed.Rfc3339.parse "2024-11-01T12:30:00Z" |> Option.get)
-
~id:"https://micro.example.com/3"
-
~content:(`Html "<p>Check out this <a href=\"#\">link</a></p>")
-
~date_published:(Jsonfeed.Rfc3339.parse "2024-11-01T16:45:00Z" |> Option.get)
-
let feed = Jsonfeed.create
-
~home_page_url:"https://micro.example.com"
-
match Jsonfeed.validate feed with
-
Format.printf "✓ Microblog feed with %d items is valid\n\n"
-
Format.printf "✗ Microblog feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_expired_feed () =
-
Format.printf "=== Test: Expired Feed ===\n";
-
let feed = Jsonfeed.create
-
~home_page_url:"https://archive.example.com"
-
~description:"This blog is no longer updated"
-
match Jsonfeed.validate feed with
-
| Ok () -> Format.printf "✓ Expired feed is valid\n\n"
-
Format.printf "✗ Expired feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_paginated_feed () =
-
Format.printf "=== Test: Paginated Feed ===\n";
-
let items = List.init 25 (fun i ->
-
~id:(Printf.sprintf "https://example.com/items/%d" i)
-
~content:(`Text (Printf.sprintf "Item %d" i))
-
let feed = Jsonfeed.create
-
~home_page_url:"https://example.com"
-
~feed_url:"https://example.com/feed.json?page=1"
-
~next_url:"https://example.com/feed.json?page=2"
-
match Jsonfeed.validate feed with
-
Format.printf "✓ Paginated feed is valid (page 1 with next_url)\n\n"
-
Format.printf "✗ Paginated feed validation failed:\n";
-
List.iter (Format.printf " - %s\n") errors;
-
let test_invalid_feed_from_json () =
-
Format.printf "=== Test: Parsing Invalid JSON ===\n";
-
(* Missing required version field *)
-
let invalid_json1 = {|{
-
(match Jsonfeed.of_string invalid_json1 with
-
| Ok _ -> Format.printf "✗ Should have failed (missing version)\n"
-
Format.printf "✓ Correctly rejected invalid feed: %s\n" (Jsont.Error.to_string err));
-
(* Missing required title field *)
-
let invalid_json2 = {|{
-
"version": "https://jsonfeed.org/version/1.1",
-
(match Jsonfeed.of_string invalid_json2 with
-
| Ok _ -> Format.printf "✗ Should have failed (missing title)\n"
-
Format.printf "✓ Correctly rejected invalid feed: %s\n" (Jsont.Error.to_string err));
-
let invalid_json3 = {|{
-
"version": "https://jsonfeed.org/version/1.1",
-
"content_text": "Hello"
-
(match Jsonfeed.of_string invalid_json3 with
-
| Ok _ -> Format.printf "✗ Should have failed (item without id)\n"
-
Format.printf "✓ Correctly rejected invalid feed: %s\n" (Jsont.Error.to_string err));
-
Format.printf "\n=== JSON Feed Validation Tests ===\n\n";
-
test_valid_minimal_feed ();
-
test_valid_complete_feed ();
-
test_feed_with_multiple_items ();
-
test_microblog_feed ();
-
test_paginated_feed ();
-
test_invalid_feed_from_json ();
-
Format.printf "=== All Tests Complete ===\n"