jaroslav@623: /** jaroslav@623: * Back 2 Browser Bytecode Translator jaroslav@623: * Copyright (C) 2012 Jaroslav Tulach jaroslav@623: * jaroslav@623: * This program is free software: you can redistribute it and/or modify jaroslav@623: * it under the terms of the GNU General Public License as published by jaroslav@623: * the Free Software Foundation, version 2 of the License. jaroslav@623: * jaroslav@623: * This program is distributed in the hope that it will be useful, jaroslav@623: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@623: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@623: * GNU General Public License for more details. jaroslav@623: * jaroslav@623: * You should have received a copy of the GNU General Public License jaroslav@623: * along with this program. Look for COPYING file in the top folder. jaroslav@623: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@623: */ jaroslav@623: package org.apidesign.bck2brwsr.tck; jaroslav@623: jaroslav@638: import java.io.InputStream; jaroslav@624: import java.net.URL; jaroslav@623: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@623: import org.apidesign.bck2brwsr.vmtest.BrwsrTest; jaroslav@667: import org.apidesign.bck2brwsr.vmtest.Http; jaroslav@623: import org.apidesign.bck2brwsr.vmtest.VMTest; jaroslav@623: import org.testng.annotations.Factory; jaroslav@623: jaroslav@623: /** jaroslav@623: * jaroslav@623: * @author Jaroslav Tulach jaroslav@623: */ jaroslav@623: public class HttpResourceTest { jaroslav@623: jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain") jaroslav@667: }) jaroslav@623: @BrwsrTest jaroslav@667: jaroslav@667: jaroslav@623: public String testReadContentViaXHR() throws Exception { jaroslav@623: String msg = read("/xhr"); jaroslav@623: assert "Hello Brwsr!".equals(msg) : "The message was " + msg; jaroslav@623: return msg; jaroslav@623: } jaroslav@624: jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") jaroslav@667: }) jaroslav@624: @BrwsrTest jaroslav@624: public String testReadContentViaURL() throws Exception { jaroslav@624: URL url = new URL("http:/url"); jaroslav@624: String msg = (String) url.getContent(); jaroslav@624: assert "Hello via URL!".equals(msg) : "The message was " + msg; jaroslav@624: return msg; jaroslav@624: } jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") jaroslav@667: }) jaroslav@624: @BrwsrTest jaroslav@624: public String testReadContentViaURLWithStringParam() throws Exception { jaroslav@624: URL url = new URL("http:/url"); jaroslav@624: String msg = (String) url.getContent(new Class[] { String.class }); jaroslav@624: assert "Hello via URL!".equals(msg) : "The message was " + msg; jaroslav@624: return msg; jaroslav@624: } jaroslav@625: jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") jaroslav@667: }) jaroslav@625: @BrwsrTest jaroslav@625: public void testReadByte() throws Exception { jaroslav@625: URL url = new URL("http:/bytes"); jaroslav@625: final Object res = url.getContent(new Class[] { byte[].class }); jaroslav@625: assert res instanceof byte[] : "Expecting byte[]: " + res; jaroslav@625: byte[] arr = (byte[]) res; jaroslav@625: assert arr.length == 1 : "One byte " + arr.length; jaroslav@625: assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]); jaroslav@625: } jaroslav@638: jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") jaroslav@667: }) jaroslav@638: @BrwsrTest jaroslav@638: public void testReadByteViaInputStream() throws Exception { jaroslav@638: URL url = new URL("http:/bytes"); jaroslav@638: InputStream is = url.openStream(); jaroslav@638: byte[] arr = new byte[10]; jaroslav@638: int len = is.read(arr); jaroslav@638: assert len == 1 : "One byte " + len; jaroslav@638: assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]); jaroslav@638: } jaroslav@623: jaroslav@623: @JavaScriptBody(args = { "url" }, body = jaroslav@623: "var req = new XMLHttpRequest();\n" jaroslav@623: + "req.open('GET', url, false);\n" jaroslav@623: + "req.send();\n" jaroslav@623: + "return req.responseText;" jaroslav@623: ) jaroslav@623: private static native String read(String url); jaroslav@623: jaroslav@623: jaroslav@623: @Factory jaroslav@623: public static Object[] create() { jaroslav@623: return VMTest.create(HttpResourceTest.class); jaroslav@623: } jaroslav@623: }