Skip to content

Commit

Permalink
Fixed example (#1419)
Browse files Browse the repository at this point in the history
* Fixed example

* Fixed example
  • Loading branch information
predic8 authored Dec 17, 2024
1 parent 0c5eb2f commit 19a9855
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 91 deletions.
139 changes: 64 additions & 75 deletions distribution/examples/javascript/README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 2 additions & 1 deletion distribution/examples/javascript/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<api port="2000">
<request>
<javascript>
console.log("Query parameters: " + params);
// Return Json as Map
({ id:7, city:"Berlin" })
({ id: params.get('id'), city: params.get('city') })
</javascript>
</request>
<return contentType="application/json"/><!-- Return response to client -->
Expand Down
2 changes: 1 addition & 1 deletion distribution/examples/validation/xml/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<api port="2001">
<template contentType="application/xml">
&gt;amount>100&gt;/amount>
&lt;amount>100&lt;/amount>
</template>
<return/>
</api>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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()
Expand Down

0 comments on commit 19a9855

Please sign in to comment.