Beispiel #1
0
    public void TestLineWrapping()
    {
        var vcard = new VCard();

        const string UNITEXT = "Dies ist ein wirklich sehr sehr sehr langer Text mit ü, Ö, und ä " + "" +
                               "damit das Line-Wrappping mit Quoted-Printable-Encoding getestet werden kann. " +
                               "Um noch eine Zeile einzufügen, folgt hier noch ein Satz. ";

        const string ASCIITEXT = "This is really a very very long ASCII-Text. This is needed to test the " +
                                 "vCard 2.1 - LineWrapping. That's why I have to write so much even though I have nothing to say.";

        byte[] bytes = CreateBytes();

        vcard.Notes = new TextProperty[]
        {
            new TextProperty(UNITEXT)
        };

        vcard.Keys   = new DataProperty[] { new DataProperty(DataUrl.FromText(ASCIITEXT)) };
        vcard.Photos = new DataProperty[] { new DataProperty(DataUrl.FromBytes(bytes, "image/jpeg")) };

        string s = vcard.ToVcfString(VCdVersion.V2_1);

        Assert.IsNotNull(s);

        Assert.IsTrue(s.Split(new string[] { VCard.NewLine }, StringSplitOptions.None)
                      .All(x => x != null && x.Length <= VCard.MAX_BYTES_PER_LINE));

        _ = VCard.ParseVcf(s);

        Assert.AreEqual(((DataUrl?)vcard.Keys?.First()?.Value)?.GetEmbeddedText(), ASCIITEXT);
        Assert.AreEqual(vcard.Photos?.First()?.Parameters.MediaType, "image/jpeg");
        Assert.IsTrue(((DataUrl?)vcard.Photos?.First()?.Value)?.GetEmbeddedBytes()?.SequenceEqual(bytes) ?? false);
Beispiel #2
0
    public void SerializationTest1()
    {
        const string MEDIA_TYPE = "image/jpeg";

        byte[] data = new byte[] { 1, 2, 3 };

        var dataUri = DataUrl.FromBytes(data, MEDIA_TYPE);

        Assert.IsNotNull(dataUri);
        Assert.AreEqual(dataUri.MimeType.MediaType, MEDIA_TYPE);
        Assert.AreEqual(dataUri.Encoding, DataEncoding.Base64);
        Assert.IsTrue(data.SequenceEqual(dataUri.GetEmbeddedBytes() !));


        var formatter = new BinaryFormatter();

        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, dataUri);

            stream.Position = 0;

            dataUri = (DataUrl)formatter.Deserialize(stream);
        }

        Assert.IsNotNull(dataUri);
        Assert.AreEqual(dataUri.MimeType.MediaType, MEDIA_TYPE);
        Assert.AreEqual(dataUri.Encoding, DataEncoding.Base64);
        Assert.IsTrue(data.SequenceEqual(dataUri.GetEmbeddedBytes() !));
    }
Beispiel #3
0
    /// <summary>
    /// Erstellt einen <see cref="DataUrl"/> aus einer physisch vorhandenen Datei.
    /// </summary>
    /// <param name="path">Absoluter Pfad zu der einzubettenden Datei.</param>
    /// <param name="mimeType">MIME-Typ der einzubettenden Datei. Wenn <c>null</c> angegeben wird,
    /// wird versucht, den MIME-Typ aus der Dateiendung automatisch zu ermitteln.</param>
    /// <returns>Ein <see cref="DataUrl"/>, in den die Daten der mit <paramref name="path"/> referenzierten Datei
    /// eingebettet sind.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="path"/> ist <c>null</c>.</exception>
    /// <exception cref="ArgumentException"><paramref name="path"/> ist kein gültiger Dateipfad oder
    /// <paramref name="mimeType"/> hat kein gültiges Format.</exception>
    /// <exception cref="UriFormatException">Es kann kein <see cref="DataUrl"/> initialisiert werden, z.B.
    /// weil der URI-String länger als 65519 Zeichen ist.</exception>
    /// <exception cref="IOException">E/A-Fehler.</exception>
    public static DataUrl FromFile(string path, string?mimeType = null)
    {
        byte[] bytes = LoadFile(path);
        mimeType ??= MimeTypeConverter.GetMimeTypeFromFileExtension(path);

        return(DataUrl.FromBytes(bytes, mimeType));
    }
Beispiel #4
0
    public void TryCreateTest()
    {
        const string TEXT      = "In Märchenbüchern herumstöbern.";
        const string MIME_TYPE = "application/vnd.api+json";

        byte[] DATA = new byte[] { 1, 2, 3 };



        var dataUrl1 = DataUrl.FromText(TEXT);

        Assert.IsTrue(DataUrl.TryCreate(dataUrl1.ToString(), out DataUrl? dataUrl2));

        Assert.AreEqual(dataUrl2?.MimeType.MediaType, "text/plain");
        Assert.AreEqual(dataUrl2?.MimeType.Parameters?[0].Value, "US-ASCII");
        Assert.AreEqual(TEXT, dataUrl2?.GetEmbeddedText());


        dataUrl1 = DataUrl.FromBytes(DATA, MIME_TYPE);

        Assert.IsTrue(DataUrl.TryCreate(dataUrl1.ToString(), out dataUrl2));

        Assert.AreEqual(dataUrl2?.MimeType.MediaType, MIME_TYPE);
        Assert.AreEqual(dataUrl2?.GetFileExtension(), ".json");
        Assert.IsTrue(dataUrl2?.GetEmbeddedBytes()?.SequenceEqual(DATA) ?? false);


        dataUrl1 = DataUrl.FromBytes(Encoding.GetEncoding("iso-8859-1").GetBytes(TEXT), ";charset=ISO-8859-1");

        Assert.IsTrue(DataUrl.TryCreate(dataUrl1.ToString(), out dataUrl2));

        Assert.AreEqual(dataUrl2?.MimeType.MediaType, "text/plain");
        Assert.AreEqual(dataUrl2?.MimeType.Parameters?[0].Value, "ISO-8859-1");
        Assert.AreEqual(dataUrl2?.Encoding, DataEncoding.Base64);
        Assert.IsTrue(dataUrl2?.ContainsText ?? false);
        Assert.AreEqual(TEXT, dataUrl2?.GetEmbeddedText());

        string test = "data:;charset=UTF-8,Text";

        Assert.IsTrue(DataUrl.TryCreate(test, out dataUrl2));

        Assert.AreEqual(dataUrl2?.EncodedData, "Text");
        Assert.AreEqual(dataUrl2?.MimeType.MediaType, "text/plain");
        Assert.AreEqual(dataUrl2?.MimeType.Parameters?[0].Value, "UTF-8");
        Assert.AreEqual(dataUrl2?.Encoding, DataEncoding.UrlEncoded);
        Assert.AreEqual("Text", dataUrl2?.GetEmbeddedText());

        Assert.IsFalse(DataUrl.TryCreate(null, out _));
        Assert.IsFalse(DataUrl.TryCreate("", out _));
        Assert.IsFalse(DataUrl.TryCreate("http://wwww.folker-kinzel.de/index.htm", out _));
    }
Beispiel #5
0
    public void TestDataUri()
    {
        string text = "http://www.fölkerchen.de";


        string test = DATA_PROTOCOL + "text/plain;charset=utf-8" + ";" + DEFAULT_ENCODING + "," + Uri.EscapeDataString(text);

        Assert.IsTrue(DataUrl.TryCreate(test, out DataUrl? dataUri));
        Assert.AreEqual(text, dataUri?.GetEmbeddedText());

        dataUri = DataUrl.FromText(text);
        Assert.IsNotNull(dataUri);

        dataUri = DataUrl.FromBytes(new byte[] { 1, 2, 3 }, "application/x-octet");
        Assert.IsNotNull(dataUri);
    }