rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
changeset 1723 3a1f262311cf
parent 1034 28dc692f3b11
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java	Wed Nov 19 19:32:00 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.tck;
    1.22 +
    1.23 +import java.io.InputStream;
    1.24 +import java.net.URL;
    1.25 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.26 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    1.27 +import org.apidesign.bck2brwsr.vmtest.Http;
    1.28 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.29 +import org.testng.annotations.Factory;
    1.30 +
    1.31 +/**
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +public class HttpResourceTest {
    1.36 +    
    1.37 +    @Http({
    1.38 +        @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
    1.39 +    })
    1.40 +    @BrwsrTest
    1.41 +    
    1.42 +    
    1.43 +    public String testReadContentViaXHR() throws Exception {
    1.44 +        String msg = read("/xhr");
    1.45 +        assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
    1.46 +        return msg;
    1.47 +    }
    1.48 +
    1.49 +    @Http({
    1.50 +        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    1.51 +    })
    1.52 +    @BrwsrTest
    1.53 +    public String testReadContentViaURL() throws Exception {
    1.54 +        URL url = new URL("http:/url");
    1.55 +        String msg = (String) url.getContent();
    1.56 +        assert "Hello via URL!".equals(msg) : "The message was " + msg;
    1.57 +        return msg;
    1.58 +    }
    1.59 +    @Http({
    1.60 +        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    1.61 +    })
    1.62 +    @BrwsrTest
    1.63 +    public String testReadContentViaURLWithStringParam() throws Exception {
    1.64 +        URL url = new URL("http:/url");
    1.65 +        String msg = (String) url.getContent(new Class[] { String.class });
    1.66 +        assert "Hello via URL!".equals(msg) : "The message was " + msg;
    1.67 +        return msg;
    1.68 +    }
    1.69 +
    1.70 +    @Http({
    1.71 +        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    1.72 +    })
    1.73 +    @BrwsrTest
    1.74 +    public void testReadByte() throws Exception {
    1.75 +        URL url = new URL("http:/bytes");
    1.76 +        final Object res = url.getContent(new Class[] { byte[].class });
    1.77 +        assert res instanceof byte[] : "Expecting byte[]: " + res;
    1.78 +        byte[] arr = (byte[]) res;
    1.79 +        assert arr.length == 1 : "One byte " + arr.length;
    1.80 +        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
    1.81 +    }
    1.82 +
    1.83 +    @Http({
    1.84 +        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    1.85 +    })
    1.86 +    @BrwsrTest
    1.87 +    public void testReadByteViaInputStream() throws Exception {
    1.88 +        URL url = new URL("http:/bytes");
    1.89 +        InputStream is = url.openStream();
    1.90 +        byte[] arr = new byte[10];
    1.91 +        int len = is.read(arr);
    1.92 +        assert len == 1 : "One byte " + len;
    1.93 +        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
    1.94 +    }
    1.95 +    
    1.96 +    @JavaScriptBody(args = { "url" }, body = 
    1.97 +          "var req = new XMLHttpRequest();\n"
    1.98 +        + "req.open('GET', url, false);\n"
    1.99 +        + "req.send();\n"
   1.100 +        + "return req.responseText;"
   1.101 +    )
   1.102 +    private static native String read(String url);
   1.103 +    
   1.104 +    
   1.105 +    @Factory
   1.106 +    public static Object[] create() {
   1.107 +        return VMTest.create(HttpResourceTest.class);
   1.108 +    }
   1.109 +}