diff --git a/src/test/java/com/eclipsesource/v8/AllTests.java b/src/test/java/com/eclipsesource/v8/AllTests.java index 7b977a476..a1ce0e358 100644 --- a/src/test/java/com/eclipsesource/v8/AllTests.java +++ b/src/test/java/com/eclipsesource/v8/AllTests.java @@ -7,7 +7,8 @@ * * Contributors: * EclipseSource - initial API and implementation - ******************************************************************************/ + * Dukehoff GmbH - node.js server test +******************************************************************************/ package com.eclipsesource.v8; import org.junit.runner.RunWith; @@ -32,7 +33,8 @@ @SuiteClasses({ V8RuntimeNotLoadedTest.class, LibraryLoaderTest.class, V8ObjectTest.class, V8Test.class, V8ArrayTest.class, V8JSFunctionCallTest.class, V8CallbackTest.class, V8ScriptCompilationExceptionTest.class, V8ScriptExecutionExceptionTest.class, V8ObjectUtilsTest.class, V8TypedArraysTest.class, NullScriptExecuteTest.class, V8MultiThreadTest.class, V8LockerTest.class, V8ExecutorTest.class, V8MapTest.class, V8PropertyMapTest.class, - DebugHandlerTest.class, ExecutionStateTest.class, FrameTest.class, ScopeTest.class, ScriptBreakPointTest.class, MirrorTest.class, BreakEventTest.class, NodeJSTest.class }) + DebugHandlerTest.class, ExecutionStateTest.class, FrameTest.class, ScopeTest.class, ScriptBreakPointTest.class, MirrorTest.class, BreakEventTest.class, + NodeJSTest.class, NodeJSRunServerTest.class }) public class AllTests { } diff --git a/src/test/java/com/eclipsesource/v8/NodeJSRunServerTest.java b/src/test/java/com/eclipsesource/v8/NodeJSRunServerTest.java new file mode 100644 index 000000000..b07391332 --- /dev/null +++ b/src/test/java/com/eclipsesource/v8/NodeJSRunServerTest.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2016 EclipseSource and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * EclipseSource - initial API and implementation + * Dukehoff GmbH - node.js server test + ******************************************************************************/ + +package com.eclipsesource.v8; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.URL; +import java.net.URLConnection; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.junit.After; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Before; + +public class NodeJSRunServerTest { + private NodeJS node; + + @Before + public void createNode() { + node = NodeJS.createNodeJS(); + } + + @After + public void destroyNode() { + node.release(); + } + + @Test + public void runServerAndConnetToIt() throws IOException, InterruptedException, ExecutionException { + final int port = findEmptyPort(); + String code = + "var http = require('http');\n" + + "function handleRequest(request, response){\n" + + " response.end('Connected: ' + request.url);\n" + + "}\n" + + "var p = " + port + ";\n" + + "var server = http.createServer(handleRequest);\n" + + "server.listen(p, function(){\n" + + " console.log(\"Server listening on: http://localhost:%s\", p);\n" + + "});\n" + + "return server"; + + File serverScript = createScriptFile(code); + V8Object server = node.require(serverScript); + assertNotNull(server); + + Future done = Executors.newSingleThreadExecutor().submit(new Callable() { + @Override + public String call() throws Exception { + URL u = new URL("http://127.0.0.1:" + port + "/hello"); + final URLConnection conn = u.openConnection(); + + InputStreamReader r = new InputStreamReader(conn.getInputStream()); + BufferedReader br = new BufferedReader(r); + String line = br.readLine(); + return line; + } + }); + + while (!done.isDone()) { + process(node, done); + } + + assertEquals("Connected: /hello", done.get()); + + server.release(); + serverScript.delete(); + } + + private File createScriptFile(String code) throws IOException { + File serverScript = File.createTempFile("temp", ".js"); + FileWriter w = new FileWriter(serverScript); + w.write(code); + w.close(); + serverScript.deleteOnExit(); + return serverScript; + } + + private int findEmptyPort() throws IOException { + final ServerSocket ss = new ServerSocket(0); + final int port = ss.getLocalPort(); + ss.close(); + return port; + } + + private void process(NodeJS node, Future await) { + while (node.isRunning()) { + if (await != null && await.isDone()) { + break; + } + node.handleMessage(); + } + } +}