I'm trying to write a test that validates the CORS for my API are setup correctly. The API is written in Go and uses GIN. I have different CORS settings depending on router group. It looks like:
router := gin.New()
router.Use(gin.Recovery())
domainOneCORS := cors.New(cors.Config{
AllowOrigins: []string{"https://domainone.com"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowHeaders: []string{"Authorization", "Content-Length", "Content-Type", "Origin"},
ExposeHeaders: []string{"Content-Length"},
MaxAge: 12 * time.Hour,
})
domainTwoCORS := cors.New(cors.Config{
AllowOrigins: []string{"https://domaintwo.com"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowHeaders: []string{"Authorization", "Content-Length", "Content-Type", "Origin"},
ExposeHeaders: []string{"Content-Length"},
MaxAge: 12 * time.Hour,
})
domainOneAPI := router.Group("/one")
domainOneAPI.Use(domainOneCORS)
{
domainOneAPI.GET("", handler.DomainOneHealth)
}
domainTwoAPI := router.Group("/two")
domainTwoAPI.Use(domainTwoCORS)
{
domainTwoAPI.GET("", handler.DomainTwoHealth)
}
I'm trying to figure out the best way to test my configuration. Most tests I've found use the handler and skip the router middleware portion. Does it make sense to test it? Even if I can mock out the router and response, it's only confirming a HTTP client can hit the endpoint. Not validating a web app on a different domain is able to hit the API. I found this issue which is similar but for echo.