package tinycdxserver;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ServerTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void test() throws IOException {
        File root = folder.newFolder();
        DataStore manager = new DataStore(root);
        Server server = new Server(manager, "127.0.0.1", -1);
        server.post(new DummySession("/test").data("- 20050614070159 http://nla.gov.au/ text/html 200 AKMCCEPOOWFMGGO5635HFZXGFRLRGWIX - - - 337023 NLA-AU-CRAWL-000-20050614070144-00003-crawling016.archive.org\n"));

    }

    private static class DummySession implements NanoHTTPD.IHTTPSession {
        InputStream stream = new ByteArrayInputStream(new byte[0]);
        Map<String, String> parms = new HashMap<String, String>();
        String url;

        public DummySession(String url) {
            this.url = url;
        }

        public DummySession data(String data) {
            stream = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
            return this;
        }

        @Override
        public void execute() throws IOException {
            // nothing
        }

        @Override
        public Map<String, String> getParms() {
            return parms;
        }

        @Override
        public Map<String, String> getHeaders() {
            return Collections.emptyMap();
        }

        @Override
        public String getUri() {
            return url;
        }

        @Override
        public String getQueryParameterString() {
            return "";
        }

        @Override
        public NanoHTTPD.Method getMethod() {
            return NanoHTTPD.Method.GET;
        }

        @Override
        public InputStream getInputStream() {
            return stream;
        }
    }
}

@@ -123,105 +123,9 @@
 .io.
-ByteArrayInputStream;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+*
 ;%0Aim
@@ -234,16 +234,151 @@
 l.Map;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 public c
@@ -886,13 +886,679 @@
 rg%5Cn
-%22));%0A
+- 20050614070159 http://example.com/ text/html 200 AKMCCEPOOWFMGGO5635HFZXGFRLRGWIX - - - 337023 NLA-AU-CRAWL-000-20050614070144-00003-crawling016.archive.org%5Cn%22));%0A        NanoHTTPD.Response response = server.query(new DummySession(%22/test%22).parm(%22url%22, %22nla.gov.au%22));%0A        assertEquals(NanoHTTPD.Response.Status.OK, response.getStatus());%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        response.send(out);%0A        String data = out.toString(%22UTF-8%22);%0A        assertTrue(data.indexOf(%22au,gov,nla)/ 20050614070159 http://nla.gov.au/ text/html 200 AKMCCEPOOWFMGGO5635HFZXGFRLRGWIX 0%22) != -1);%0A        assertTrue(data.indexOf(%22example%22) == -1);
 %0A   
@@ -1916,24 +1916,24 @@
 ing data) %7B%0A
-
             
@@ -2036,32 +2036,164 @@
 his;%0A        %7D%0A%0A
+        public DummySession parm(String key, String value) %7B%0A            parms.put(key, value);%0A            return this;%0A        %7D%0A%0A
         @Overrid

Test basic queries