Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wiremock extension for remote servers #1

Merged
merged 1 commit into from May 24, 2023
Merged

Conversation

yyYank
Copy link
Contributor

@yyYank yyYank commented May 24, 2023

Added wiremock extension for remote servers.

Motivation

When using the wiremock Docker image, wiremock is started as a remote server.
The current implementation of extensions in this repository only supports local invocation of wiremock (eg within a test runner). We want to use this extension on our wiremock remote server as well.

What changes did you make?

  • GraphqlBodyMatcher's match method now checks parameters: Parameter (this allows the remote server to communicate with the local client via Parameter)
  • Updated jvmTarget to 1.8. This is for compatibility in the execution environment because the wiremock image assumes jre8 :)
  • Added maven-assembly-plugin. This will create a jar with all dependencies when you run mvn package. The reason for this is that it is convenient to make the remote wiremock server aware
  • I wanted the unit test to pass, so I changed it (mockk<Parameter>)

About what we tested

  • mvn package
  • On docker, include jar in wiremock and start the container
FROM wiremock/wiremock:latest-alpine
COPY ./wiremock-graphql-extension-0.3.0-jar-with-dependencies.jar /var/wiremock/extensions/wiremock-graphql-extension-0.3.0-jar-with-dependencies.jar
CMD ["--verbose", "--enable-stub-cors", "--extensions", "io.github.nilwurtz.GraphqlBodyMatcher"]
  • register GraphqlBodyMatcher in test code
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

fun registerGraphQLWiremock(endpoint: String, token: String, request: Request, responseJson:  String) {
     post(urlPathEqualTo(endPoint))
        .withQueryParam("access_token", matching(token))
        .andMatching("graphql-body-matcher", Parameters.one("expectedQuery", Json.encodeToString(request)))
        .willReturn(
            aResponse()
                 .withStatus(200)
                 .withBody(responseJson)
                 .withHeader("Content-Type", "application/json; charset=utf-8")
     ).let(mock::register)
}
  • run test

powered by Google Translate


[日本語] リモートサーバー用のワイヤーモック拡張機能を追加

モチベーション

wiremockのDockerイメージを使う場合、wiremockはリモートサーバーとして起動します。
現在のこのリポジトリのエクステンション実装はwiremockのローカル起動(例テストランナー内での起動)の場合のみしかサポートしていません。我々はwiremockのリモートサーバーでもこのエクステンションを使用したいです。

どのような変更をあなたは加えました?

  • GraphqlBodyMatcherのmatchメソッドでparameters: Parameter をチェックするようにしました(これにより、リモートサーバーはParameterを介してローカルのクライアントとコミュニケーション出来ます)
  • jvmTargetを1.8にしました。これは、wiremockのimageがjre8を前提としているため実行環境での互換性のためです :)
  • maven-assembly-pluginを追加しました。これにより、mvn package実行時に依存関係を全て含んだjarを作成します。これの理由は、remoteのwiremockサーバーに認識させるのに都合が良いためです
  • ユニットテストがPASSしたかったので、変更をしました(mockk<Parameter>)

テストした内容について

  • maven package
  • docker上で、wiremockにjarを含めてコンテナ起動
FROM wiremock/wiremock:latest-alpine
COPY ./wiremock-graphql-extension-0.3.0-jar-with-dependencies.jar /var/wiremock/extensions/wiremock-graphql-extension-0.3.0-jar-with-dependencies.jar
CMD ["--verbose", "--enable-stub-cors", "--extensions", "io.github.nilwurtz.GraphqlBodyMatcher"]
  • テストコードでGraphqlBodyMatcherをregister
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

fun registerGraphQLWiremock(endpoint: String, token: String, request: Request, responseJson:  String) {
     post(urlPathEqualTo(endPoint))
        .withQueryParam("access_token", matching(token))
        .andMatching("graphql-body-matcher", Parameters.one("expectedQuery", Json.encodeToString(request)))
        .willReturn(
            aResponse()
                 .withStatus(200)
                 .withBody(responseJson)
                 .withHeader("Content-Type", "application/json; charset=utf-8")
     ).let(mock::register)
}
  • テストを実行してPASS

@nilwurtz nilwurtz merged commit 4fe14e9 into wiremock:main May 24, 2023
@nilwurtz
Copy link
Collaborator

Thanks ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants