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.5 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/geplauder/lithium/pipelines"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/bxcodec/faker/v3"
  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, responseRecorder.Code, 200, "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. if err != nil {
  26. fmt.Println(err)
  27. }
  28. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  29. router := mux.NewRouter()
  30. RegisterPipelineRoutes(router, []pipelines.IPipeline{data})
  31. request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
  32. responseRecorder := httptest.NewRecorder()
  33. router.ServeHTTP(responseRecorder, request)
  34. assert.Equal(t, responseRecorder.Code, 200)
  35. body, _ := json.Marshal(data)
  36. assert.JSONEq(t, string(body), responseRecorder.Body.String())
  37. })
  38. t.Run("Unregistered pipelines return 404", func(t *testing.T) {
  39. router := mux.NewRouter()
  40. request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
  41. responseRecorder := httptest.NewRecorder()
  42. router.ServeHTTP(responseRecorder, request)
  43. assert.Equal(t, responseRecorder.Code, 404)
  44. })
  45. }