mirror of https://github.com/knative/func.git
feat: add echo for springboot http template (#1072)
* feat: add echo func * chore: package * docs: update docs * refactor: add space between headers * refactor: regenerate zz file * refactor: clean with pre-commit * refactor: add func_yaml-schema.json file * refactor: rename test and fix white-spaces * refactor: remove .idea files * test: change http test * test: change expected result
This commit is contained in:
parent
80093d66a4
commit
f828b0998c
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Welcome to your new Function project!
|
Welcome to your new Function project!
|
||||||
|
|
||||||
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.uppercase()`, which returns the uppercase of the data passed.
|
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.echo()`, which returns an echo of the data passed via HTTP request.
|
||||||
|
|
||||||
## Local execution
|
## Local execution
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ export URL=$(kn service describe $(basename $PWD) -ourl)
|
||||||
Using `func invoke` command with Path-Based routing:
|
Using `func invoke` command with Path-Based routing:
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
func invoke --target "$URL/uppercase" --data "$(whoami)"
|
func invoke --target "$URL/echo" --data "$(whoami)"
|
||||||
```
|
```
|
||||||
|
|
||||||
If your function class only contains one function, then you can leave out the target path:
|
If your function class only contains one function, then you can leave out the target path:
|
||||||
|
@ -98,7 +98,7 @@ func invoke --data "$(whoami)"
|
||||||
### cURL
|
### cURL
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
curl -v "$URL/uppercase" \
|
curl -v "$URL/echo" \
|
||||||
-H "Content-Type:text/plain" \
|
-H "Content-Type:text/plain" \
|
||||||
-w "\n" \
|
-w "\n" \
|
||||||
-d "$(whoami)"
|
-d "$(whoami)"
|
||||||
|
@ -116,7 +116,7 @@ curl -v "$URL" \
|
||||||
### HTTPie
|
### HTTPie
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
echo "$(whoami)" | http -v "$URL/uppercase"
|
echo "$(whoami)" | http -v "$URL/echo"
|
||||||
```
|
```
|
||||||
|
|
||||||
If your function class only contains one function, then you can leave out the target path:
|
If your function class only contains one function, then you can leave out the target path:
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package functions;
|
package functions;
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.messaging.Message;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class CloudFunctionApplication {
|
public class CloudFunctionApplication {
|
||||||
|
@ -13,8 +15,22 @@ public class CloudFunctionApplication {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Function<String, String> uppercase() {
|
public Function<Message<String>, String> echo() {
|
||||||
return String::toUpperCase;
|
return (inputMessage) -> {
|
||||||
}
|
|
||||||
|
|
||||||
|
var stringBuilder = new StringBuilder();
|
||||||
|
inputMessage.getHeaders()
|
||||||
|
.forEach((key, value) -> {
|
||||||
|
stringBuilder.append(key).append(": ").append(value).append(" ");
|
||||||
|
});
|
||||||
|
|
||||||
|
var payload = inputMessage.getPayload();
|
||||||
|
|
||||||
|
if (!payload.isBlank()) {
|
||||||
|
stringBuilder.append("echo: ").append(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.toString();
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,18 +14,29 @@ import static org.hamcrest.Matchers.*;
|
||||||
|
|
||||||
@SpringBootTest(classes = CloudFunctionApplication.class,
|
@SpringBootTest(classes = CloudFunctionApplication.class,
|
||||||
webEnvironment = WebEnvironment.RANDOM_PORT)
|
webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class UpperCaseFunctionTest {
|
public class EchoCaseFunctionTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestRestTemplate rest;
|
private TestRestTemplate rest;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpperCase() throws Exception {
|
public void testEchoWithBody() throws Exception {
|
||||||
ResponseEntity<String> response = this.rest.exchange(
|
ResponseEntity<String> response = this.rest.exchange(
|
||||||
RequestEntity.post(new URI("/uppercase"))
|
RequestEntity.post(new URI("/echo"))
|
||||||
.body("hello"), String.class);
|
.body("hello"), String.class);
|
||||||
assertThat(response.getStatusCode()
|
assertThat(response.getStatusCode()
|
||||||
.value(), equalTo(200));
|
.value(), equalTo(200));
|
||||||
assertThat(response.getBody(), equalTo("HELLO"));
|
assertThat(response.getBody(), containsString("echo: hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEchoWithoutBody() throws Exception {
|
||||||
|
ResponseEntity<String> response = this.rest.exchange(
|
||||||
|
RequestEntity.get(new URI("/echo/"))
|
||||||
|
.header("custom-header", "custom-value")
|
||||||
|
.build(), String.class);
|
||||||
|
assertThat(response.getStatusCode()
|
||||||
|
.value(), equalTo(200));
|
||||||
|
assertThat(response.getBody(), containsString("custom-header: custom-value"));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -86,10 +86,10 @@ var defaultFunctionsHttpValidators = []FunctionHttpResponsivenessValidator{
|
||||||
expects: `{"message":"hello"}`,
|
expects: `{"message":"hello"}`,
|
||||||
},
|
},
|
||||||
{runtime: "springboot",
|
{runtime: "springboot",
|
||||||
targetUrl: "%s/uppercase",
|
targetUrl: "%s/echo",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
bodyData: "message=hello",
|
bodyData: "message=hello",
|
||||||
expects: `MESSAGE=HELLO`,
|
expects: `message=hello`,
|
||||||
},
|
},
|
||||||
{runtime: "typescript",
|
{runtime: "typescript",
|
||||||
targetUrl: "%s",
|
targetUrl: "%s",
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue