github.com/kubri/kubri@v0.5.1-0.20240317001612-bda2aaef967e/integrations/sparkle/rss_test.go (about)

     1  package sparkle_test
     2  
     3  import (
     4  	"encoding/xml"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  
     9  	"github.com/kubri/kubri/integrations/sparkle"
    10  )
    11  
    12  func TestRSSMarshalUnmarshal(t *testing.T) {
    13  	in := &sparkle.RSS{
    14  		Channels: []*sparkle.Channel{{
    15  			Title:       "Title",
    16  			Description: "Description",
    17  			Link:        "https://example.com/sparkle.xml",
    18  			Language:    "en-gb",
    19  			Items: []*sparkle.Item{
    20  				{
    21  					Title:       "v1.0.0",
    22  					Description: &sparkle.CdataString{"Test"},
    23  					PubDate:     "Mon, 02 Jan 2006 15:04:05 +0000",
    24  					Version:     "1.0.0",
    25  					Tags:        &sparkle.Tags{CriticalUpdate: true},
    26  					Enclosure: &sparkle.Enclosure{
    27  						URL:         "https://example.com/test_v1.0.0.dmg",
    28  						OS:          "macos",
    29  						Version:     "1.0.0",
    30  						EDSignature: "test",
    31  						Length:      100,
    32  						Type:        "application/x-apple-diskimage",
    33  					},
    34  				},
    35  				{
    36  					Title:          "v1.1.0",
    37  					Description:    &sparkle.CdataString{"\n\t\t\t\t<h2>Test</h2>\n\t\t\t"},
    38  					PubDate:        "Mon, 02 Jan 2007 15:04:05 +0000",
    39  					Version:        "1.1.0",
    40  					CriticalUpdate: &sparkle.CriticalUpdate{Version: "1.0.0"},
    41  					Enclosure: &sparkle.Enclosure{
    42  						URL:         "https://example.com/test_v1.1.0.dmg",
    43  						OS:          "macos",
    44  						Version:     "1.1.0",
    45  						EDSignature: "test",
    46  						Length:      100,
    47  						Type:        "application/x-apple-diskimage",
    48  					},
    49  				},
    50  			},
    51  		}},
    52  	}
    53  
    54  	want := `<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" xmlns:dc="http://purl.org/dc/elements/1.1/">
    55  	<channel>
    56  		<title>Title</title>
    57  		<link>https://example.com/sparkle.xml</link>
    58  		<description>Description</description>
    59  		<language>en-gb</language>
    60  		<item>
    61  			<title>v1.0.0</title>
    62  			<pubDate>Mon, 02 Jan 2006 15:04:05 +0000</pubDate>
    63  			<description><![CDATA[Test]]></description>
    64  			<sparkle:version>1.0.0</sparkle:version>
    65  			<sparkle:tags>
    66  				<sparkle:criticalUpdate></sparkle:criticalUpdate>
    67  			</sparkle:tags>
    68  			<enclosure url="https://example.com/test_v1.0.0.dmg" sparkle:os="macos" sparkle:version="1.0.0" sparkle:edSignature="test" length="100" type="application/x-apple-diskimage"></enclosure>
    69  		</item>
    70  		<item>
    71  			<title>v1.1.0</title>
    72  			<pubDate>Mon, 02 Jan 2007 15:04:05 +0000</pubDate>
    73  			<description><![CDATA[
    74  				<h2>Test</h2>
    75  			]]></description>
    76  			<sparkle:version>1.1.0</sparkle:version>
    77  			<sparkle:criticalUpdate sparkle:version="1.0.0"></sparkle:criticalUpdate>
    78  			<enclosure url="https://example.com/test_v1.1.0.dmg" sparkle:os="macos" sparkle:version="1.1.0" sparkle:edSignature="test" length="100" type="application/x-apple-diskimage"></enclosure>
    79  		</item>
    80  	</channel>
    81  </rss>`
    82  
    83  	b, err := xml.MarshalIndent(in, "", "\t")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	if diff := cmp.Diff(want, string(b)); diff != "" {
    89  		t.Fatal(diff)
    90  	}
    91  
    92  	got := &sparkle.RSS{}
    93  	if err = xml.Unmarshal(b, got); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	if diff := cmp.Diff(in, got); diff != "" {
    98  		t.Fatal(diff)
    99  	}
   100  }