### WireMock gRPC Extension Setup Source: https://github.com/wiremock/go-wiremock/blob/main/README.md This section outlines the prerequisites for mocking gRPC services with WireMock. It includes commands for generating a protobuf descriptor file and downloading the necessary WireMock gRPC extension JAR file. ```bash # create descriptor file for wiremock protoc --include_imports --descriptor_set_out ./proto/services_nebius.dsc ./proto/greeting_service.proto # download the wiremock grpc extension curl -L https://github-registry-files.githubusercontent.com/694300421/0a0c0b80-089b-11f0-8fd1-f7e53ce5fce0?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20250609%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250609T202456Z&X-Amz-Expires=300&X-Amz-Signature=85f6bc85c1b86f2416ceb5c5031fde515f9f3003d612e2e375801e3dadde0d1c&X-Amz-SignedHeaders=host&response-content-disposition=filename%3Dwiremock-grpc-extension-standalone-0.10.0.jar&response-content-type=application%2Foctet-stream -o ./extensions/wiremock-grpc-0.10.0.jar # run wiremock with grpc extension docker run -it --rm -p 8080:8080 -v $(pwd)/extensions:/var/wiremock/extensions -v $(pwd)/proto:/home/wiremock/grpc wiremock/wiremock --verbose ``` -------------------------------- ### Record Stubs with Wiremock Go Client Source: https://github.com/wiremock/go-wiremock/blob/main/README.md Demonstrates how to start and stop stub recording using the wiremock client. It's recommended to defer the stop recording call to ensure it's executed. ```go wiremockClient.StartRecording("https://my.saas.endpoint.com") defer wiremockClient.StopRecording() //… do some requests to Wiremock //… do some assertions using your Saas' SDK ``` -------------------------------- ### Go WireMock gRPC Stubbing Example Source: https://github.com/wiremock/go-wiremock/blob/main/README.md This Go code demonstrates how to mock gRPC services using the go-wiremock library. It shows how to create a gRPC service client, stub specific methods, match request messages, and define responses, errors, or faults for those methods. ```go package main import ( "google.golang.org/grpc/codes" "github.com/wiremock/go-wiremock" wiremockGRPC "github.com/wiremock/go-wiremock/grpc" proto "github.com/wiremock/go-wiremock/grpc/testdata" ) func main() { wiremockClient := wiremock.NewClient("http://0.0.0.0:8080") service := wiremockGRPC.NewService("com.example.greeting.v1.GreetingService", wiremockClient) service.StubFor( wiremockGRPC.Method("Greet"). WithRequestMessage(wiremockGRPC.EqualToMessage(&proto.GreetingRequest{ Name: "Tom", })). WillReturn(wiremockGRPC.Message(&proto.GreetingResponse{ Greeting: "Hello, Tom!", }))) service.StubFor( wiremockGRPC.Method("Greet"). WithRequestMessage(wiremockGRPC.EqualToMessage(&proto.GreetingRequest{ Name: "Robert", })). WillReturn(wiremockGRPC.Error(codes.NotFound, "Robert not found"))) service.StubFor( wiremockGRPC.Method("Greet"). WithRequestMessage(wiremockGRPC.EqualToMessage(&proto.GreetingRequest{ Name: "Richard", })). WillReturn(wiremockGRPC.Fault(wiremock.FaultConnectionResetByPeer))) } ``` -------------------------------- ### Go WireMock HTTP Stubbing with Scenarios Source: https://github.com/wiremock/go-wiremock/blob/main/README.md Demonstrates using the go-wiremock client to create HTTP stubs. It covers stubbing POST requests with query parameters, JSON bodies, headers, and bearer tokens, as well as defining responses with JSON bodies and status codes. It also includes an example of using scenarios to manage state transitions. ```go package main import ( "net/http" "testing" "github.com/wiremock/go-wiremock" ) func TestSome(t *testing.T) { wiremockClient := wiremock.NewClient("http://0.0.0.0:8080") defer wiremockClient.Reset() // stubbing POST http://0.0.0.0:8080/example wiremockClient.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/example")). WithQueryParam("firstName", wiremock.EqualTo("John")), WithQueryParam("lastName", wiremock.NotMatching("Black")), WithBodyPattern(wiremock.EqualToJson(`{"meta": "information"}`)), WithHeader("x-session", wiremock.Matching("^\\S+fingerprint\\S+$")), WithBearerToken(wiremock.StartsWith("token")), WillReturnResponse( wiremock.NewResponse(). WithJSONBody(map[string]interface{}{ "code": 400, "detail": "detail", }). WithHeader("Content-Type", "application/json"). WithStatus(http.StatusBadRequest), ). AtPriority(1)) // scenario defer wiremockClient.ResetAllScenarios() wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/status")), WillReturnResponse( wiremock.NewResponse(). WithJSONBody(map[string]interface{}{ "status": nil, }). WithHeader("Content-Type", "application/json"). WithStatus(http.StatusOK), ). InScenario("Set status"). WhenScenarioStateIs(wiremock.ScenarioStateStarted)) wiremockClient.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/state")), WithBodyPattern(wiremock.EqualToJson(`{"status": "started"}`)), InScenario("Set status"). WillSetStateTo("Status started")) statusStub := wiremock.Get(wiremock.URLPathEqualTo("/status")), WillReturnResponse( wiremock.NewResponse(). WithJSONBody(map[string]interface{}{ "status": "started", }). WithHeader("Content-Type", "application/json"). WithStatus(http.StatusOK), ). InScenario("Set status"). WhenScenarioStateIs("Status started") wiremockClient.StubFor(statusStub) //testing code... verifyResult, _ := wiremockClient.Verify(statusStub.Request(), 1) if !verifyResult { //... } wiremockClient.DeleteStub(statusStub) } ``` -------------------------------- ### Launch Standalone WireMock Docker Instance Source: https://github.com/wiremock/go-wiremock/blob/main/README.md This snippet shows the command to launch a standalone WireMock Docker container. It maps port 8080 from the container to the host, making WireMock accessible for client connections. ```shell docker run -it --rm -p 8080:8080 wiremock/wiremock ``` -------------------------------- ### Wiremock Go Authentication Stubbing Source: https://github.com/wiremock/go-wiremock/blob/main/README.md Illustrates the use of various authentication schemes supported by the Go Wiremock library, including Basic, Bearer, API Token, and Digest authentication. These methods simplify setting the 'Authorization' header. ```APIDOC Wiremock Go Authentication Methods: Wiremock provides methods to simplify setting common authentication headers. 1. **Basic Authentication** - Method: `WithBasicAuth(username, password)` - Description: Configures the stub to expect Basic Authentication. - Equivalent to: `WithHeader("Authorization", wiremock.EqualTo("Basic dXNlcm5hbWU6cGFzc3dvcmQ="))` - Example: ```go wiremock.Get(wiremock.URLPathEqualTo("/basic")). WithBasicAuth("username", "password"). WillReturnResponse(wiremock.NewResponse().WithStatus(http.StatusOK)) ``` 2. **Bearer Token Authentication** - Method: `WithBearerToken(tokenMatcher)` - Description: Configures the stub to expect a Bearer token. - `tokenMatcher`: A `wiremock.Matcher` for the token value. - Equivalent to: `WithHeader("Authorization", wiremock.Matching("^Bearer \S+abc\S+$"))` - Example: ```go wiremock.Get(wiremock.URLPathEqualTo("/bearer")). WithBearerToken(wiremock.Matching("^\\S+abc\\S+$")). WillReturnResponse(wiremock.NewResponse().WithStatus(http.StatusOK)) ``` 3. **API Token Authentication** - Method: `WithAuthToken(tokenMatcher)` - Description: Configures the stub to expect an API token. - `tokenMatcher`: A `wiremock.Matcher` for the token value. - Equivalent to: `WithHeader("Authorization", wiremock.StartsWith("Token myToken123"))` - Example: ```go wiremock.Get(wiremock.URLPathEqualTo("/token")). WithAuthToken(wiremock.StartsWith("myToken123")). WillReturnResponse(wiremock.NewResponse().WithStatus(http.StatusOK)) ``` 4. **Digest Access Authentication** - Method: `WithDigestAuth(challengeMatcher)` - Description: Configures the stub to expect Digest Access Authentication. - `challengeMatcher`: A `wiremock.Matcher` for the digest challenge. - Equivalent to: `WithHeader("Authorization", wiremock.StartsWith("Digest ").And(Contains("realm")))` - Example: ```go wiremock.Get(wiremock.URLPathEqualTo("/digest")). WithDigestAuth(wiremock.Contains("realm")). WillReturnResponse(wiremock.NewResponse().WithStatus(http.StatusOK)) ``` These methods are equivalent to manually specifying the "Authorization" header with the appropriate prefix and value. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.