52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestHealthCheck(t *testing.T) {
|
|
// Create a request to pass to our handler.
|
|
req, err := http.NewRequest("GET", "/api/health", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
|
|
rr := httptest.NewRecorder()
|
|
|
|
// Initialize Router
|
|
router := NewRouter()
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// Check the status code is what we expect.
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
// Check the response body is what we expect.
|
|
expected := "ok"
|
|
if rr.Body.String() != expected {
|
|
t.Errorf("handler returned unexpected body: got %v want %v",
|
|
rr.Body.String(), expected)
|
|
}
|
|
}
|
|
|
|
func TestSearchValidation(t *testing.T) {
|
|
// Test missing query parameter
|
|
req, err := http.NewRequest("GET", "/api/search", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler := http.HandlerFunc(SearchTracks)
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusBadRequest {
|
|
t.Errorf("handler returned wrong status code for missing query: got %v want %v",
|
|
status, http.StatusBadRequest)
|
|
}
|
|
}
|