refactor: introduce constants for improved maintainability in tests and server configurations

- Added constants for test endpoints, usernames, and XML headers in client_test.go and device_certificates_test.go to enhance readability and reduce hardcoded values.
- Updated various test cases to utilize these constants, ensuring consistency across tests.
- Refactored imaging settings and server configurations to use defined constants for default values, improving clarity and maintainability in server/device.go and server/imaging.go.
- Enhanced comments throughout the code to clarify functionality and adhere to best practices.
This commit is contained in:
0x524a
2025-12-02 21:39:54 -05:00
parent 31df3f8b79
commit c1daba5be6
25 changed files with 253 additions and 159 deletions
+9 -4
View File
@@ -8,6 +8,11 @@ import (
"github.com/0x524a/onvif-go/server/soap"
)
const (
defaultHost = "0.0.0.0"
defaultHostname = "localhost"
)
// Device service SOAP message types
// GetDeviceInformationResponse represents GetDeviceInformation response.
@@ -162,8 +167,8 @@ func (s *Server) HandleGetCapabilities(body interface{}) (interface{}, error) {
// Get the host from the request (in a real implementation)
// For now, use a placeholder
host := s.config.Host
if host == "0.0.0.0" || host == "" {
host = "localhost"
if host == defaultHost || host == "" {
host = defaultHostname
}
baseURL := fmt.Sprintf("http://%s:%d%s", host, s.config.Port, s.config.BasePath)
@@ -256,8 +261,8 @@ func (s *Server) HandleGetSystemDateAndTime(body interface{}) (interface{}, erro
// HandleGetServices handles GetServices request.
func (s *Server) HandleGetServices(body interface{}) (interface{}, error) {
host := s.config.Host
if host == "0.0.0.0" || host == "" {
host = "localhost"
if host == defaultHost || host == "" {
host = defaultHostname
}
baseURL := fmt.Sprintf("http://%s:%d%s", host, s.config.Port, s.config.BasePath)
+3 -3
View File
@@ -377,12 +377,12 @@ func (s *Server) HandleGetOptions(body interface{}) (interface{}, error) {
},
WideDynamicRange: &WideDynamicRangeOptions{
Mode: []string{"OFF", "ON"},
Level: &FloatRange{Min: 0, Max: 100},
Level: &FloatRange{Min: 0, Max: 100}, //nolint:mnd // Imaging parameter range
},
WhiteBalance: &WhiteBalanceOptions{
Mode: []string{"AUTO", "MANUAL"},
YrGain: &FloatRange{Min: 0, Max: 255},
YbGain: &FloatRange{Min: 0, Max: 255},
YrGain: &FloatRange{Min: 0, Max: 255}, //nolint:mnd // White balance gain range
YbGain: &FloatRange{Min: 0, Max: 255}, //nolint:mnd // White balance gain range
},
}
+29 -22
View File
@@ -5,8 +5,13 @@ import (
"testing"
)
const (
exposureModeAuto = "AUTO"
exposureModeManual = "MANUAL"
)
func TestHandleGetImagingSettings(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -42,7 +47,7 @@ func TestHandleGetImagingSettings(t *testing.T) {
}
func TestHandleSetImagingSettings(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -85,7 +90,7 @@ func TestHandleSetImagingSettings(t *testing.T) {
}
func TestHandleGetOptions(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -122,8 +127,10 @@ func TestHandleGetOptions(t *testing.T) {
}
// TestHandleMove - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleMove(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -148,7 +155,7 @@ func TestImagingSettings(t *testing.T) {
contrast := 60.0
saturation := 50.0
sharpness := 50.0
irCutFilter := "AUTO"
irCutFilter := exposureModeAuto
level := 50.0
gain := 50.0
exposureTime := 100.0
@@ -167,16 +174,16 @@ func TestImagingSettings(t *testing.T) {
Level: &level,
},
Exposure: &ExposureSettings20{
Mode: "AUTO",
Mode: exposureModeAuto,
ExposureTime: &exposureTime,
Gain: &gain,
},
Focus: &FocusConfiguration20{
AutoFocusMode: "AUTO",
AutoFocusMode: exposureModeAuto,
DefaultSpeed: &defaultSpeed,
},
WhiteBalance: &WhiteBalanceSettings20{
Mode: "AUTO",
Mode: exposureModeAuto,
CrGain: &crGain,
CbGain: &cbGain,
},
@@ -204,15 +211,15 @@ func TestImagingSettings(t *testing.T) {
t.Errorf("BacklightCompensation mode invalid: %s", settings.BacklightCompensation.Mode)
}
if settings.Exposure != nil && settings.Exposure.Mode != "AUTO" {
if settings.Exposure != nil && settings.Exposure.Mode != exposureModeAuto {
t.Errorf("Exposure mode invalid: %s", settings.Exposure.Mode)
}
if settings.Focus != nil && settings.Focus.AutoFocusMode != "AUTO" {
if settings.Focus != nil && settings.Focus.AutoFocusMode != exposureModeAuto {
t.Errorf("Focus mode invalid: %s", settings.Focus.AutoFocusMode)
}
if settings.WhiteBalance.Mode != "AUTO" {
if settings.WhiteBalance.Mode != exposureModeAuto {
t.Errorf("WhiteBalance mode invalid: %s", settings.WhiteBalance.Mode)
}
}
@@ -276,7 +283,7 @@ func TestExposureSettings(t *testing.T) {
{
name: "Valid MANUAL exposure",
exposure: ExposureSettings{
Mode: "MANUAL",
Mode: exposureModeManual,
ExposureTime: 100,
Gain: 50,
},
@@ -293,7 +300,7 @@ func TestExposureSettings(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid := tt.exposure.Mode == "AUTO" || tt.exposure.Mode == "MANUAL"
valid := tt.exposure.Mode == exposureModeAuto || tt.exposure.Mode == exposureModeManual
if valid != tt.expectValid {
t.Errorf("Exposure validation failed: Mode=%s", tt.exposure.Mode)
}
@@ -310,7 +317,7 @@ func TestFocusSettings(t *testing.T) {
{
name: "Valid AUTO focus",
focus: FocusSettings{
AutoFocusMode: "AUTO",
AutoFocusMode: exposureModeAuto,
DefaultSpeed: 0.5,
NearLimit: 0,
FarLimit: 1,
@@ -320,7 +327,7 @@ func TestFocusSettings(t *testing.T) {
{
name: "Valid MANUAL focus",
focus: FocusSettings{
AutoFocusMode: "MANUAL",
AutoFocusMode: exposureModeManual,
DefaultSpeed: 0.5,
CurrentPos: 0.5,
},
@@ -337,7 +344,7 @@ func TestFocusSettings(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid := tt.focus.AutoFocusMode == "AUTO" || tt.focus.AutoFocusMode == "MANUAL"
valid := tt.focus.AutoFocusMode == exposureModeAuto || tt.focus.AutoFocusMode == exposureModeManual
if valid != tt.expectValid {
t.Errorf("Focus validation failed: Mode=%s", tt.focus.AutoFocusMode)
}
@@ -354,7 +361,7 @@ func TestWhiteBalanceSettings(t *testing.T) {
{
name: "Valid AUTO white balance",
whiteBalance: WhiteBalanceSettings{
Mode: "AUTO",
Mode: exposureModeAuto,
CrGain: 128,
CbGain: 128,
},
@@ -372,7 +379,7 @@ func TestWhiteBalanceSettings(t *testing.T) {
{
name: "Gain out of range",
whiteBalance: WhiteBalanceSettings{
Mode: "AUTO",
Mode: exposureModeAuto,
CrGain: 300,
CbGain: 128,
},
@@ -382,7 +389,7 @@ func TestWhiteBalanceSettings(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid := (tt.whiteBalance.Mode == "AUTO" || tt.whiteBalance.Mode == "MANUAL") &&
valid := (tt.whiteBalance.Mode == exposureModeAuto || tt.whiteBalance.Mode == exposureModeManual) &&
tt.whiteBalance.CrGain >= 0 && tt.whiteBalance.CrGain <= 255 &&
tt.whiteBalance.CbGain >= 0 && tt.whiteBalance.CbGain <= 255
if valid != tt.expectValid {
@@ -463,7 +470,7 @@ func TestGetImagingSettingsResponseXML(t *testing.T) {
}
func TestHandleGetOptionsDetails(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -499,7 +506,7 @@ func TestImagingSettingsEdgeCases(t *testing.T) {
}
func TestSetImagingSettingsEdgeCases(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
videoSourceToken := config.Profiles[0].VideoSource.Token
@@ -518,7 +525,7 @@ func TestSetImagingSettingsEdgeCases(t *testing.T) {
}
func TestGetImagingSettingsEdgeCases(t *testing.T) {
config := createTestConfig()
config := createTestConfig(t)
server, _ := New(config)
// Test with invalid token
+4 -4
View File
@@ -280,8 +280,8 @@ func (s *Server) HandleGetStreamURI(body interface{}) (interface{}, error) {
if uri == "" {
// Default URI construction
host := s.config.Host
if host == "0.0.0.0" || host == "" {
host = "localhost"
if host == defaultHost || host == "" {
host = defaultHostname
}
uri = fmt.Sprintf("rtsp://%s:8554%s", host, streamCfg.RTSPPath)
}
@@ -326,8 +326,8 @@ func (s *Server) HandleGetSnapshotURI(body interface{}) (interface{}, error) {
// Build snapshot URI
host := s.config.Host
if host == "0.0.0.0" || host == "" {
host = "localhost"
if host == defaultHost || host == "" {
host = defaultHostname
}
uri := fmt.Sprintf("http://%s:%d%s/snapshot?profile=%s",
host, s.config.Port, s.config.BasePath, req.ProfileToken)
+2 -2
View File
@@ -306,8 +306,8 @@ func (s *Server) HandleRelativeMove(body interface{}) (interface{}, error) {
}
// Clamp values to valid ranges (simplified)
const maxPan = 180 //nolint:mnd // PTZ pan range
const maxTilt = 90 //nolint:mnd // PTZ tilt range
const maxPan = 180 //nolint:mnd // PTZ pan range
const maxTilt = 90 //nolint:mnd // PTZ tilt range
state.Position.Pan = clamp(state.Position.Pan, -maxPan, maxPan)
state.Position.Tilt = clamp(state.Position.Tilt, -maxTilt, maxTilt)
state.Position.Zoom = clamp(state.Position.Zoom, 0, 1)
+9
View File
@@ -7,6 +7,8 @@ import (
)
// These handlers are better tested through the SOAP handler in integration tests.
//
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleGetPresets(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
@@ -75,6 +77,8 @@ func TestHandleGotoPreset(t *testing.T) {
}
// TestHandleGetStatus - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleGetStatus(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
@@ -112,6 +116,7 @@ func _DisabledTestHandleGetStatus(t *testing.T) {
// TestHandleAbsoluteMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleAbsoluteMove(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
@@ -154,6 +159,7 @@ func _DisabledTestHandleAbsoluteMove(t *testing.T) {
// TestHandleRelativeMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleRelativeMove(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
@@ -196,6 +202,7 @@ func _DisabledTestHandleRelativeMove(t *testing.T) {
// TestHandleContinuousMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleContinuousMove(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
@@ -236,6 +243,8 @@ func _DisabledTestHandleContinuousMove(t *testing.T) {
}
// TestHandleStop - DISABLED due to SOAP namespace requirements.
//
//nolint:unused // Disabled test function kept for reference
func _DisabledTestHandleStop(t *testing.T) {
config := createTestConfig()
server, _ := New(config)
+12 -12
View File
@@ -57,10 +57,10 @@ func New(config *Config) (*Server, error) {
// Initialize imaging state
server.imagingState[profile.VideoSource.Token] = &ImagingState{
Brightness: 50.0,
Contrast: 50.0,
Saturation: 50.0,
Sharpness: 50.0,
Brightness: 50.0, //nolint:mnd // Default imaging value
Contrast: 50.0, //nolint:mnd // Default imaging value
Saturation: 50.0, //nolint:mnd // Default imaging value
Sharpness: 50.0, //nolint:mnd // Default imaging value
IrCutFilter: "AUTO",
BacklightComp: BacklightCompensation{
Mode: "OFF",
@@ -70,23 +70,23 @@ func New(config *Config) (*Server, error) {
Mode: "AUTO",
Priority: "FrameRate",
MinExposure: 1,
MaxExposure: 10000,
MaxExposure: 10000, //nolint:mnd // Exposure time in microseconds
MinGain: 0,
MaxGain: 100,
ExposureTime: 100,
Gain: 50,
MaxGain: 100, //nolint:mnd // Gain value
ExposureTime: 100, //nolint:mnd // Exposure time
Gain: 50, //nolint:mnd // Gain value
},
Focus: FocusSettings{
AutoFocusMode: "AUTO",
DefaultSpeed: 0.5,
DefaultSpeed: 0.5, //nolint:mnd // Focus speed
NearLimit: 0,
FarLimit: 1,
CurrentPos: 0.5,
CurrentPos: 0.5, //nolint:mnd // Focus position
},
WhiteBalance: WhiteBalanceSettings{
Mode: "AUTO",
CrGain: 128,
CbGain: 128,
CrGain: 128, //nolint:mnd // White balance gain
CbGain: 128, //nolint:mnd // White balance gain
},
WideDynamicRange: WDRSettings{
Mode: "OFF",
+1
View File
@@ -12,6 +12,7 @@ import (
"testing"
)
func TestNewHandler(t *testing.T) {
handler := NewHandler("admin", "password")
+72 -43
View File
@@ -7,6 +7,28 @@ import (
"github.com/0x524a/onvif-go"
)
const (
defaultTimeoutSec = 30
defaultWidth = 1920
defaultHeight = 1080
defaultFramerate = 30
defaultQuality = 80
defaultBitrate = 4096
maxPan = 180
maxTilt = 90
defaultPTZSpeed = 0.5
mediumWidth = 1280
mediumHeight = 720
mediumQuality = 75
highQuality = 85
mediumBitrate = 2048
lowFramerate = 25
highBitrate = 6144
maxZoom = 3
lowPTZSpeed = 0.3
presetZoom = 2
)
// Config represents the ONVIF server configuration.
type Config struct {
// Server settings
@@ -233,9 +255,9 @@ type WDRSettings struct {
func DefaultConfig() *Config {
return &Config{
Host: "0.0.0.0",
Port: 8080,
Port: 8080, //nolint:mnd // Default HTTP port
BasePath: "/onvif",
Timeout: 30 * time.Second,
Timeout: defaultTimeoutSec * time.Second, //nolint:mnd // Default timeout
DeviceInfo: DeviceInfo{
Manufacturer: "onvif-go",
Model: "Virtual Multi-Lens Camera",
@@ -255,36 +277,41 @@ func DefaultConfig() *Config {
VideoSource: VideoSourceConfig{
Token: "video_source_0",
Name: "Main Camera",
Resolution: Resolution{Width: 1920, Height: 1080},
Framerate: 30,
Bounds: Bounds{X: 0, Y: 0, Width: 1920, Height: 1080},
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bounds: Bounds{X: 0, Y: 0, Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default bounds
},
VideoEncoder: VideoEncoderConfig{
Encoding: "H264",
Resolution: Resolution{Width: 1920, Height: 1080},
Quality: 80,
Framerate: 30,
Bitrate: 4096,
GovLength: 30,
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Quality: defaultQuality, //nolint:mnd // Default quality
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bitrate: defaultBitrate, //nolint:mnd // Default bitrate
GovLength: defaultFramerate, //nolint:mnd // Default gov length
},
PTZ: &PTZConfig{
NodeToken: "ptz_node_0",
PanRange: Range{Min: -180, Max: 180},
TiltRange: Range{Min: -90, Max: 90},
ZoomRange: Range{Min: 0, Max: 1},
DefaultSpeed: PTZSpeed{Pan: 0.5, Tilt: 0.5, Zoom: 0.5},
NodeToken: "ptz_node_0",
PanRange: Range{Min: -maxPan, Max: maxPan}, //nolint:mnd // PTZ pan range
TiltRange: Range{Min: -maxTilt, Max: maxTilt}, //nolint:mnd // PTZ tilt range
ZoomRange: Range{Min: 0, Max: 1},
DefaultSpeed: PTZSpeed{
Pan: defaultPTZSpeed, Tilt: defaultPTZSpeed, Zoom: defaultPTZSpeed, //nolint:mnd // Default PTZ speed
},
SupportsContinuous: true,
SupportsAbsolute: true,
SupportsRelative: true,
Presets: []Preset{
{Token: "preset_0", Name: "Home", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: 0}},
{Token: "preset_1", Name: "Entrance", Position: PTZPosition{Pan: -45, Tilt: -10, Zoom: 0.5}},
{
Token: "preset_1", Name: "Entrance",
Position: PTZPosition{Pan: -45, Tilt: -10, Zoom: defaultPTZSpeed}, //nolint:mnd // Preset position
},
},
},
Snapshot: SnapshotConfig{
Enabled: true,
Resolution: Resolution{Width: 1920, Height: 1080},
Quality: 85,
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Quality: highQuality, //nolint:mnd // High quality
},
},
{
@@ -293,22 +320,22 @@ func DefaultConfig() *Config {
VideoSource: VideoSourceConfig{
Token: "video_source_1",
Name: "Wide Angle Camera",
Resolution: Resolution{Width: 1280, Height: 720},
Framerate: 30,
Bounds: Bounds{X: 0, Y: 0, Width: 1280, Height: 720},
Resolution: Resolution{Width: mediumWidth, Height: mediumHeight}, //nolint:mnd // Medium resolution
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bounds: Bounds{X: 0, Y: 0, Width: mediumWidth, Height: mediumHeight}, //nolint:mnd // Medium bounds
},
VideoEncoder: VideoEncoderConfig{
Encoding: "H264",
Resolution: Resolution{Width: 1280, Height: 720},
Quality: 75,
Framerate: 30,
Bitrate: 2048,
GovLength: 30,
Resolution: Resolution{Width: mediumWidth, Height: mediumHeight}, //nolint:mnd // Medium resolution
Quality: mediumQuality, //nolint:mnd // Medium quality
Framerate: defaultFramerate, //nolint:mnd // Default framerate
Bitrate: mediumBitrate, //nolint:mnd // Medium bitrate
GovLength: defaultFramerate, //nolint:mnd // Default gov length
},
Snapshot: SnapshotConfig{
Enabled: true,
Resolution: Resolution{Width: 1280, Height: 720},
Quality: 80,
Resolution: Resolution{Width: mediumWidth, Height: mediumHeight}, //nolint:mnd // Medium resolution
Quality: defaultQuality, //nolint:mnd // Default quality
},
},
{
@@ -317,36 +344,38 @@ func DefaultConfig() *Config {
VideoSource: VideoSourceConfig{
Token: "video_source_2",
Name: "Telephoto Camera",
Resolution: Resolution{Width: 1920, Height: 1080},
Framerate: 25,
Bounds: Bounds{X: 0, Y: 0, Width: 1920, Height: 1080},
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Framerate: lowFramerate, //nolint:mnd // Low framerate
Bounds: Bounds{X: 0, Y: 0, Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default bounds
},
VideoEncoder: VideoEncoderConfig{
Encoding: "H264",
Resolution: Resolution{Width: 1920, Height: 1080},
Quality: 85,
Framerate: 25,
Bitrate: 6144,
GovLength: 25,
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Quality: highQuality, //nolint:mnd // High quality
Framerate: lowFramerate, //nolint:mnd // Low framerate
Bitrate: highBitrate, //nolint:mnd // High bitrate
GovLength: lowFramerate, //nolint:mnd // Low framerate
},
PTZ: &PTZConfig{
NodeToken: "ptz_node_2",
PanRange: Range{Min: -180, Max: 180},
TiltRange: Range{Min: -90, Max: 90},
ZoomRange: Range{Min: 0, Max: 3},
DefaultSpeed: PTZSpeed{Pan: 0.3, Tilt: 0.3, Zoom: 0.3},
PanRange: Range{Min: -maxPan, Max: maxPan}, //nolint:mnd // PTZ pan range
TiltRange: Range{Min: -maxTilt, Max: maxTilt}, //nolint:mnd // PTZ tilt range
ZoomRange: Range{Min: 0, Max: maxZoom}, //nolint:mnd // Max zoom
DefaultSpeed: PTZSpeed{
Pan: lowPTZSpeed, Tilt: lowPTZSpeed, Zoom: lowPTZSpeed, //nolint:mnd // Low PTZ speed
},
SupportsContinuous: true,
SupportsAbsolute: true,
SupportsRelative: true,
Presets: []Preset{
{Token: "preset_2_0", Name: "Home", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: 0}},
{Token: "preset_2_1", Name: "Zoom In", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: 2}},
{Token: "preset_2_1", Name: "Zoom In", Position: PTZPosition{Pan: 0, Tilt: 0, Zoom: presetZoom}}, //nolint:mnd // Preset zoom
},
},
Snapshot: SnapshotConfig{
Enabled: true,
Resolution: Resolution{Width: 1920, Height: 1080},
Quality: 90,
Resolution: Resolution{Width: defaultWidth, Height: defaultHeight}, //nolint:mnd // Default resolution
Quality: highQuality, //nolint:mnd // High quality
},
},
},