Micro-service for file storage and processing written in Go
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.

59 lines
1.6 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/bxcodec/faker/v3"
  8. "github.com/geplauder/lithium/pipelines"
  9. "github.com/geplauder/lithium/storage"
  10. "github.com/gorilla/mux"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestIndexRoute(t *testing.T) {
  14. t.Run("Index route returns valid response", func(t *testing.T) {
  15. request := httptest.NewRequest(http.MethodGet, "/", nil)
  16. responseRecorder := httptest.NewRecorder()
  17. IndexHandler(responseRecorder, request)
  18. assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200")
  19. assert.NotNil(t, responseRecorder.Body, "Response should contain body")
  20. })
  21. }
  22. func TestEndpointRoute(t *testing.T) {
  23. data := pipelines.Pipeline{}
  24. err := faker.FakeData(&data)
  25. assert.Nil(t, err)
  26. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  27. router := mux.NewRouter()
  28. fs := storage.GetMemoryStorageProvider()
  29. RegisterRoutes(router, []pipelines.IPipeline{data}, fs)
  30. request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
  31. responseRecorder := httptest.NewRecorder()
  32. router.ServeHTTP(responseRecorder, request)
  33. assert.Equal(t, 200, responseRecorder.Code)
  34. body, _ := json.Marshal(data)
  35. assert.JSONEq(t, string(body), responseRecorder.Body.String())
  36. })
  37. t.Run("Unregistered pipelines return 404", func(t *testing.T) {
  38. router := mux.NewRouter()
  39. request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
  40. responseRecorder := httptest.NewRecorder()
  41. router.ServeHTTP(responseRecorder, request)
  42. assert.Equal(t, 404, responseRecorder.Code)
  43. })
  44. }