Skip to content

Commit

Permalink
Add the Context option to the mock definition #minor (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
valxntine authored May 1, 2024
1 parent 4e62e1c commit 015cf97
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
Build()

response := mock.NewResponseBuilder(http.StatusOK).AddBody(`{"status": "OK"}`).Build()

mockDefinition := mock.NewDefinition(request, response)

err = instance.AddMock(mockDefinition)
Expand Down Expand Up @@ -92,6 +92,25 @@ response := mock.NewResponseBuilder(http.StatusOK).
mockDefinition := mock.NewDefinition(request, response)
```

You can also include a limit on how many times a mock can be called using the `WithCallLimit` option function.

```go
request := mock.NewRequestBuilder(http.MethodPut, "/foo/bar").
AddQueryParam("limit", "10").
AddQueryParam("filters", "red", "green").
AddHeader("Content-Type", "application/json", "application/vnd.api+json").
AddBearerAuthToken("sv2361fr1o8ph3oin").
AddJsonBody(`{"example": "body"`).
Build()

response := mock.NewResponseBuilder(http.StatusOK).
AddBody(`{"status": "OK"}`).
AddHeader("Content-Type", "application/json").
Build()

mockDefinition := mock.NewDefinition(request, response, mock.WithCallLimit(3))
```

### Raw Json

Not all features of the Smocker mocks have been captured in the builders and new features may be added in the future. To
Expand Down
13 changes: 13 additions & 0 deletions mock/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mock

type ContextOption func(context *Context) *Context

func WithCallLimit(times int) ContextOption {
return func(context *Context) *Context {
if context == nil {
context = &Context{}
}
context.Times = times
return context
}
}
35 changes: 35 additions & 0 deletions mock/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mock_test

import (
"github.com/churmd/smockerclient/mock"
"github.com/stretchr/testify/assert"
"testing"
)

func TestWithCallLimit(t *testing.T) {
t.Run("it creates a new context with the times key if one doesnt exist", func(t *testing.T) {
req := createRequest()
res := createResponse()
def := mock.NewDefinition(req, res, mock.WithCallLimit(3))

expectedDef := mock.Definition{
Request: req,
Response: res,
Context: &mock.Context{Times: 3},
}
assert.Equal(t, expectedDef, def)
})

t.Run("if a context already exists, it uses that", func(t *testing.T) {
req := createRequest()
res := createResponse()
def := mock.NewDefinition(req, res, mock.WithCallLimit(3), mock.WithCallLimit(5))

expectedDef := mock.Definition{
Request: req,
Response: res,
Context: &mock.Context{Times: 5},
}
assert.Equal(t, expectedDef, def)
})
}
20 changes: 18 additions & 2 deletions mock/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ type Response struct {
Body string `json:"body,omitempty"`
}

type Context struct {
Times int `json:"times"`
}

type Definition struct {
Request Request `json:"request"`
Response Response `json:"response"`
Context *Context `json:"context,omitempty"`
}

func NewDefinition(req Request, resp Response) Definition {
return Definition{
func NewDefinition(req Request, resp Response, contextOptions ...ContextOption) Definition {
def := Definition{
Request: req,
Response: resp,
}

var context *Context
for _, fn := range contextOptions {
context = fn(context)
}

if context != nil {
def.Context = context
}

return def
}

func (d Definition) ToMockDefinitionJson() ([]byte, error) {
Expand Down
60 changes: 51 additions & 9 deletions mock/definition_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package mock_test

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/churmd/smockerclient/mock"
)

func TestDefinition_ToMockJson(t *testing.T) {
expectedJson := `{
t.Run("With no Context", func(t *testing.T) {
expectedJson := `{
"request": {
"method": "PUT",
"path": "/foo/bar",
Expand All @@ -36,14 +36,56 @@ func TestDefinition_ToMockJson(t *testing.T) {
}
}`

request := createRequest()
response := createResponse()
definition := mock.NewDefinition(request, response)
request := createRequest()
response := createResponse()
definition := mock.NewDefinition(request, response)

actualJson, err := definition.ToMockDefinitionJson()

assert.NoError(t, err)
assert.JSONEq(t, expectedJson, string(actualJson))
})

t.Run("With Context", func(t *testing.T) {
expectedJson := `{
"request": {
"method": "PUT",
"path": "/foo/bar",
"query_params": {
"limit": ["10"],
"filters": ["red", "green"]
},
"headers": {
"Content-Type": ["application/json", "application/vnd.api+json"],
"Authorization": ["Bearer sv2361fr1o8ph3oin"]
},
"body": {
"matcher": "ShouldEqualJSON",
"value": "{\"name\": \"John Smith\", \"uuid\": \"daa7b90d-9429-4d7a-9304-edc41ff44a6d\", \"rank\": 10}"
}
},
"context": {
"times": 3
},
"response": {
"status": 200,
"headers": {
"Content-Type": ["application/json"]
},
"body": "{\"status\": \"OK\"}"
}
}`

request := createRequest()
response := createResponse()
definition := mock.NewDefinition(request, response, mock.WithCallLimit(3))

actualJson, err := definition.ToMockDefinitionJson()

actualJson, err := definition.ToMockDefinitionJson()
assert.NoError(t, err)
assert.JSONEq(t, expectedJson, string(actualJson))
})

assert.NoError(t, err)
assert.JSONEq(t, expectedJson, string(actualJson))
}

func createRequest() mock.Request {
Expand Down

0 comments on commit 015cf97

Please sign in to comment.