vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
author Martin Soch <Martin.Soch@oracle.com>
Thu, 07 Feb 2013 17:41:41 +0100
brancharithmetic
changeset 700 b9bf26ea0118
parent 626 f08eb4df84c1
child 667 5866e89ef568
permissions -rw-r--r--
Fixed negation for Integer.MIN_VALUE
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@638
    20
import java.io.InputStream;
jaroslav@624
    21
import java.net.URL;
jaroslav@623
    22
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@623
    23
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@623
    24
import org.apidesign.bck2brwsr.vmtest.HttpResource;
jaroslav@623
    25
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@623
    26
import org.testng.annotations.Factory;
jaroslav@623
    27
jaroslav@623
    28
/**
jaroslav@623
    29
 *
jaroslav@623
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@623
    31
 */
jaroslav@623
    32
public class HttpResourceTest {
jaroslav@623
    33
    
jaroslav@623
    34
    @HttpResource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
jaroslav@623
    35
    @BrwsrTest
jaroslav@623
    36
    public String testReadContentViaXHR() throws Exception {
jaroslav@623
    37
        String msg = read("/xhr");
jaroslav@623
    38
        assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
jaroslav@623
    39
        return msg;
jaroslav@623
    40
    }
jaroslav@624
    41
jaroslav@624
    42
    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@624
    43
    @BrwsrTest
jaroslav@624
    44
    public String testReadContentViaURL() throws Exception {
jaroslav@624
    45
        URL url = new URL("http:/url");
jaroslav@624
    46
        String msg = (String) url.getContent();
jaroslav@624
    47
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    48
        return msg;
jaroslav@624
    49
    }
jaroslav@624
    50
    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@624
    51
    @BrwsrTest
jaroslav@624
    52
    public String testReadContentViaURLWithStringParam() throws Exception {
jaroslav@624
    53
        URL url = new URL("http:/url");
jaroslav@624
    54
        String msg = (String) url.getContent(new Class[] { String.class });
jaroslav@624
    55
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    56
        return msg;
jaroslav@624
    57
    }
jaroslav@625
    58
jaroslav@626
    59
    @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
jaroslav@625
    60
    @BrwsrTest
jaroslav@625
    61
    public void testReadByte() throws Exception {
jaroslav@625
    62
        URL url = new URL("http:/bytes");
jaroslav@625
    63
        final Object res = url.getContent(new Class[] { byte[].class });
jaroslav@625
    64
        assert res instanceof byte[] : "Expecting byte[]: " + res;
jaroslav@625
    65
        byte[] arr = (byte[]) res;
jaroslav@625
    66
        assert arr.length == 1 : "One byte " + arr.length;
jaroslav@625
    67
        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
jaroslav@625
    68
    }
jaroslav@638
    69
jaroslav@638
    70
    @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
jaroslav@638
    71
    @BrwsrTest
jaroslav@638
    72
    public void testReadByteViaInputStream() throws Exception {
jaroslav@638
    73
        URL url = new URL("http:/bytes");
jaroslav@638
    74
        InputStream is = url.openStream();
jaroslav@638
    75
        byte[] arr = new byte[10];
jaroslav@638
    76
        int len = is.read(arr);
jaroslav@638
    77
        assert len == 1 : "One byte " + len;
jaroslav@638
    78
        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
jaroslav@638
    79
    }
jaroslav@623
    80
    
jaroslav@623
    81
    @JavaScriptBody(args = { "url" }, body = 
jaroslav@623
    82
          "var req = new XMLHttpRequest();\n"
jaroslav@623
    83
        + "req.open('GET', url, false);\n"
jaroslav@623
    84
        + "req.send();\n"
jaroslav@623
    85
        + "return req.responseText;"
jaroslav@623
    86
    )
jaroslav@623
    87
    private static native String read(String url);
jaroslav@623
    88
    
jaroslav@623
    89
    
jaroslav@623
    90
    @Factory
jaroslav@623
    91
    public static Object[] create() {
jaroslav@623
    92
        return VMTest.create(HttpResourceTest.class);
jaroslav@623
    93
    }
jaroslav@623
    94
}