Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
wiremockAdminURN = "__admin"
wiremockAdminMappingsURN = "__admin/mappings"
wiremockAdminFindURN = "__admin/requests/find"
)

// A Client implements requests to the wiremock server
Expand Down Expand Up @@ -107,3 +108,36 @@ func (c *Client) ResetAllScenarios() error {

return nil
}

// FindFor finds requests that were made for a StubRule
func (c *Client) FindRequestsFor(stubRule *StubRule) (map[string]interface{}, error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the sake of keeping this PR small returning a map[string]interface{} else we can work on modelling out the expected response as a struct

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we had a structure, at least requests []map[string]interface{}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok will address this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we only use the request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure let me try to refactor that

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@walkerus the main reasoning to re-use StubRule for this request was because wire mock uses the similar structure for find request patterns. To quote the docs

The criteria part in the parameter to postRequestedFor() uses the same builder as for stubbing, so all of the same predicates are available

But if you feel strongly about using request alone then we have to create builders for request just like StubRule
So for every function that returns a StubRule like the following

func Get(urlMatchingPair URLMatcher) *StubRule {
	return NewStubRule(http.MethodGet, urlMatchingPair)
}

we have to create a corresponding function like below

func Get(urlMatchingPair URLMatcher) *request {
	return NewRequest(http.MethodGet, urlMatchingPair)
}

Let me know your thoughts around this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh or @walkerus did you mean we could build the StubRule as is but change the api alone to use request

func (c *Client) FindRequestsFor(request *request) (map[string]interface{}, error) {

but then the users need to know that they must use StubRule to build the request but when invoking the function they would need to know that they have to extract the request from the StubRule ??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the multiple comments or if we could build a new set of builder for request and re-use them in StubRule like

func NewStubRule(method string, urlMatcher URLMatcher) *StubRule {
	return &StubRule{
		request: NewFindRequest(method, urlMatcher),
		response: response{
			status: http.StatusOK,
		},
	}
}

func NewFindRequest(method string, urlMatcher URLMatcher) request {
	return request{
			urlMatcher: urlMatcher,
			method:     method,
		}
}

// WithQueryParam adds query param and returns *StubRule
func (s *StubRule) WithQueryParam(param string, matcher ParamMatcherInterface) *StubRule {
	s.request.WithQueryParam(param, matcher)
	return s
}

func (r *request) WithQueryParam(param string, matcher ParamMatcherInterface) *request {
	if r.queryParams == nil {
		r.queryParams = map[string]ParamMatcherInterface{}
	}

	r.queryParams[param] = matcher
	return r
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice.
We can make the request importable structure (request -> Request) to be used separately from StubRule.
The request stubbing rules can already be implemented in the YAML file and someone can check if a specific request was received without initiating StubRule

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @walkerus will try to make changes accordingly

//to find requests matching a stub, we need only the request portion of the stub
requestBody, err := json.Marshal(&stubRule.request)
if err != nil {
return nil, fmt.Errorf("build stub request error: %s", err.Error())
}

res, err := http.Post(fmt.Sprintf("%s/%s", c.url, wiremockAdminFindURN), "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return nil, fmt.Errorf("stub request error: %s", err.Error())
}
defer res.Body.Close()

bodyBytes, err := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
if err != nil {
return nil, fmt.Errorf("read response error: %s", err.Error())
}

return nil, fmt.Errorf("bad response status: %d, response: %s", res.StatusCode, string(bodyBytes))
}

var response map[string]interface{}
err = json.Unmarshal(bodyBytes, &response)

if err != nil {
return nil, fmt.Errorf("error: %s unmarshalling response: %s", err, response)
}

return response, nil
}
53 changes: 32 additions & 21 deletions stub_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,46 +166,57 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
jsonStubRule.Response.Body = s.response.body
jsonStubRule.Response.Headers = s.response.headers
jsonStubRule.Response.Status = s.response.status
jsonStubRule.Request = map[string]interface{}{
"method": s.request.method,
string(s.request.urlMatcher.Strategy()): s.request.urlMatcher.Value(),
jsonStubRule.Request = mapFrom(&s.request)
return json.Marshal(jsonStubRule)
}

//MarshalJSON makes json body for request to find requests
//adding a separate MarshalJSON method for the request object
//as it is required to convert the request to JSON
func (r *request) MarshalJSON() ([]byte, error) {
return json.Marshal(mapFrom(r))
}

func mapFrom(r *request) map[string]interface{} {
req := map[string]interface{}{
"method": r.method,
string(r.urlMatcher.Strategy()): r.urlMatcher.Value(),
}
if len(s.request.bodyPatterns) > 0 {
bodyPatterns := make([]map[ParamMatchingStrategy]string, len(s.request.bodyPatterns))
for i, bodyPattern := range s.request.bodyPatterns {
if len(r.bodyPatterns) > 0 {
bodyPatterns := make([]map[ParamMatchingStrategy]string, len(r.bodyPatterns))
for i, bodyPattern := range r.bodyPatterns {
bodyPatterns[i] = map[ParamMatchingStrategy]string{
bodyPattern.Strategy(): bodyPattern.Value(),
}
}
jsonStubRule.Request["bodyPatterns"] = bodyPatterns
req["bodyPatterns"] = bodyPatterns
}
if len(s.request.headers) > 0 {
headers := make(map[string]map[ParamMatchingStrategy]string, len(s.request.bodyPatterns))
for key, header := range s.request.headers {
if len(r.headers) > 0 {
headers := make(map[string]map[ParamMatchingStrategy]string, len(r.bodyPatterns))
for key, header := range r.headers {
headers[key] = map[ParamMatchingStrategy]string{
header.Strategy(): header.Value(),
}
}
jsonStubRule.Request["headers"] = headers
req["headers"] = headers
}
if len(s.request.cookies) > 0 {
cookies := make(map[string]map[ParamMatchingStrategy]string, len(s.request.cookies))
for key, cookie := range s.request.cookies {
if len(r.cookies) > 0 {
cookies := make(map[string]map[ParamMatchingStrategy]string, len(r.cookies))
for key, cookie := range r.cookies {
cookies[key] = map[ParamMatchingStrategy]string{
cookie.Strategy(): cookie.Value(),
}
}
jsonStubRule.Request["cookies"] = cookies
req["cookies"] = cookies
}
if len(s.request.queryParams) > 0 {
params := make(map[string]map[ParamMatchingStrategy]string, len(s.request.queryParams))
for key, param := range s.request.queryParams {
if len(r.queryParams) > 0 {
params := make(map[string]map[ParamMatchingStrategy]string, len(r.queryParams))
for key, param := range r.queryParams {
params[key] = map[ParamMatchingStrategy]string{
param.Strategy(): param.Value(),
}
}
jsonStubRule.Request["queryParameters"] = params
req["queryParameters"] = params
}

return json.Marshal(jsonStubRule)
return req
}