rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 667 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java@5866e89ef568
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
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@667
    24
import org.apidesign.bck2brwsr.vmtest.Http;
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@667
    34
    @Http({
jaroslav@667
    35
        @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
jaroslav@667
    36
    })
jaroslav@623
    37
    @BrwsrTest
jaroslav@667
    38
    
jaroslav@667
    39
    
jaroslav@623
    40
    public String testReadContentViaXHR() throws Exception {
jaroslav@623
    41
        String msg = read("/xhr");
jaroslav@623
    42
        assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
jaroslav@623
    43
        return msg;
jaroslav@623
    44
    }
jaroslav@624
    45
jaroslav@667
    46
    @Http({
jaroslav@667
    47
        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@667
    48
    })
jaroslav@624
    49
    @BrwsrTest
jaroslav@624
    50
    public String testReadContentViaURL() throws Exception {
jaroslav@624
    51
        URL url = new URL("http:/url");
jaroslav@624
    52
        String msg = (String) url.getContent();
jaroslav@624
    53
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    54
        return msg;
jaroslav@624
    55
    }
jaroslav@667
    56
    @Http({
jaroslav@667
    57
        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
jaroslav@667
    58
    })
jaroslav@624
    59
    @BrwsrTest
jaroslav@624
    60
    public String testReadContentViaURLWithStringParam() throws Exception {
jaroslav@624
    61
        URL url = new URL("http:/url");
jaroslav@624
    62
        String msg = (String) url.getContent(new Class[] { String.class });
jaroslav@624
    63
        assert "Hello via URL!".equals(msg) : "The message was " + msg;
jaroslav@624
    64
        return msg;
jaroslav@624
    65
    }
jaroslav@625
    66
jaroslav@667
    67
    @Http({
jaroslav@667
    68
        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
jaroslav@667
    69
    })
jaroslav@625
    70
    @BrwsrTest
jaroslav@625
    71
    public void testReadByte() throws Exception {
jaroslav@625
    72
        URL url = new URL("http:/bytes");
jaroslav@625
    73
        final Object res = url.getContent(new Class[] { byte[].class });
jaroslav@625
    74
        assert res instanceof byte[] : "Expecting byte[]: " + res;
jaroslav@625
    75
        byte[] arr = (byte[]) res;
jaroslav@625
    76
        assert arr.length == 1 : "One byte " + arr.length;
jaroslav@625
    77
        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
jaroslav@625
    78
    }
jaroslav@638
    79
jaroslav@667
    80
    @Http({
jaroslav@667
    81
        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
jaroslav@667
    82
    })
jaroslav@638
    83
    @BrwsrTest
jaroslav@638
    84
    public void testReadByteViaInputStream() throws Exception {
jaroslav@638
    85
        URL url = new URL("http:/bytes");
jaroslav@638
    86
        InputStream is = url.openStream();
jaroslav@638
    87
        byte[] arr = new byte[10];
jaroslav@638
    88
        int len = is.read(arr);
jaroslav@638
    89
        assert len == 1 : "One byte " + len;
jaroslav@638
    90
        assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
jaroslav@638
    91
    }
jaroslav@623
    92
    
jaroslav@623
    93
    @JavaScriptBody(args = { "url" }, body = 
jaroslav@623
    94
          "var req = new XMLHttpRequest();\n"
jaroslav@623
    95
        + "req.open('GET', url, false);\n"
jaroslav@623
    96
        + "req.send();\n"
jaroslav@623
    97
        + "return req.responseText;"
jaroslav@623
    98
    )
jaroslav@623
    99
    private static native String read(String url);
jaroslav@623
   100
    
jaroslav@623
   101
    
jaroslav@623
   102
    @Factory
jaroslav@623
   103
    public static Object[] create() {
jaroslav@623
   104
        return VMTest.create(HttpResourceTest.class);
jaroslav@623
   105
    }
jaroslav@623
   106
}