Skip to content

Commit

Permalink
chore: add integration testing for service bindings
Browse files Browse the repository at this point in the history
Adds integration testing for service bindings by creating a new worker
in the worker-sandbox directory and mounting it with Miniflare.
  • Loading branch information
zebp committed Nov 17, 2022
1 parent cd1d989 commit 9945338
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions worker-sandbox/remote-service/package.json
@@ -0,0 +1,3 @@
{
"main": "./service.js"
}
3 changes: 3 additions & 0 deletions worker-sandbox/remote-service/service.js
@@ -0,0 +1,3 @@
addEventListener('fetch', event => {
event.respondWith(new Response('hello world'))
})
2 changes: 2 additions & 0 deletions worker-sandbox/remote-service/wrangler.toml
@@ -0,0 +1,2 @@
name = "remote-service"
type = "javascript"
11 changes: 11 additions & 0 deletions worker-sandbox/src/lib.rs
Expand Up @@ -646,6 +646,17 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
Ok(resp)
}
})
.get_async("/remote-by-request", |req, ctx| async move {
let fetcher = ctx.service("remote")?;
fetcher.fetch_request(req).await
})
.get_async("/remote-by-path", |req, ctx| async move {
let fetcher = ctx.service("remote")?;
let mut init = RequestInit::new();
init.with_method(Method::Post);

fetcher.fetch(req.url()?.to_string(), Some(init)).await
})
.or_else_any_method_async("/*catchall", |_, ctx| async move {
console_log!(
"[or_else_any_method_async] caught: {}",
Expand Down
9 changes: 9 additions & 0 deletions worker-sandbox/tests/requests.rs
Expand Up @@ -469,3 +469,12 @@ fn cache_api() {
let body: serde_json::Value = post(delete_endpoint.as_str(), |r| r).json().unwrap();
assert_eq!("ResponseNotFound", body);
}

#[test]
fn test_service_binding() {
let body: String = get("remote-by-request", |r| r).text().unwrap();
assert_eq!(body, "hello world");

let body: String = get("remote-by-path", |r| r).text().unwrap();
assert_eq!(body, "hello world");
}
7 changes: 7 additions & 0 deletions worker-sandbox/wrangler.toml
Expand Up @@ -11,6 +11,13 @@ kv_namespaces = [

vars = { SOME_VARIABLE = "some value" }

[[services]]
binding = "remote"
service = "remote-service"

[miniflare.mounts]
remote-service = "./remote-service"

[durable_objects]
bindings = [{ name = "COUNTER", class_name = "Counter" }, { name = "ALARM", class_name = "AlarmObject" }]

Expand Down
8 changes: 4 additions & 4 deletions worker/src/fetcher.rs
Expand Up @@ -2,19 +2,19 @@ use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use worker_sys::{Fetcher as FetcherSys, Response as ResponseSys};

use crate::{Request, RequestInit, Response, Result, env::EnvBinding};
use crate::{env::EnvBinding, Request, RequestInit, Response, Result};

/// A struct for invoking fetch events to other Workers.
pub struct Fetcher(FetcherSys);

impl Fetcher {
/// Invoke a fetch event in a worker with a path and optionally a [RequestInit].
/// Invoke a fetch event in a worker with a url and optionally a [RequestInit].
pub async fn fetch(
&self,
path: impl Into<String>,
url: impl Into<String>,
init: Option<RequestInit>,
) -> Result<Response> {
let path = path.into();
let path = url.into();
let promise = match init {
Some(ref init) => self.0.fetch_with_str_and_init(&path, &init.into()),
None => self.0.fetch_with_str(&path),
Expand Down
2 changes: 1 addition & 1 deletion worker/src/router.rs
Expand Up @@ -10,7 +10,7 @@ use crate::{
http::Method,
request::Request,
response::Response,
Result, Fetcher,
Fetcher, Result,
};

type HandlerFn<D> = fn(Request, RouteContext<D>) -> Result<Response>;
Expand Down

0 comments on commit 9945338

Please sign in to comment.