diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/GlobalInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/GlobalInterceptor.java index d09fd3c491..e3610640a2 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/GlobalInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/GlobalInterceptor.java @@ -22,7 +22,7 @@ * @description The global chain applies plugins to all endpoints, enabling centralized features * such as global user authentication, logging, and other cross-cutting concerns. */ -@MCElement(name = "global", excludeFromFlow = true, component = false, topLevel = true) +@MCElement(name = "global", excludeFromFlow = true, component = false, topLevel = true, noEnvelope = true) public class GlobalInterceptor extends AbstractFlowWithChildrenInterceptor { @Override diff --git a/distribution/examples/extending-membrane/global-interceptor/README.md b/distribution/examples/extending-membrane/global-interceptor/README.md index b934e650da..c0d5da8461 100644 --- a/distribution/examples/extending-membrane/global-interceptor/README.md +++ b/distribution/examples/extending-membrane/global-interceptor/README.md @@ -11,11 +11,11 @@ Some functionalities, such as authentication and rate limiting, are required acr 2. **Test the APIs:** - **API 1 (Port 2000) → Returns `200 OK`** ```sh - curl -i http://localhost:2000 + curl -i http://localhost:2000/foo ``` - **API 2 (Port 2001) → Returns `404 Not Found`** ```sh - curl -i http://localhost:2001 + curl -i http://localhost:2001/bar ``` **Check the request:** both contain CORS headers -3. **Check `proxies.xml`** to see how the global interceptors are applied. +3. **Check `apis.yaml`** to see how the global interceptors are applied. diff --git a/distribution/examples/extending-membrane/global-interceptor/apis.yaml b/distribution/examples/extending-membrane/global-interceptor/apis.yaml new file mode 100644 index 0000000000..b86a84ec71 --- /dev/null +++ b/distribution/examples/extending-membrane/global-interceptor/apis.yaml @@ -0,0 +1,31 @@ +# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.4.json + +global: + - log: + message: "Path: ${path}" + - response: + - setHeader: + name: Access-Control-Allow-Origin + value: "*" + - setHeader: + name: Access-Control-Allow-Methods + value: GET, POST + - setHeader: + name: Access-Control-Allow-Headers + value: Content-Type + +--- + +api: + port: 2000 + flow: + - return: + status: 200 + +--- + +api: + port: 2001 + flow: + - return: + status: 404 \ No newline at end of file diff --git a/distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/test/GlobalInterceptorExampleTest.java b/distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/test/GlobalInterceptorExampleTest.java index e524cdbf1d..6b3ceaf567 100644 --- a/distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/test/GlobalInterceptorExampleTest.java +++ b/distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/test/GlobalInterceptorExampleTest.java @@ -14,10 +14,11 @@ package com.predic8.membrane.examples.withoutinternet.test; -import com.predic8.membrane.examples.util.AbstractSampleMembraneStartStopTestcase; -import org.junit.jupiter.api.Test; +import com.predic8.membrane.examples.util.*; +import org.junit.jupiter.api.*; -import static io.restassured.RestAssured.given; +import static com.predic8.membrane.core.http.Header.*; +import static io.restassured.RestAssured.*; public class GlobalInterceptorExampleTest extends AbstractSampleMembraneStartStopTestcase { @@ -28,30 +29,28 @@ protected String getExampleDirName() { // @formatter:off @Test - public void request1() { + void request1() { given() .when() .get("http://localhost:2000") .then() .assertThat() .header("Access-Control-Allow-Origin", "*") - .header("Access-Control-Allow-Methods", "*") - .header("Access-Control-Allow-Headers", "*") - .header("Access-Control-Allow-Credentials", "*") + .header("Access-Control-Allow-Methods", "GET, POST") + .header("Access-Control-Allow-Headers", CONTENT_TYPE) .statusCode(200); } @Test - public void request2() { + void request2() { given() .when() .get("http://localhost:2001") .then() .assertThat() .header("Access-Control-Allow-Origin", "*") - .header("Access-Control-Allow-Methods", "*") - .header("Access-Control-Allow-Headers", "*") - .header("Access-Control-Allow-Credentials", "*") + .header("Access-Control-Allow-Methods", "GET, POST") + .header("Access-Control-Allow-Headers", CONTENT_TYPE) .statusCode(404); } // @formatter:on