From 19a9855b340d7ed02d46013f3833557a0a6bdf1b Mon Sep 17 00:00:00 2001 From: Thomas Bayer Date: Tue, 17 Dec 2024 09:25:56 +0100 Subject: [PATCH] Fixed example (#1419) * Fixed example * Fixed example --- distribution/examples/javascript/README.md | 139 ++++++++---------- distribution/examples/javascript/proxies.xml | 3 +- .../examples/validation/xml/proxies.xml | 2 +- .../examples/tests/JavascriptTest.java | 25 ++-- .../tests/validation/XMLValidationTest.java | 7 +- 5 files changed, 85 insertions(+), 91 deletions(-) diff --git a/distribution/examples/javascript/README.md b/distribution/examples/javascript/README.md index 1bd0a644c3..e2a7e96730 100644 --- a/distribution/examples/javascript/README.md +++ b/distribution/examples/javascript/README.md @@ -1,80 +1,69 @@ # Access and Manipulate Messages with Javascript - Example -Javascript is a powerful tool to manipulate messages and to change the behaviour of an API. Using the `javascript` plugin you can use Javascript within API definitions. - -**Hint:** These examples require Membrane version 5.1.0 or newer. - -## Running the example - -1. Take a look at [proxies.xml](proxies.xml). There you'll find the APIs with the `javascript`plugin. -2. Open a commandline session or a terminal. -3. Run `service-proxy.bat` or `./service-proxy.sh` in this folder -4. Open a second terminal and run the commands: - - **Create JSON with Javascript:** - - ```json - curl localhost:2000 - {"id":7,"city":"Berlin"} - ``` - - **Transform JSON to JSON:** - - Have a look at the [order.json](order.json) file, then send it to the API to transform the JSON into a different JSON format: - - ```json - curl -d @order.json -H "Content-Type: application/json" http://localhost:2010 - { - "id": 731, - "date": "2023-04-07", - "client": 17, - "total": 38.35, - "positions": [ - { - "pieces": 5, - "price": 5.9, - "article": "Oolong" - }, - { - "pieces": 2, - "price": 2.95, - "article": "Assam" - }, - { - "pieces": 1, - "price": 2.95, - "article": "Darjeeling" - } - ] - } - ``` - - **Access HTTP Headers and a Spring Bean:** - - ```shell - ❯ curl http://localhost:2020 -v - > GET / HTTP/1.1 - > Host: localhost:2020 - - < HTTP/1.1 200 Ok - < Content-Length: 21 - < X-Groovy: 42 - - Greatings from Spring - ``` - - Then take a look at the output of the `service-proxy.sh/bat` script. You should see the output from the script, printing the request header fields. - - - ``` - Request headers: - Host: localhost:2000 - User-Agent: curl/7.79.1 - Accept: */* - X-Forwarded-For: 127.0.0.1 - X-Forwarded-Proto: http - X-Forwarded-Host: localhost:2000 - ``` +JavaScript is a powerful tool for manipulating messages and customizing the behavior of an API. With the `javascript` plugin, you can embed JavaScript directly into API definitions to achieve flexible transformations and logic. + +**Note:** These examples require Membrane version 5.1.0 or newer. + +## Running the Example + +1. Review the [proxies.xml](proxies.xml) file to see how the APIs are configured with the `javascript` plugin. +2. Open a command-line session or terminal. +3. Run `service-proxy.bat` (Windows) or `./service-proxy.sh` (Linux/Mac) in this folder. +4. Open a second terminal and execute the following commands: + +### Create JSON with JavaScript + +The query parameter from: + +```bash +curl "http://localhost:2000?id=7&city=Berlin" +``` + +will be filled in a JSON document. + +```json +{"id":"7","city":"Berlin"} +``` + +See API at port 2000 in 'proxies.xml' for details. + +### Transform JSON to JSON + +Examine the [order.json](order.json) file to understand the input format. Then, send it to the API to transform it into a different JSON structure: + +```bash +cat order.json + +curl -d @order.json -H "Content-Type: application/json" http://localhost:2010 +``` + +### Access HTTP Headers and a Spring Bean + +Send a request to the API and inspect the response: + +```bash +curl http://localhost:2020 -v +> GET / HTTP/1.1 +> Host: localhost:2020 + +< HTTP/1.1 200 Ok +< Content-Length: 21 +< X-Javascript: 42 + +Greatings from Javascript +``` + +The response should include an `X-Javascript` header. Additionally, the console output of `service-proxy.sh` or `service-proxy.bat` will display the request header fields logged by the script: + +``` +Request headers: +Host: localhost:2000 +User-Agent: curl/7.79.1 +Accept: */* +X-Forwarded-For: 127.0.0.1 +X-Forwarded-Proto: http +X-Forwarded-Host: localhost:2000 +``` --- See: diff --git a/distribution/examples/javascript/proxies.xml b/distribution/examples/javascript/proxies.xml index 9d64652451..cd11d380cf 100644 --- a/distribution/examples/javascript/proxies.xml +++ b/distribution/examples/javascript/proxies.xml @@ -14,8 +14,9 @@ + console.log("Query parameters: " + params); // Return Json as Map - ({ id:7, city:"Berlin" }) + ({ id: params.get('id'), city: params.get('city') }) diff --git a/distribution/examples/validation/xml/proxies.xml b/distribution/examples/validation/xml/proxies.xml index 6829a099e3..c774bf04a9 100644 --- a/distribution/examples/validation/xml/proxies.xml +++ b/distribution/examples/validation/xml/proxies.xml @@ -18,7 +18,7 @@ diff --git a/distribution/src/test/java/com/predic8/membrane/examples/tests/JavascriptTest.java b/distribution/src/test/java/com/predic8/membrane/examples/tests/JavascriptTest.java index 4d22a15a0b..8862bfee31 100644 --- a/distribution/src/test/java/com/predic8/membrane/examples/tests/JavascriptTest.java +++ b/distribution/src/test/java/com/predic8/membrane/examples/tests/JavascriptTest.java @@ -15,7 +15,6 @@ package com.predic8.membrane.examples.tests; import com.predic8.membrane.examples.util.*; -import io.restassured.filter.log.*; import io.restassured.response.*; import org.junit.jupiter.api.*; import org.skyscreamer.jsonassert.*; @@ -49,11 +48,12 @@ void startMembrane() throws IOException, InterruptedException { @Test public void returnJsonAsMap() { - Response r = get("http://localhost:2000"); - - get("http://localhost:2000").then().assertThat() - .body("id",equalTo(7)) - .body("city",equalTo("Berlin")); + // @formatter:off + Response r = get("http://localhost:2000?id=314&city=Boston"); + r.then().assertThat() + .body("id",equalTo("314")) + .body("city",equalTo("Boston")); + // @formatter:on } @Test @@ -117,12 +117,15 @@ public void transformJson() { @Test public void headers() { + + // @formatter:off given() - .header("X-Foo",3141) - .get("http://localhost:2020") - .then().assertThat() - .statusCode(200) - .header("X-Javascript", "42"); + .header("X-Foo",3141) + .get("http://localhost:2020") + .then().assertThat() + .statusCode(200) + .header("X-Javascript", "42"); + // @formatter:on assertContains("Request headers:",logger.toString()); assertContains("X-Foo",logger.toString()); diff --git a/distribution/src/test/java/com/predic8/membrane/examples/tests/validation/XMLValidationTest.java b/distribution/src/test/java/com/predic8/membrane/examples/tests/validation/XMLValidationTest.java index b7bd39d92e..bf2d6a2808 100644 --- a/distribution/src/test/java/com/predic8/membrane/examples/tests/validation/XMLValidationTest.java +++ b/distribution/src/test/java/com/predic8/membrane/examples/tests/validation/XMLValidationTest.java @@ -15,6 +15,7 @@ package com.predic8.membrane.examples.tests.validation; import com.predic8.membrane.examples.util.*; +import io.restassured.response.*; import org.junit.jupiter.api.*; import static io.restassured.RestAssured.*; @@ -31,10 +32,10 @@ protected String getExampleDirName() { public void test() throws Exception { try(Process2 ignored = startServiceProxyScript()) { // @formatter:off - given() + Response r = given() .body(readFileFromBaseDir("year.xml")) - .post(LOCALHOST_2000) - .then() + .post(LOCALHOST_2000); + r.then() .statusCode(200); given()