You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.6 KiB
62 lines
1.6 KiB
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/bxcodec/faker/v3"
|
|
"github.com/geplauder/lithium/pipelines"
|
|
"github.com/geplauder/lithium/storage"
|
|
"github.com/gorilla/mux"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIndexRoute(t *testing.T) {
|
|
t.Run("Index route returns valid response", func(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
IndexHandler(responseRecorder, request)
|
|
|
|
assert.Equal(t, responseRecorder.Code, 200, "Response code should be 200")
|
|
assert.NotNil(t, responseRecorder.Body, "Response should contain body")
|
|
})
|
|
}
|
|
|
|
func TestEndpointRoute(t *testing.T) {
|
|
data := pipelines.Pipeline{}
|
|
err := faker.FakeData(&data)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
t.Run("Registered pipelines are valid routes", func(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
fs := storage.GetMemoryStorageProvider()
|
|
|
|
RegisterPipelineRoutes(router, []pipelines.IPipeline{data}, fs)
|
|
|
|
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(responseRecorder, request)
|
|
|
|
assert.Equal(t, responseRecorder.Code, 200)
|
|
body, _ := json.Marshal(data)
|
|
assert.JSONEq(t, string(body), responseRecorder.Body.String())
|
|
})
|
|
|
|
t.Run("Unregistered pipelines return 404", func(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
|
|
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(responseRecorder, request)
|
|
|
|
assert.Equal(t, responseRecorder.Code, 404)
|
|
})
|
|
}
|