Ejemplo n.º 1
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 _));
    }
Ejemplo n.º 2
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);
    }