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.

62 lines
1.6 KiB

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