| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package json // import "miniflux.app/v2/internal/reader/json"
- import (
- "bytes"
- "crypto/sha256"
- "encoding/hex"
- "testing"
- "time"
- )
- func TestParseJsonFeedVersion1(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "icon": "https://micro.blog/jsonfeed/avatar.jpg",
- "favicon": "https://micro.blog/jsonfeed/favicon.png",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2",
- "content_text": "This is a second item.",
- "url": "https://example.org/second-item"
- },
- {
- "id": "1",
- "content_html": "<p>Hello, world!</p>",
- "url": "https://example.org/initial-post"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Title != "My Example Feed" {
- t.Errorf("Incorrect title, got: %s", feed.Title)
- }
- if feed.Description != "" {
- t.Errorf("Incorrect description, got: %s", feed.Description)
- }
- if feed.FeedURL != "https://example.org/feed.json" {
- t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
- }
- if feed.SiteURL != "https://example.org/" {
- t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
- }
- if feed.IconURL != "https://micro.blog/jsonfeed/favicon.png" {
- t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
- }
- if len(feed.Entries) != 2 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- if feed.Entries[0].URL != "https://example.org/second-item" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
- }
- if feed.Entries[0].Title != "This is a second item." {
- t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
- }
- if feed.Entries[0].Content != "This is a second item." {
- t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
- }
- if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
- }
- if feed.Entries[1].URL != "https://example.org/initial-post" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
- }
- if feed.Entries[1].Title != "Hello, world!" {
- t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
- }
- if feed.Entries[1].Content != "<p>Hello, world!</p>" {
- t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
- }
- }
- func TestParseFeedWithDescription(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "description": "This is a sample feed description.",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Description != "This is a sample feed description." {
- t.Errorf("Incorrect description, got: %s", feed.Description)
- }
- }
- func TestParsePodcast(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "user_comment": "This is a podcast feed. You can add this feed to your podcast client using the following URL: http://therecord.co/feed.json",
- "title": "The Record",
- "home_page_url": "http://therecord.co/",
- "feed_url": "http://therecord.co/feed.json",
- "items": [
- {
- "id": "http://therecord.co/chris-parrish",
- "title": "Special #1 - Chris Parrish",
- "url": "http://therecord.co/chris-parrish",
- "content_text": "Chris has worked at Adobe and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped Napkin, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on Bainbridge Island, a quick ferry ride from Seattle.",
- "content_html": "Chris has worked at <a href=\"http://adobe.com/\">Adobe</a> and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped <a href=\"http://aged-and-distilled.com/napkin/\">Napkin</a>, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on <a href=\"http://www.ci.bainbridge-isl.wa.us/\">Bainbridge Island</a>, a quick ferry ride from Seattle.",
- "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
- "date_published": "2014-05-09T14:04:00-07:00",
- "attachments": [
- {
- "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
- "mime_type": "audio/x-m4a",
- "size_in_bytes": 89970236,
- "duration_in_seconds": 6629
- }
- ]
- }
- ]
- }`
- feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Title != "The Record" {
- t.Errorf("Incorrect title, got: %s", feed.Title)
- }
- if feed.FeedURL != "http://therecord.co/feed.json" {
- t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
- }
- if feed.SiteURL != "http://therecord.co/" {
- t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
- }
- if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
- t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
- }
- if feed.Entries[0].Content != `Chris has worked at <a href="http://adobe.com/">Adobe</a> and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped <a href="http://aged-and-distilled.com/napkin/">Napkin</a>, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on <a href="http://www.ci.bainbridge-isl.wa.us/">Bainbridge Island</a>, a quick ferry ride from Seattle.` {
- t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
- }
- location, _ := time.LoadLocation("America/Vancouver")
- if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
- t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
- }
- if len(feed.Entries[0].Enclosures) != 1 {
- t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
- }
- if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
- t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
- }
- if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
- t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
- }
- if feed.Entries[0].Enclosures[0].Size != 89970236 {
- t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
- }
- }
- func TestParseFeedWithFeedURLWithTrailingSpace(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json ",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.FeedURL != "https://example.org/feed.json" {
- t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
- }
- }
- func TestParseFeedWithRelativeFeedURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "/feed.json",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.FeedURL != "https://example.org/feed.json" {
- t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
- }
- }
- func TestParseFeedSiteURLWithTrailingSpace(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/ ",
- "feed_url": "https://example.org/feed.json",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.SiteURL != "https://example.org/" {
- t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
- }
- }
- func TestParseFeedWithRelativeSiteURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "/home ",
- "feed_url": "https://example.org/feed.json",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.SiteURL != "https://example.org/home" {
- t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
- }
- }
- func TestParseFeedWithoutTitle(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Title != "https://example.org/" {
- t.Errorf("Incorrect title, got: %s", feed.Title)
- }
- }
- func TestParseFeedWithoutHomePage(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "feed_url": "https://example.org/feed.json",
- "title": "Some test",
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.SiteURL != "https://example.org/feed.json" {
- t.Errorf("Incorrect title, got: %s", feed.Title)
- }
- }
- func TestParseFeedWithoutFeedURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Some test",
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.SiteURL != "https://example.org/feed.json" {
- t.Errorf("Incorrect title, got: %s", feed.Title)
- }
- }
- func TestParseItemWithoutAttachmentURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "user_comment": "This is a podcast feed. You can add this feed to your podcast client using the following URL: http://therecord.co/feed.json",
- "title": "The Record",
- "home_page_url": "http://therecord.co/",
- "feed_url": "http://therecord.co/feed.json",
- "items": [
- {
- "id": "http://therecord.co/chris-parrish",
- "title": "Special #1 - Chris Parrish",
- "url": "http://therecord.co/chris-parrish",
- "content_text": "Chris has worked at Adobe and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped Napkin, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on Bainbridge Island, a quick ferry ride from Seattle.",
- "date_published": "2014-05-09T14:04:00-07:00",
- "attachments": [
- {
- "url": "",
- "mime_type": "audio/x-m4a",
- "size_in_bytes": 0
- }
- ]
- }
- ]
- }`
- feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if len(feed.Entries[0].Enclosures) != 0 {
- t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
- }
- }
- func TestParseItemWithRelativeURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2347259",
- "url": "something.html",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Entries[0].URL != "https://example.org/something.html" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
- }
- }
- func TestParseItemWithExternalURLAndNoURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "1234259",
- "external_url": "some_page.html"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].URL != "https://example.org/some_page.html" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
- }
- }
- func TestParseItemWithExternalURLAndURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "1234259",
- "url": "https://example.org/article",
- "external_url": "https://example.org/another-article"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].URL != "https://example.org/article" {
- t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
- }
- }
- func TestParseItemWithLegacyAuthorField(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
- "title": "Brent Simmons’s Microblog",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "author": {
- "name": "Brent Simmons",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- },
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Author != "Brent Simmons" {
- t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
- }
- }
- func TestParseItemWithMultipleAuthorFields(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
- "title": "Brent Simmons’s Microblog",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "author": {
- "name": "Deprecated Author Field",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- },
- "authors": [
- {
- "name": "Brent Simmons",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- }
- ],
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Author != "Brent Simmons, Deprecated Author Field" {
- t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
- }
- }
- func TestParseItemWithMultipleDuplicateAuthors(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00",
- "authors": [
- {
- "name": "Author B",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- },
- {
- "name": "Author A",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- },
- {
- "name": "Author B",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- }
- ]
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Author != "Author A, Author B" {
- t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
- }
- }
- func TestParseItemWithAuthorsObject(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "1",
- "title": "Example Item",
- "url": "https://example.org/item",
- "date_published": "2020-01-02T03:04:05Z",
- "authors": {
- "name": "Example Author"
- }
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Author != "Example Author" {
- t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
- }
- }
- func TestParseItemWithInvalidDate(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "Tomorrow"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- duration := time.Since(feed.Entries[0].Date)
- if duration.Seconds() > 1 {
- t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
- }
- }
- func TestParseItemWithMissingTitleUsesSummaryFallback(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "summary": "Summary title",
- "content_text": "Content text title",
- "content_html": "<p>HTML title</p>"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Title != "Summary title" {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemWithMissingTitleUsesContentTextFallback(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "summary": " ",
- "content_text": "Content text title",
- "content_html": "<p>HTML title</p>"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Title != "Content text title" {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemWithMissingTitleUsesHTMLFallback(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "summary": "",
- "content_text": "",
- "content_html": "<p>HTML title</p>"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Title != "HTML title" {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemWithMissingTitleUsesURLFallback(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "url": "https://example.org/item"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Title != "https://example.org/item" {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemWithTooLongUnicodeTitle(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴♂️"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if len(feed.Entries[0].Title) != 110 {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- if len([]rune(feed.Entries[0].Title)) != 93 {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemTitleWithXMLTags(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "title": "</example>"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Title != "</example>" {
- t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
- }
- }
- func TestParseItemHashPrefersIDOverURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "id-value",
- "url": "https://example.org/article"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- expected := sha256.Sum256([]byte("id-value"))
- if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- }
- func TestParseItemHashUsesURLWhenNoID(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "url": "https://example.org/article"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- expected := sha256.Sum256([]byte("https://example.org/article"))
- if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- }
- func TestParseItemHashUsesExternalURLFallback(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "external_url": "https://example.org/external"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- expected := sha256.Sum256([]byte("https://example.org/external"))
- if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- }
- func TestParseItemHashFallsBackToContent(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "content_text": "Text",
- "content_html": "<p>HTML</p>",
- "summary": "Summary"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- expected := sha256.Sum256([]byte("Text<p>HTML</p>Summary"))
- if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
- t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
- }
- }
- func TestParseItemTags(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
- "title": "Brent Simmons’s Microblog",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "author": {
- "name": "Brent Simmons",
- "url": "http://example.org/",
- "avatar": "https://example.org/avatar.png"
- },
- "items": [
- {
- "id": "2347259",
- "url": "https://example.org/2347259",
- "content_text": "Cats are neat. \n\nhttps://example.org/cats",
- "date_published": "2016-02-09T14:22:00-07:00",
- "tags": [
- " tag 1",
- " ",
- "tag 2",
- "tag 2",
- "aaa"
- ]
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
- }
- if len(feed.Entries[0].Tags) != 3 {
- t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
- }
- expected := []string{"aaa", "tag 1", "tag 2"}
- for i, tag := range feed.Entries[0].Tags {
- if tag != expected[i] {
- t.Errorf("Incorrect entry tag, got %q instead of %q", tag, expected[i])
- }
- }
- }
- func TestParseFeedFavicon(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "favicon": "https://example.org/jsonfeed/favicon.png",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2",
- "content_text": "This is a second item.",
- "url": "https://example.org/second-item"
- },
- {
- "id": "1",
- "content_html": "<p>Hello, world!</p>",
- "url": "https://example.org/initial-post"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
- t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
- }
- }
- func TestParseFeedIcon(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "icon": "https://example.org/jsonfeed/icon.png",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2",
- "content_text": "This is a second item.",
- "url": "https://example.org/second-item"
- },
- {
- "id": "1",
- "content_html": "<p>Hello, world!</p>",
- "url": "https://example.org/initial-post"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.IconURL != "https://example.org/jsonfeed/icon.png" {
- t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
- }
- }
- func TestParseFeedWithRelativeAttachmentURL(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1",
- "title": "My Example Feed",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": [
- {
- "id": "2",
- "content_text": "This is a second item.",
- "url": "https://example.org/second-item",
- "attachments": [
- {
- "url": " /attachment.mp3 ",
- "mime_type": "audio/mpeg",
- "size_in_bytes": 123456
- }
- ]
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries[0].Enclosures) != 1 {
- t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
- }
- if feed.Entries[0].Enclosures[0].URL != "https://example.org/attachment.mp3" {
- t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
- }
- }
- func TestParseInvalidJSON(t *testing.T) {
- data := `garbage`
- _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err == nil {
- t.Error("Parse should returns an error")
- }
- }
- func TestParseNullJSONFeed(t *testing.T) {
- data := `null`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatalf("Unexpected error when parsing null feed: %v", err)
- }
- if feed == nil {
- t.Fatalf("Feed should not be nil")
- }
- }
- func TestParseFeedWithLanguage(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "language": "en-US",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Language != "en-us" {
- t.Errorf("Incorrect language, got: %q", feed.Language)
- }
- }
- func TestParseFeedWithoutLanguage(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "items": []
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if feed.Language != "" {
- t.Errorf("Expected empty language, got: %q", feed.Language)
- }
- }
- func TestParseItemWithLanguage(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "language": "en-US",
- "items": [
- {
- "id": "1",
- "url": "https://example.org/item",
- "content_text": "Bonjour",
- "language": "fr-FR"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Language != "fr-fr" {
- t.Errorf("Incorrect entry language, got: %q", feed.Entries[0].Language)
- }
- }
- func TestParseItemWithoutLanguage(t *testing.T) {
- data := `{
- "version": "https://jsonfeed.org/version/1.1",
- "title": "Example",
- "home_page_url": "https://example.org/",
- "feed_url": "https://example.org/feed.json",
- "language": "en-US",
- "items": [
- {
- "id": "1",
- "url": "https://example.org/item",
- "content_text": "Hello"
- }
- ]
- }`
- feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
- if err != nil {
- t.Fatal(err)
- }
- if len(feed.Entries) != 1 {
- t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
- }
- if feed.Entries[0].Language != "" {
- t.Errorf("Expected empty entry language, got: %q", feed.Entries[0].Language)
- }
- }
|