-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial code for Flowable Mock Web Server
- Loading branch information
0 parents
commit 3d3f62b
Showing
17 changed files
with
2,201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: CI | ||
|
||
on: [push, pull_request] | ||
|
||
env: | ||
MAVEN_ARGS: -V -B --no-transfer-progress -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 | ||
|
||
jobs: | ||
test_jdk: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
java: [17, 21] | ||
name: 'Linux JDK ${{ matrix.java }}' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: 'Checkout' | ||
uses: actions/checkout@v4 | ||
- name: 'Set up JDK' | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'zulu' | ||
java-version: ${{ matrix.java }} | ||
- name: 'Test' | ||
run: ./mvnw ${MAVEN_ARGS} install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
name: Release | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
description: 'Release version' | ||
required: true | ||
next: | ||
description: 'Next version' | ||
required: false | ||
|
||
jobs: | ||
release: | ||
# This job has been inspired by the moditect release (https://github.com/moditect/moditect/blob/main/.github/workflows/release.yml) | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Java | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: 17 | ||
distribution: 'zulu' | ||
cache: maven | ||
|
||
- name: Set release version | ||
id: version | ||
run: | | ||
RELEASE_VERSION=${{ github.event.inputs.version }} | ||
NEXT_VERSION=${{ github.event.inputs.next }} | ||
PLAIN_VERSION=`echo ${RELEASE_VERSION} | awk 'match($0, /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)/) { print substr($0, RSTART, RLENGTH); }'` | ||
COMPUTED_NEXT_VERSION="${PLAIN_VERSION}-SNAPSHOT" | ||
if [ -z $NEXT_VERSION ] | ||
then | ||
NEXT_VERSION=$COMPUTED_NEXT_VERSION | ||
fi | ||
./mvnw -ntp -B versions:set versions:commit -DnewVersion=$RELEASE_VERSION -DgenerateBackupPoms=false | ||
git config --global user.email "${{ vars.GH_BOT_EMAIL }}" | ||
git config --global user.name "GitHub Action" | ||
git commit -a -m "Releasing version $RELEASE_VERSION" | ||
git push | ||
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV | ||
echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_ENV | ||
echo "PLAIN_VERSION=$PLAIN_VERSION" >> $GITHUB_ENV | ||
- name: Stage | ||
run: | | ||
export GPG_TTY=$(tty) | ||
./mvnw -ntp -B --file pom.xml \ | ||
-Dmaven.site.skip=true -Drelease=true -Ppublication,stage | ||
- name: Release | ||
env: | ||
JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
JRELEASER_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} | ||
JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }} | ||
JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_PRIVATE_KEY }} | ||
JRELEASER_NEXUS2_MAVEN_CENTRAL_USERNAME: ${{ secrets.SONATYPE_USERNAME }} | ||
JRELEASER_NEXUS2_MAVEN_CENTRAL_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} | ||
run: | | ||
./mvnw -ntp -B --file pom.xml -Pjreleaser jreleaser:release | ||
- name: JReleaser output | ||
if: always() | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: jreleaser-release | ||
path: | | ||
parent/target/jreleaser/trace.log | ||
parent/target/jreleaser/output.properties | ||
- name: Reset NEXT_RELEASE_CHANGELOG.md | ||
run: echo -e "\n" > NEXT_RELEASE_CHANGELOG.md | ||
|
||
- name: Set next version | ||
run: | | ||
./mvnw -ntp -B versions:set versions:commit -DnewVersion=${{ env.NEXT_VERSION }} -DgenerateBackupPoms=false | ||
sed -i -e "s@project.build.outputTimestamp>.*</project.build.outputTimestamp@project.build.outputTimestamp>\${git.commit.author.time}</project.build.outputTimestamp@g" parent/pom.xml | ||
git config --global user.email "${{ vars.GH_BOT_EMAIL }}" | ||
git config --global user.name "GitHub Action" | ||
git commit -a -m "Next version ${{ env.NEXT_VERSION }}" | ||
git push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Eclipse | ||
.metadata | ||
.recommenders | ||
.classpath | ||
.project | ||
.settings | ||
.factorypath | ||
.checkstyle | ||
.externalToolBuilders | ||
|
||
# IntelliJ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea | ||
|
||
# Netbeans | ||
nb-configuration.xml | ||
|
||
# Build | ||
/**/target/ | ||
test-output | ||
|
||
# Misc. | ||
.DS_Store | ||
checkstyle.cache | ||
.flattened-pom.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
wrapperVersion=3.3.2 | ||
distributionType=only-script | ||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
First release of Flowable Mock Web Server, a Web Server for testing HTTP clients |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
# Flowable Mock Web Server | ||
|
||
Web Server for testing HTTP clients | ||
|
||
### Motivation | ||
|
||
This library is inspired by the [OkHttp MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver). | ||
It allows to specify which the responses that the server should return for each request and to verify the requests that the server received. | ||
|
||
It uses a light weight webserver [Microhttp](https://github.com/ebarlas/microhttp) to handle the requests. | ||
|
||
### Example | ||
|
||
Here is a complete example | ||
|
||
```java | ||
class ExampleTest { | ||
|
||
protected MockWebServer server; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
server = new MockWebServer(); | ||
server.start(); | ||
server.failFast(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
void queryPets() throws IOException, InterruptedException { | ||
HttpClient client = HttpClient.newHttpClient(); | ||
server.enqueue(MockResponse.newBuilder().jsonBody(""" | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "Loki", | ||
"type": "dog" | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Garfield", | ||
"type": "cat" | ||
} | ||
] | ||
""")); | ||
server.enqueue(MockResponse.newBuilder().jsonBody(""" | ||
{ | ||
"message": "Not Found" | ||
} | ||
""").notFound()); | ||
|
||
String petsUrl = server.url("/pets"); | ||
HttpResponse<String> response = client.send(HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(petsUrl)) | ||
.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); | ||
|
||
assertThat(response.statusCode()).isEqualTo(200); | ||
assertThat(response.headers().firstValue("Content-Type")) | ||
.hasValue("application/json"); | ||
assertThat(response.body()) | ||
.isEqualTo(""" | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "Loki", | ||
"type": "dog" | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Garfield", | ||
"type": "cat" | ||
} | ||
] | ||
"""); | ||
|
||
response = client.send(HttpRequest.newBuilder() | ||
.PUT(HttpRequest.BodyPublishers.ofString(""" | ||
{ | ||
"name": "Loki Updated", | ||
"type": "dog" | ||
} | ||
""")) | ||
.uri(URI.create(petsUrl + "/1")) | ||
.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); | ||
|
||
assertThat(response.statusCode()).isEqualTo(404); | ||
assertThat(response.body()).isEqualTo(""" | ||
{ | ||
"message": "Not Found" | ||
} | ||
"""); | ||
|
||
RecordedRequest recordedRequest = server.takeRequest(); | ||
assertThat(recordedRequest.path()).isEqualTo("/pets"); | ||
assertThat(recordedRequest.method()).isEqualTo("GET"); | ||
assertThat(recordedRequest.requestUrl().queryParameters()).isEmpty(); | ||
|
||
recordedRequest = server.takeRequest(); | ||
assertThat(recordedRequest.path()).isEqualTo("/pets/1"); | ||
assertThat(recordedRequest.method()).isEqualTo("PUT"); | ||
assertThat(recordedRequest.requestUrl().queryParameters()).isEmpty(); | ||
assertThat(recordedRequest.body().asString()) | ||
.isEqualTo(""" | ||
{ | ||
"name": "Loki Updated", | ||
"type": "dog" | ||
} | ||
"""); | ||
} | ||
} | ||
``` | ||
|
||
### API | ||
|
||
#### MockResponse | ||
|
||
MockResponse is used to specify the response that the server should return for the next request. | ||
By default, an empty response with status code 200 is returned. | ||
They are created using `MockResponseBuilder` and you can customize it using a fluent API. | ||
|
||
```java | ||
MockResponse response = MockResponse.newBuilder() | ||
.jsonBody(""" | ||
{ | ||
"id": 1, | ||
"name": "Loki", | ||
"type": "dog" | ||
} | ||
""") | ||
.header("Cache-Control", "no-cache") | ||
.build(); | ||
``` | ||
|
||
Responses can also be throttled, to simulate a slow network. | ||
|
||
```java | ||
MockResponse response = MockResponse.newBuilder() | ||
.jsonBody("{}") | ||
.bodyDelay(Duration.ofSeconds(2)) | ||
.build(); | ||
``` | ||
|
||
The status of the response can be configured via `MockResponse.status` / `MockResponse.statusCode`. | ||
It can directly be set using an integer value of the status or through some of the known statuses in `MockHttpStatus` | ||
|
||
#### MockWebServer | ||
|
||
The main entry point where response can be queued and requests can be retrieved to be verified. | ||
It can be started on a random port (using `start()`) or on a specific port (`start(31313)`). | ||
The URL of a specific path on the server can be retrieved using `url("/path")`. | ||
|
||
```java | ||
MockWebServer server = new MockWebServer(); | ||
server.start(); | ||
int port = server.getPort(); | ||
String petsUrl = server.url("/pets"); | ||
``` | ||
|
||
Responses can be enqueued directly using the `MockResponseBuilder` or `MockResponse` | ||
|
||
e.g. | ||
|
||
```java | ||
server.enqueue(MockResponse.newBuilder() | ||
.jsonBody(""" | ||
{ | ||
"message": "Not Found" | ||
} | ||
""") | ||
.notFound()); | ||
``` | ||
|
||
```java | ||
MockResponse response = MockResponse.newBuilder() | ||
.jsonBody(""" | ||
{ | ||
"message": "Not Found" | ||
} | ||
""") | ||
.notFound() | ||
.build(); | ||
server.enqueue(response); | ||
``` | ||
Once you are done with the server it can be shutdown via `server.shutdown()`. | ||
|
||
It is also possible to provide a custom (non queue based) response provider. | ||
|
||
```java | ||
Function<RecordedRequest, MockResponse> customResponseProvider = request -> switch (request.path()) { | ||
case "/login/auth/": | ||
return MockResponse.newBuilder().build(); | ||
case "/check/version/": | ||
return MockResponse.newBuilder().body("version=9"); | ||
case "/pets/1": | ||
return MockResponse.newBuilder().jsonBody(""" | ||
{ | ||
"id": 1, | ||
"name": "Loki", | ||
"type": "dog" | ||
} | ||
"""); | ||
default: | ||
return MockResponse.newBuilder().notFound().build(); | ||
}; | ||
``` | ||
|
||
#### RecordedRequest | ||
|
||
RecordedRequest is used to verify the requests that the server received. | ||
They can be retrieved using `server.takeRequest()` (which returns immediately) or `server.takeRequest(Duration.ofSeconds(5))` (which will wait 5 seconds for a request to be available before returning `null`). | ||
|
||
|
||
```java | ||
RecordedRequest recordedRequest = server.takeRequest(); | ||
assertThat(recordedRequest.path()).isEqualTo("/pets/1"); | ||
assertThat(recordedRequest.method()).isEqualTo("PUT"); | ||
assertThat(recordedRequest.header("Content-Type")).isEqualTo("application/json"); | ||
assertThat(recordedRequest.headerValues("Content-Type")).containsExactly("application/json"); | ||
assertThat(recordedRequest.body().asString()).isEqualTo(""" | ||
{ | ||
"name": "Loki Updated", | ||
"type": "dog" | ||
} | ||
"""); | ||
``` |
Oops, something went wrong.