48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package tests
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"Tasker_API/internal/routes"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestPing(t *testing.T) {
|
|
// Set Gin into test mode
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
// Initialize router with routes
|
|
router := gin.Default()
|
|
routes.InitializeRoutes(router)
|
|
|
|
// Create a test HTTP request for the /ping endpoint
|
|
req, err := http.NewRequest("GET", "/ping", nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create request: %v", err)
|
|
}
|
|
|
|
// Use httptest to record the response
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
// Check for 200 OK status
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Expected status code 200, got %d", w.Code)
|
|
}
|
|
|
|
// Parse the JSON response body
|
|
var response map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("Failed to parse JSON response: %v", err)
|
|
}
|
|
|
|
// Check for the expected message
|
|
if msg, exists := response["message"]; !exists || msg != "pong" {
|
|
t.Errorf("Expected message 'pong', got '%v'", response["message"])
|
|
}
|
|
}
|