Skip to content

Commit

Permalink
feat(#21): provide basic verifier client
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Jul 21, 2023
1 parent 80c5306 commit b827013
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -109,6 +109,13 @@ Useful for unit testing.
mockClient := aurestmock.New(mockResponses, mockErrors)
```

#### 1c. Or use verifier (super-basic consumer interaction testing)

This mock client doesn't make actual requests, but instead you set up a list of
expected interactions.

This allows doing very simple consumer tests, useful mostly for their documentation value.

#### 2. Response recording

If your tests use Option 1a (playback), you should insert a response recorder in your production stack.
Expand Down
145 changes: 145 additions & 0 deletions implementation/verifier/verifier.go
@@ -0,0 +1,145 @@
package aurestverifier

import (
"context"
"encoding/json"
"errors"
"fmt"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
"io"
"net/url"
)

type VerifierImpl struct {
expectations []Expectation
firstUnexpected *Request
}

type Request struct {
Name string // key for the request
Method string
Url string
Body interface{}
}

type ResponseOrError struct {
Response aurestclientapi.ParsedResponse
Error error
}

type Expectation struct {
Request Request
Response ResponseOrError
Matched bool
}

func (e Expectation) matches(method string, requestUrl string, requestBody interface{}) bool {
// this is a very simple "must match 100%" for the first version
urlMatches := e.Request.Url == requestUrl
methodMatches := e.Request.Method == method
bodyMatches := requestBodyAsString(e.Request.Body) == requestBodyAsString(requestBody)

return urlMatches && methodMatches && bodyMatches
}

func requestBodyAsString(requestBody interface{}) string {
if requestBody == nil {
return ""
}
if asCustom, ok := requestBody.(aurestclientapi.CustomRequestBody); ok {
if b, err := io.ReadAll(asCustom.BodyReader); err == nil {
return string(b)
} else {
return fmt.Sprintf("ERROR: %s", err.Error())
}
}
if asString, ok := requestBody.(string); ok {
return asString
}
if asUrlValues, ok := requestBody.(url.Values); ok {
asString := asUrlValues.Encode()
return asString
}

marshalled, err := json.Marshal(requestBody)
if err != nil {
return fmt.Sprintf("ERROR: %s", err.Error())
}
return string(marshalled)
}

func New() (aurestclientapi.Client, *VerifierImpl) {
instance := &VerifierImpl{
expectations: make([]Expectation, 0),
}
return instance, instance
}

func (c *VerifierImpl) Perform(ctx context.Context, method string, requestUrl string, requestBody interface{}, response *aurestclientapi.ParsedResponse) error {
expected, err := c.currentExpectation(method, requestUrl, requestBody)
if err != nil {
return err
}

if expected.Response.Error != nil {
return expected.Response.Error
}

mockResponse := expected.Response.Response

response.Header = mockResponse.Header
response.Status = mockResponse.Status
response.Time = mockResponse.Time
if response.Body != nil && mockResponse.Body != nil {
// copy over through json round trip
marshalled, _ := json.Marshal(mockResponse.Body)
_ = json.Unmarshal(marshalled, response.Body)
}

return nil
}

func (c *VerifierImpl) currentExpectation(method string, requestUrl string, requestBody interface{}) (Expectation, error) {
for i, e := range c.expectations {
if !e.Matched {
if e.matches(method, requestUrl, requestBody) {
c.expectations[i].Matched = true
return e, nil
} else {
if c.firstUnexpected == nil {
c.firstUnexpected = &Request{
Name: fmt.Sprintf("unmatched expectation %d - %s", i+1, e.Request.Name),
Method: method,
Url: requestUrl,
Body: requestBody,
}
}
return Expectation{}, fmt.Errorf("unmatched expectation %d - %s", i+1, e.Request.Name)
}
}
}

if c.firstUnexpected == nil {
c.firstUnexpected = &Request{
Name: fmt.Sprintf("no expectations remaining - unexpected request at end"),
Method: method,
Url: requestUrl,
Body: requestBody,
}
}
return Expectation{}, errors.New("no expectations remaining - unexpected request at end")
}

func (c *VerifierImpl) AddExpectation(requestMatcher Request, response aurestclientapi.ParsedResponse, err error) {
c.expectations = append(c.expectations, Expectation{
Request: requestMatcher,
Response: ResponseOrError{
Response: response,
Error: err,
},
})
}

func (c *VerifierImpl) FirstUnexpectedOrNil() *Request {
return c.firstUnexpected
}

0 comments on commit b827013

Please sign in to comment.