github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/test/http/http.utils_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/go-chi/chi"
     8  )
     9  
    10  func TestRouteExists(t *testing.T) {
    11  	// Create a chi.Mux router
    12  	router := chi.NewRouter()
    13  
    14  	// Register some routes
    15  	router.Get("/authentications/otp/{phone}", func(w http.ResponseWriter, r *http.Request) {})
    16  	router.Post("/users/{userID}", func(w http.ResponseWriter, r *http.Request) {})
    17  	router.Get("/products/{productID}", func(w http.ResponseWriter, r *http.Request) {})
    18  
    19  	// Test if the routes exist
    20  	if !IsRouteExists(router, "/authentications/otp/{phone}") {
    21  		t.Errorf("Expected route '/authentications/otp/{phone}' not found in the router")
    22  	}
    23  
    24  	if !IsRouteExists(router, "/users/{userID}") {
    25  		t.Errorf("Expected route '/users/{userID}' not found in the router")
    26  	}
    27  
    28  	if !IsRouteExists(router, "/products/{productID}") {
    29  		t.Errorf("Expected route '/products/{productID}' not found in the router")
    30  	}
    31  
    32  	if IsRouteExists(router, "/nonexistent/route") {
    33  		t.Errorf("Unexpected route '/nonexistent/route' found in the router")
    34  	}
    35  }