2022-08-30 02:30:36 +00:00
package v1
import (
2022-09-04 02:42:03 +00:00
"net/http"
"net/http/httptest"
2022-08-30 02:30:36 +00:00
"testing"
2022-09-14 04:06:07 +00:00
"github.com/hay-kot/content/backend/internal/types"
2022-08-30 02:30:36 +00:00
"github.com/stretchr/testify/assert"
)
func Test_NewHandlerV1 ( t * testing . T ) {
v1Base := BaseUrlFunc ( "/testing/v1" )
2022-09-03 18:38:35 +00:00
ctrl := NewControllerV1 ( mockHandler . svc )
2022-08-30 02:30:36 +00:00
assert . NotNil ( t , ctrl )
assert . Equal ( t , "/testing/v1/v1/abc123" , v1Base ( "/abc123" ) )
assert . Equal ( t , "/testing/v1/v1/abc123" , v1Base ( "/abc123" ) )
}
2022-09-04 02:42:03 +00:00
func TestHandlersv1_HandleBase ( t * testing . T ) {
// Setup
2022-09-14 04:06:07 +00:00
hdlrFunc := mockHandler . HandleBase ( func ( ) bool { return true } , types . Build {
Version : "0.1.0" ,
Commit : "HEAD" ,
BuildTime : "now" ,
} )
2022-09-04 02:42:03 +00:00
// Call Handler Func
rr := httptest . NewRecorder ( )
hdlrFunc ( rr , nil )
// Validate Status Code
if rr . Code != http . StatusOK {
t . Errorf ( "Expected status code to be %d, got %d" , http . StatusOK , rr . Code )
}
// Validate Json Payload
2022-09-14 17:35:23 +00:00
expected := ` { "health":true,"versions":null,"title":"Go API Template","message":"Welcome to the Go API Template Application!","Build": { "version":"0.1.0","commit":"HEAD","buildTime":"now"}} `
2022-09-04 02:42:03 +00:00
if rr . Body . String ( ) != expected {
t . Errorf ( "Expected json to be %s, got %s" , expected , rr . Body . String ( ) )
}
}