vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Jan 2013 17:39:47 +0100
branchemul
changeset 623 4af0d3dedb9d
child 624 e79ab81a7656
permissions -rw-r--r--
@HttpResource allows a test to connect back to the server and read static resource
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@623
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@623
    21
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@623
    22
import org.apidesign.bck2brwsr.vmtest.HttpResource;
jaroslav@623
    23
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@623
    24
import org.testng.annotations.Factory;
jaroslav@623
    25
jaroslav@623
    26
/**
jaroslav@623
    27
 *
jaroslav@623
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@623
    29
 */
jaroslav@623
    30
public class HttpResourceTest {
jaroslav@623
    31
    
jaroslav@623
    32
    @HttpResource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
jaroslav@623
    33
    @BrwsrTest
jaroslav@623
    34
    public String testReadContentViaXHR() throws Exception {
jaroslav@623
    35
        String msg = read("/xhr");
jaroslav@623
    36
        assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
jaroslav@623
    37
        return msg;
jaroslav@623
    38
    }
jaroslav@623
    39
    
jaroslav@623
    40
    @JavaScriptBody(args = { "url" }, body = 
jaroslav@623
    41
          "var req = new XMLHttpRequest();\n"
jaroslav@623
    42
        + "req.open('GET', url, false);\n"
jaroslav@623
    43
        + "req.send();\n"
jaroslav@623
    44
        + "return req.responseText;"
jaroslav@623
    45
    )
jaroslav@623
    46
    private static native String read(String url);
jaroslav@623
    47
    
jaroslav@623
    48
    
jaroslav@623
    49
    @Factory
jaroslav@623
    50
    public static Object[] create() {
jaroslav@623
    51
        return VMTest.create(HttpResourceTest.class);
jaroslav@623
    52
    }
jaroslav@623
    53
}