feat: ServerMux to gorilla/mux, time route
This commit is contained in:
149
internal/apiserver/handlers/time_test.go
Normal file
149
internal/apiserver/handlers/time_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.nwaifu.su/sergey/MyGoServer/internal/apiserver/models"
|
||||
)
|
||||
|
||||
func TestTimeHandler_ServeHTTP(t *testing.T) {
|
||||
// Create a new time handler
|
||||
handler := NewTimeHandler()
|
||||
|
||||
// Create a test request
|
||||
req := httptest.NewRequest("GET", "/time", nil)
|
||||
|
||||
// Create a test response recorder
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// Call the handler
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check status code
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check content type
|
||||
if ct := rr.Header().Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("handler returned wrong content type: got %v want %v",
|
||||
ct, "application/json")
|
||||
}
|
||||
|
||||
// Check response body contains expected structure
|
||||
var response models.Response
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
|
||||
t.Errorf("handler returned invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Check success field
|
||||
if !response.Success {
|
||||
t.Errorf("handler returned success=false, want success=true")
|
||||
}
|
||||
|
||||
// Check that data exists and is a map
|
||||
if response.Data == nil {
|
||||
t.Errorf("handler returned nil data")
|
||||
return
|
||||
}
|
||||
|
||||
// Type assert to map[string]interface{}
|
||||
dataMap, ok := response.Data.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Errorf("handler returned data of unexpected type")
|
||||
return
|
||||
}
|
||||
|
||||
// Check required fields exist
|
||||
requiredFields := []string{"server_time", "unix_timestamp", "timezone"}
|
||||
for _, field := range requiredFields {
|
||||
if _, exists := dataMap[field]; !exists {
|
||||
t.Errorf("handler response missing required field: %s", field)
|
||||
}
|
||||
}
|
||||
|
||||
// Check timezone is UTC
|
||||
if timezone, ok := dataMap["timezone"].(string); ok {
|
||||
if timezone != "UTC" {
|
||||
t.Errorf("handler returned wrong timezone: got %v want %v",
|
||||
timezone, "UTC")
|
||||
}
|
||||
} else {
|
||||
t.Errorf("handler returned timezone of unexpected type")
|
||||
}
|
||||
|
||||
// Check server_time format (should be RFC3339)
|
||||
if serverTime, ok := dataMap["server_time"].(string); ok {
|
||||
if _, err := time.Parse(time.RFC3339, serverTime); err != nil {
|
||||
t.Errorf("handler returned invalid server_time format: got %v, error: %v",
|
||||
serverTime, err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("handler returned server_time of unexpected type")
|
||||
}
|
||||
|
||||
// Check unix_timestamp is a number
|
||||
if unixTimestamp, ok := dataMap["unix_timestamp"].(float64); ok {
|
||||
// Verify it's a reasonable timestamp (should be close to current time)
|
||||
now := time.Now().UTC().Unix()
|
||||
// Allow 10 seconds difference for test execution time
|
||||
if diff := now - int64(unixTimestamp); diff > 10 || diff < -10 {
|
||||
t.Errorf("handler returned unreasonable unix_timestamp: got %v, current time: %v",
|
||||
int64(unixTimestamp), now)
|
||||
}
|
||||
} else {
|
||||
// Try as string and convert
|
||||
if unixTimestampStr, ok := dataMap["unix_timestamp"].(string); ok {
|
||||
if _, err := strconv.ParseInt(unixTimestampStr, 10, 64); err != nil {
|
||||
t.Errorf("handler returned unix_timestamp of unexpected format: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("handler returned unix_timestamp of unexpected type")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeHandler_ResponseStructure(t *testing.T) {
|
||||
handler := NewTimeHandler()
|
||||
req := httptest.NewRequest("GET", "/time", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Test that response is valid JSON and contains all expected top-level fields
|
||||
var response models.Response
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Response is not valid JSON: %v", err)
|
||||
}
|
||||
|
||||
// Test response structure
|
||||
if response.Success != true {
|
||||
t.Errorf("Expected success=true, got success=%v", response.Success)
|
||||
}
|
||||
|
||||
if response.Error != "" {
|
||||
t.Errorf("Expected no error, got error=%v", response.Error)
|
||||
}
|
||||
|
||||
if response.Data == nil {
|
||||
t.Errorf("Expected data to be present, got nil")
|
||||
}
|
||||
|
||||
// Test that response body contains JSON structure
|
||||
body := rr.Body.String()
|
||||
if !strings.Contains(body, `"success":true`) {
|
||||
t.Errorf("Response body missing success field: %s", body)
|
||||
}
|
||||
|
||||
if !strings.Contains(body, `"data"`) {
|
||||
t.Errorf("Response body missing data field: %s", body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user