package net.ranides.assira.io.uri.impl;

import org.junit.Test;

import net.ranides.assira.io.IOStreams;
import net.ranides.assira.io.uri.URIHandle;
import net.ranides.assira.text.FormatHex;

import java.io.IOException;
import java.net.URISyntaxException;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class CDataHandlerTest {

    @Test
    public void testText() throws IOException, URISyntaxException {
        URIHandle handle1 = new CDataHandler().resolve("data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678");
        URIHandle handle2 = new CDataHandler().resolve("data:text/plain,hello%20world");
        URIHandle handle3 = new CDataHandler().resolve("data:text/plain;charset=UTF-8;base64,xbpyZWJpxJkgbmEgxYLEhWNlIHNrYWN6ZSBwbyDFgsSFY2U=");
        URIHandle handle4 = new CDataHandler().resolve("data:text/plain;base64,xbpyZWJpxJkgbmEgxYLEhWNlIHNrYWN6ZSBwbyDFgsSFY2U=");
        URIHandle handle5 = new CDataHandler().resolve("data:text/plain;charset=iso-8859-7,%be%20%f0%20%ee");


        assertEquals("the data:1234,5678", handle1.text());
        assertEquals("hello world", handle2.text());
        assertEquals("źrebię na łące skacze po łące", handle3.text());
        assertEquals("źrebię na łące skacze po łące", handle4.text());
        assertEquals("Ύ π ξ", handle5.text());
    }

    @Test
    public void testData() throws IOException, URISyntaxException {
        URIHandle handle1 = new CDataHandler().resolve("data:application/octet-stream;base64,QkNERXGRoMDe");

        byte[] expected = FormatHex.parse("42 43 44 45 71 91 a0 c0 de");
        byte[] binary = IOStreams.read(handle1.istream());
        assertArrayEquals(expected, binary);
    }
}