100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package webcontext
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWebContext_SetAndGetUser(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
// Initially empty
|
|
require.Empty(t, c.GetUserID())
|
|
require.Empty(t, c.GetUserEmail())
|
|
require.False(t, c.UserIsLogged())
|
|
|
|
// Set user
|
|
c.SetUser("user123", "test@example.com")
|
|
|
|
// Check values
|
|
require.Equal(t, "user123", c.GetUserID())
|
|
require.Equal(t, "test@example.com", c.GetUserEmail())
|
|
require.True(t, c.UserIsLogged())
|
|
}
|
|
|
|
func TestWebContext_SetUserID(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
c.SetUserID("user456")
|
|
|
|
require.Equal(t, "user456", c.GetUserID())
|
|
require.True(t, c.UserIsLogged())
|
|
}
|
|
|
|
func TestWebContext_SetUserEmail(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
c.SetUserEmail("email@example.com")
|
|
|
|
require.Equal(t, "email@example.com", c.GetUserEmail())
|
|
}
|
|
|
|
func TestWebContext_RequestID(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
// Initially empty
|
|
require.Empty(t, c.GetRequestID())
|
|
|
|
// Set request ID
|
|
c.SetRequestID("req-123")
|
|
|
|
require.Equal(t, "req-123", c.GetRequestID())
|
|
}
|
|
|
|
func TestWebContext_WithContext(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
c.SetUser("user789", "user@example.com")
|
|
|
|
// Create new context with modified context
|
|
c2 := c.WithContext(c.Context())
|
|
|
|
// Should have same values
|
|
require.Equal(t, "user789", c2.GetUserID())
|
|
require.Equal(t, "user@example.com", c2.GetUserEmail())
|
|
}
|
|
|
|
func TestWebContext_ResponseWriter(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
require.Equal(t, w, c.ResponseWriter())
|
|
}
|
|
|
|
func TestWebContext_Request(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
w := httptest.NewRecorder()
|
|
c := NewWebContext(w, req)
|
|
|
|
// After setting user, request should be updated
|
|
c.SetUser("user999", "test@test.com")
|
|
updatedReq := c.Request()
|
|
|
|
// Create new context from updated request
|
|
c2 := NewWebContext(w, updatedReq)
|
|
require.Equal(t, "user999", c2.GetUserID())
|
|
}
|