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

feat: add ability to provide own mock id #304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions server/handlers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ func (a *Admin) AddMocks(c echo.Context) error {
}

for _, mock := range mocks {
if mock.MockID != "" { // user has defined own mock ID
_, err := a.mocksServices.GetMockByID(sessionID, mock.MockID)
if err == nil {
return echo.NewHTTPError(http.StatusConflict, "Mock ID: "+mock.MockID+" already exists.")
}

if err != types.MockNotFound {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
}

if _, err := a.mocksServices.AddMock(sessionID, mock); err != nil {
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
Expand Down
7 changes: 6 additions & 1 deletion server/types/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (m Mocks) Clone() Mocks {
}

type Mock struct {
MockID string `json:"mockId,omitempty" yaml:"mockId"`
Request MockRequest `json:"request,omitempty" yaml:"request"`
Response *MockResponse `json:"response,omitempty" yaml:"response,omitempty"`
Context *MockContext `json:"context,omitempty" yaml:"context,omitempty"`
Expand Down Expand Up @@ -67,7 +68,11 @@ func (m *Mock) Validate() error {
func (m *Mock) Init() {
m.State = &MockState{
CreationDate: time.Now(),
ID: shortid.MustGenerate(),
}
if m.MockID != "" {
m.State.ID = m.MockID
} else {
m.State.ID = shortid.MustGenerate()
}

if m.Context == nil {
Expand Down
8 changes: 8 additions & 0 deletions tests/data/basic_mock_with_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- request:
path: /test
response:
headers:
Content-Type: application/json
body: >
{"message": "test"}
mockId: "1"
33 changes: 33 additions & 0 deletions tests/features/use_mocks_with_own_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Use mocks to respond
version: "2"
testcases:
- name: Use basic mock with id
steps:
- type: http
method: POST
url: http://localhost:8081/mocks?reset=true
bodyFile: ../data/basic_mock_with_id.yml
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.message ShouldEqual "Mocks registered successfully"

- type: http
method: GET
url: http://localhost:8080/test
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.message ShouldEqual test

- type: http
method: GET
url: http://localhost:8081/mocks
assertions:
- result.bodyjson.__len__ ShouldEqual 1
- result.bodyjson.bodyjson0.state.id ShouldEqual 1

- type: http
method: POST
url: http://localhost:8081/mocks
bodyFile: ../data/basic_mock_with_id.yml
assertions:
- result.statuscode ShouldEqual 409