diff --git a/server/handlers/admin.go b/server/handlers/admin.go index 6807e6e..5a5dfa4 100644 --- a/server/handlers/admin.go +++ b/server/handlers/admin.go @@ -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)) } diff --git a/server/services/mocks.go b/server/services/mocks.go index 00436d2..12d0191 100644 --- a/server/services/mocks.go +++ b/server/services/mocks.go @@ -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) @@ -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 { diff --git a/tests/features/use_sessions.yml b/tests/features/use_sessions.yml index e913164..c378fd1 100644 --- a/tests/features/use_sessions.yml +++ b/tests/features/use_sessions.yml @@ -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}}