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 query param to be able to reset existing session and make it the last one #288

Open
wants to merge 1 commit 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
8 changes: 7 additions & 1 deletion server/handlers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ func (a *Admin) SummarizeSessions(c echo.Context) error {

func (a *Admin) NewSession(c echo.Context) error {
name := c.QueryParam("name")
session := a.mocksServices.NewSession(name)
override, _ := strconv.ParseBool(c.QueryParam("override"))
var session *types.Session
if override {
session = a.mocksServices.ResetSession(name)
} else {
session = a.mocksServices.NewSession(name)
}
return respondAccordingAccept(c, types.SessionSummary(*session))
}

Expand Down
30 changes: 30 additions & 0 deletions server/services/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Mocks interface {
GetHistory(sessionID string) (types.History, error)
GetHistoryByPath(sessionID, filterPath string) (types.History, error)
NewSession(name string) *types.Session
ResetSession(name string) *types.Session
UpdateSession(id, name string) (*types.Session, error)
GetLastSession() *types.Session
GetSessionByID(id string) (*types.Session, error)
Expand Down Expand Up @@ -222,6 +223,35 @@ func (s *mocks) NewSession(name string) *types.Session {
return session
}

func (s *mocks) ResetSession(name string) *types.Session {
s.mu.Lock()

var foundSession *types.Session
sessions := make([]*types.Session, 0, len(s.sessions))
for _, ses := range s.sessions {
session := *ses
if session.Name == name {
foundSession = &session
continue
}
sessions = append(sessions, &session)
}

if foundSession == nil {
s.mu.Unlock()
return s.NewSession(name)
}

foundSession.Date = time.Now()
foundSession.History = types.History{}
foundSession.Mocks = types.Mocks{}
s.sessions = append(sessions, foundSession)

go s.persistence.StoreSession(s.sessions.Summarize(), foundSession)
s.mu.Unlock()
return foundSession
}

func (s *mocks) UpdateSession(sessionID, name string) (*types.Session, error) {
session, err := s.GetSessionByID(sessionID)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions tests/features/use_sessions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,31 @@ testcases:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 3
- result.bodyjson.bodyjson0.name ShouldEqual test4

- name: Override 'test4' session
steps:
- type: http
method: POST
url: http://localhost:8081/sessions?name=test4&override=true
- type: http
method: GET
url: http://localhost:8081/history
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 0
- type: http
method: GET
url: http://localhost:8081/mocks
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 0
- type: http
method: GET
url: http://localhost:8081/sessions/summary
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 3
- result.bodyjson.bodyjson0.name ShouldEqual test2
- result.bodyjson.bodyjson1.name ShouldEqual test3
- result.bodyjson.bodyjson2.name ShouldEqual test4
- result.bodyjson.bodyjson2.id ShouldEqual {{.RetrieveSessionsSummary.session_id}}