...

Source file src/edge-infra.dev/pkg/edge/auth-proxy/session/mock.go

Documentation: edge-infra.dev/pkg/edge/auth-proxy/session

     1  package session
     2  
     3  import (
     4  	"github.com/gin-contrib/sessions"
     5  
     6  	"edge-infra.dev/pkg/lib/uuid"
     7  )
     8  
     9  // MockSessions represents a session store in memory.
    10  type MockSessions struct {
    11  	name     string
    12  	sessions map[interface{}]MockSession
    13  	opts     sessions.Options
    14  }
    15  
    16  // MockSession represents a single session.
    17  type MockSession struct {
    18  	id  string
    19  	key interface{}
    20  	val interface{}
    21  }
    22  
    23  // NewMockSessions returns a new instance of MockSessions.
    24  func NewMockSessions() *MockSessions {
    25  	return NewMockSessionsWithOptions("mock-sessions", make(map[interface{}]MockSession))
    26  }
    27  
    28  // NewMockSessionsWithOptions returns a new instance of MockSessions with the specified params.
    29  func NewMockSessionsWithOptions(name string, sess map[interface{}]MockSession) *MockSessions {
    30  	return &MockSessions{
    31  		name:     name,
    32  		sessions: sess,
    33  	}
    34  }
    35  
    36  func (m *MockSessions) ID() string {
    37  	return ""
    38  }
    39  
    40  // Get returns a session whose key is specified.
    41  func (m *MockSessions) Get(key interface{}) interface{} {
    42  	return m.sessions[key].val
    43  }
    44  
    45  // Set sets a session with the specified key and value.
    46  func (m *MockSessions) Set(key interface{}, val interface{}) {
    47  	m.sessions[key] = MockSession{
    48  		id:  uuid.New().UUID,
    49  		key: key,
    50  		val: val,
    51  	}
    52  }
    53  
    54  // Delete deletes a session whose key is specified.
    55  func (m *MockSessions) Delete(key interface{}) {
    56  	delete(m.sessions, key)
    57  }
    58  
    59  // Clear clears all sessions.
    60  func (m *MockSessions) Clear() {
    61  	m.sessions = make(map[interface{}]MockSession)
    62  }
    63  
    64  func (m *MockSessions) AddFlash(_ interface{}, _ ...string) {
    65  	// TODO(pa250194_ncrvoyix): decided not to implement as the proxy does not call it.
    66  }
    67  
    68  func (m *MockSessions) Flashes(_ ...string) []interface{} {
    69  	// TODO(pa250194_ncrvoyix): decided not to implement as the proxy does not call it.
    70  	return make([]interface{}, 0)
    71  }
    72  
    73  // Options sets the options for the sessions.
    74  func (m *MockSessions) Options(opts sessions.Options) {
    75  	m.opts = opts
    76  }
    77  
    78  // Save retruns nil as set adds to map of sessions already.
    79  func (m *MockSessions) Save() error {
    80  	return nil
    81  }
    82  

View as plain text