Skip to content

Commit

Permalink
add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
evacchi committed Dec 24, 2024
1 parent d5e23db commit 0c87014
Showing 3 changed files with 162 additions and 29 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/extism/chicory/sdk/HostEnv.java
Original file line number Diff line number Diff line change
@@ -267,6 +267,9 @@ public class Http {
HttpResponse<byte[]> lastResponse;

public Http(String[] allowedHosts) {
if (allowedHosts == null) {
allowedHosts = new String[0];
}
this.hostPatterns = new HostPattern[allowedHosts.length];
for (int i = 0; i < allowedHosts.length; i++) {
this.hostPatterns[i] = new HostPattern(allowedHosts[i]);
@@ -328,7 +331,7 @@ byte[] request(String method, URI uri, Map<String, String> headers, byte[] reque
}

var host = uri.getHost();
if (Arrays.stream(hostPatterns).anyMatch(p -> !p.matches(host))) {
if (Arrays.stream(hostPatterns).noneMatch(p -> p.matches(host))) {
throw new ExtismException(String.format("HTTP request to '%s' is not allowed", host));
}

@@ -350,9 +353,12 @@ byte[] request(String method, URI uri, Map<String, String> headers, byte[] reque
}

long[] statusCode(Instance instance, long... args) {
return new long[]{lastResponse == null ? 0 : lastResponse.statusCode()};
return new long[]{statusCode()};
}

int statusCode() {
return lastResponse == null ? 0 : lastResponse.statusCode();
}

long[] headers(Instance instance, long[] longs) {
var result = new long[1];
27 changes: 0 additions & 27 deletions src/test/java/org/extism/chicory/sdk/HostEnvTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package org.extism.chicory.sdk;

import com.dylibso.chicory.log.SystemLogger;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@@ -29,27 +25,4 @@ public void testShowcase() {
long ptr = hostEnv.memory().alloc(size);
assertEquals(hostEnv.memory().length(ptr), size);
}

public void testHttp() {
var logger = new SystemLogger();
var hostEnv = new HostEnv(new Kernel(), Map.of(), new String[0], logger);

byte[] response = hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
JsonObject responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));

byte[] response2 = hostEnv.http().request(
"POST",
URI.create("http://httpbin.org/post"),
Map.of(),
"hello".getBytes(StandardCharsets.UTF_8));


JsonObject responseObject2 = Json.createReader(new ByteArrayInputStream(response2)).readObject();
assertEquals("hello", responseObject2.getString("data"));
}
}
154 changes: 154 additions & 0 deletions src/test/java/org/extism/chicory/sdk/HttpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.extism.chicory.sdk;

import com.dylibso.chicory.log.SystemLogger;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class HttpTest extends TestCase {

public void testNoAllowedHosts() {
var logger = new SystemLogger();

var noAllowedHosts = new String[0];
var hostEnv = new HostEnv(new Kernel(), Map.of(), noAllowedHosts, logger);

try {
hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
fail("Should have thrown an exception");
} catch (ExtismException e) {
assertEquals("HTTP request to 'httpbin.org' is not allowed", e.getMessage());
}
}
public void testAllowSingleHost() {
var logger = new SystemLogger();

var anyHost = new String[]{"httpbin.org"};
var hostEnv = new HostEnv(new Kernel(), Map.of(), anyHost, logger);

byte[] response = hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
JsonObject responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));

byte[] response2 = hostEnv.http().request(
"POST",
URI.create("http://httpbin.org/post"),
Map.of(),
"hello".getBytes(StandardCharsets.UTF_8));

JsonObject responseObject2 = Json.createReader(new ByteArrayInputStream(response2)).readObject();
assertEquals("hello", responseObject2.getString("data"));

try {
hostEnv.http().request(
"GET",
URI.create("http://example.com"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
fail("Should have thrown an exception");
} catch (ExtismException e) {
assertEquals("HTTP request to 'example.com' is not allowed", e.getMessage());
}
}

public void testAllowHostPattern() {
var logger = new SystemLogger();

var anyHost = new String[]{"*.httpbin.org"};
var hostEnv = new HostEnv(new Kernel(), Map.of(), anyHost, logger);

byte[] response = hostEnv.http().request(
"GET",
URI.create("http://www.httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
JsonObject responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));


try {
hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
fail("Should have thrown an exception");
} catch (ExtismException e) {
assertEquals("HTTP request to 'httpbin.org' is not allowed", e.getMessage());
}
}


public void testAllowMultiHostPattern() {
var logger = new SystemLogger();

var anyHost = new String[]{"*.httpbin.org", "httpbin.org"};
var hostEnv = new HostEnv(new Kernel(), Map.of(), anyHost, logger);

byte[] response = hostEnv.http().request(
"GET",
URI.create("http://www.httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
JsonObject responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));


response = hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));
}


public void testAllowAnyHost() {
var logger = new SystemLogger();

var anyHost = new String[]{"*"};
var hostEnv = new HostEnv(new Kernel(), Map.of(), anyHost, logger);

byte[] response = hostEnv.http().request(
"GET",
URI.create("http://www.httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
JsonObject responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));


response = hostEnv.http().request(
"GET",
URI.create("http://httpbin.org/headers"),
Map.of("X-Custom-Header", "hello"),
new byte[0]);
responseObject = Json.createReader(new ByteArrayInputStream(response)).readObject();
assertEquals("hello", responseObject.getJsonObject("headers").getString("X-Custom-Header"));

response = hostEnv.http().request(
"GET",
URI.create("http://example.com/"),
Map.of(),
new byte[0]);

assertEquals(200, hostEnv.http().statusCode());
assertTrue(response.length > 0);
}


}

0 comments on commit 0c87014

Please sign in to comment.