vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Jan 2013 18:45:00 +0100
branchemul
changeset 625 8efbe49b283f
parent 624 e79ab81a7656
child 626 f08eb4df84c1
permissions -rw-r--r--
Can read binary data via new URL('').getContent(byte[].class)
jaroslav@623
     1
/**
jaroslav@623
     2
 * Back 2 Browser Bytecode Translator
jaroslav@623
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@623
     4
 *
jaroslav@623
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@623
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@623
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@623
     8
 *
jaroslav@623
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@623
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@623
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@623
    12
 * GNU General Public License for more details.
jaroslav@623
    13
 *
jaroslav@623
    14
 * You should have received a copy of the GNU General Public License
jaroslav@623
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@623
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@623
    17
 */
jaroslav@623
    18
package org.apidesign.bck2brwsr.tck;
jaroslav@623
    19
jaroslav@624
    20
import java.net.URL;
jaroslav@623
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@623
    22
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@623
    23
import org.apidesign.bck2brwsr.vmtest.HttpResource;
jaroslav@623
    24
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@623
    25
import org.testng.annotations.Factory;
jaroslav@623
    26
jaroslav@623
    27
/**
jaroslav@623
    28
 *
jaroslav@623
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@623
    30
 */
jaroslav@623
    31
public class HttpResourceTest {
jaroslav@623
    32
    
jaroslav@623
    33
    @HttpResource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
jaroslav@623
    34
    @BrwsrTest
jaroslav@623
    35
    public String testReadContentViaXHR() throws Exception {
jaroslav@623
    36
        String msg = read("/xhr");
jaroslav@623
    37
        assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
jaroslav@623
    38
        return msg;
jaroslav@623
    39
    }
jaroslav@624
    40
jaroslav@624
    41
    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@624
    42
    @BrwsrTest
jaroslav@624
    43
    public String testReadContentViaURL() throws Exception {
jaroslav@624
    44
        URL url = new URL("http:/url");
jaroslav@624
    45
        String msg = (String) url.getContent();
jaroslav@624
    46
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    47
        return msg;
jaroslav@624
    48
    }
jaroslav@624
    49
    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@624
    50
    @BrwsrTest
jaroslav@624
    51
    public String testReadContentViaURLWithStringParam() throws Exception {
jaroslav@624
    52
        URL url = new URL("http:/url");
jaroslav@624
    53
        String msg = (String) url.getContent(new Class[] { String.class });
jaroslav@624
    54
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    55
        return msg;
jaroslav@624
    56
    }
jaroslav@625
    57
jaroslav@625
    58
    @HttpResource(path = "/bytes", content = "\u00fe", mimeType = "x-application/binary")
jaroslav@625
    59
    @BrwsrTest
jaroslav@625
    60
    public void testReadByte() throws Exception {
jaroslav@625
    61
        URL url = new URL("http:/bytes");
jaroslav@625
    62
        final Object res = url.getContent(new Class[] { byte[].class });
jaroslav@625
    63
        assert res instanceof byte[] : "Expecting byte[]: " + res;
jaroslav@625
    64
        byte[] arr = (byte[]) res;
jaroslav@625
    65
        assert arr.length == 1 : "One byte " + arr.length;
jaroslav@625
    66
        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
jaroslav@625
    67
    }
jaroslav@623
    68
    
jaroslav@623
    69
    @JavaScriptBody(args = { "url" }, body = 
jaroslav@623
    70
          "var req = new XMLHttpRequest();\n"
jaroslav@623
    71
        + "req.open('GET', url, false);\n"
jaroslav@623
    72
        + "req.send();\n"
jaroslav@623
    73
        + "return req.responseText;"
jaroslav@623
    74
    )
jaroslav@623
    75
    private static native String read(String url);
jaroslav@623
    76
    
jaroslav@623
    77
    
jaroslav@623
    78
    @Factory
jaroslav@623
    79
    public static Object[] create() {
jaroslav@623
    80
        return VMTest.create(HttpResourceTest.class);
jaroslav@623
    81
    }
jaroslav@623
    82
}