Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context #11

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ func main() {
Build()

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

limit := mock.NewMockCallLimit(3)

mockDefinition := mock.NewDefinition(request, response)
mockDefinition := mock.NewDefinition(request, response, limit)

err = instance.AddMock(mockDefinition)
if err != nil {
Expand Down Expand Up @@ -89,7 +91,28 @@ response := mock.NewResponseBuilder(http.StatusOK).
AddHeader("Content-Type", "application/json").
Build()

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

You can also include a limit on how many times a mock can be called using the `NewMockCallLimit` 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()

limit := mock.NewMockCallLimit(5)

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

### Raw Json
Expand Down
4 changes: 3 additions & 1 deletion examples/createmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func main() {

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

mockDefinition := mock.NewDefinition(request, response)
limit := mock.NewMockCallLimit(3)

mockDefinition := mock.NewDefinition(request, response, limit)

err = instance.AddMock(mockDefinition)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions mock/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mock

func NewMockCallLimit(t int) *Context {
return &Context{Times: t}
}
15 changes: 15 additions & 0 deletions mock/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mock_test

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

func TestNewMockCallLimit(t *testing.T) {
expectedContext := &mock.Context{Times: 3}

actualContext := mock.NewMockCallLimit(3)

assert.Equal(t, expectedContext, actualContext)
}
15 changes: 13 additions & 2 deletions mock/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,27 @@ 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, context *Context) Definition {
danielchurm marked this conversation as resolved.
Show resolved Hide resolved
def := Definition{
Request: req,
Response: resp,
}

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

return def
}

func (d Definition) ToMockDefinitionJson() ([]byte, error) {
Expand Down
65 changes: 56 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,57 @@ func TestDefinition_ToMockJson(t *testing.T) {
}
}`

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

actualJson, err := definition.ToMockDefinitionJson()
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()
context := createContext()
definition := mock.NewDefinition(request, response, context)

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 Expand Up @@ -79,3 +122,7 @@ func createResponse() mock.Response {
}
return response
}

func createContext() *mock.Context {
return &mock.Context{Times: 3}
}