Skip to content

Unable to initialize BodyMatcher when using Go client for Smocker API #316

@suzuito

Description

@suzuito

Hi, thank you for your great work on Smocker!

I'm currently using a Go client to send requests to the Smocker API (POST /mocks) and encountered a problem: it's not possible to properly initialize the BodyMatcher struct due to the lack of constructor functions.

Here’s an example of what I'm trying to do:

package main

import (
	"bytes"
	"encoding/json"
	"net/http"

	"github.com/smocker-dev/smocker/server/types"
)

func main() {
	mocks := types.Mocks{
		{
			Request: types.MockRequest{
				Method: types.StringMatcher{Matcher: "ShouldEqual", Value: "POST"},
				Path:   types.StringMatcher{Matcher: "ShouldEqual", Value: "/api/hoge"},
				Headers: types.MultiMapMatcher{
					"Host": {{Matcher: "ShouldEqual", Value: "www.example.com"}},
				},
				Body: &types.BodyMatcher{},
			},
			Response: &types.MockResponse{
				Status: http.StatusOK,
				Headers: types.MapStringSlice{
					"Content-Type": types.StringSlice{"application/json"},
				},
				Body: `{"message":"ok"}`,
			},
		},
	}

	bodyBytes, err := json.Marshal(mocks)
	if err != nil {
		panic(err)
	}

	res, err := http.Post(http.MethodPost, "http://localhost:8081/mocks", bytes.NewReader(bodyBytes))
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions