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.

119 lines
3.5 KiB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/bxcodec/faker/v3"
  10. "github.com/geplauder/lithium/pipelines"
  11. "github.com/geplauder/lithium/storage"
  12. "github.com/gorilla/mux"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestIndexRoute(t *testing.T) {
  16. t.Run("Index route returns valid response", func(t *testing.T) {
  17. request := httptest.NewRequest(http.MethodGet, "/", nil)
  18. responseRecorder := httptest.NewRecorder()
  19. IndexHandler(responseRecorder, request)
  20. assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200")
  21. assert.NotNil(t, responseRecorder.Body, "Response should contain body")
  22. })
  23. }
  24. func TestEndpointRoute(t *testing.T) {
  25. data := pipelines.Pipeline{}
  26. err := faker.FakeData(&data)
  27. assert.Nil(t, err)
  28. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  29. router := mux.NewRouter()
  30. fs := storage.GetMemoryStorageProvider()
  31. RegisterRoutes(router, []pipelines.IPipeline{data}, fs)
  32. request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
  33. responseRecorder := httptest.NewRecorder()
  34. router.ServeHTTP(responseRecorder, request)
  35. assert.Equal(t, 200, responseRecorder.Code)
  36. body, _ := json.Marshal(data)
  37. assert.JSONEq(t, string(body), responseRecorder.Body.String())
  38. })
  39. t.Run("Unregistered pipelines return 404", func(t *testing.T) {
  40. router := mux.NewRouter()
  41. request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
  42. responseRecorder := httptest.NewRecorder()
  43. router.ServeHTTP(responseRecorder, request)
  44. assert.Equal(t, 404, responseRecorder.Code)
  45. })
  46. }
  47. func TestUploadRoute(t *testing.T) {
  48. t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
  49. router := mux.NewRouter()
  50. fs := storage.GetMemoryStorageProvider()
  51. RegisterRoutes(router, []pipelines.IPipeline{pipelines.Pipeline{
  52. Name: "",
  53. Slug: "",
  54. Type: 0,
  55. RemoveMetadata: false,
  56. Steps: []pipelines.Step{},
  57. Output: struct {
  58. Format string `json:"format"`
  59. Quality int `json:"quality"`
  60. }{"jpeg", 10},
  61. }}, fs)
  62. request, _ := http.NewRequest("POST", "/upload", nil)
  63. request.Header["Content-Type"] = []string{"multipart/form-data"}
  64. responseRecorder := httptest.NewRecorder()
  65. router.ServeHTTP(responseRecorder, request)
  66. assert.Equal(t, 0x1A6, responseRecorder.Code)
  67. str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5" +
  68. "vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==")
  69. assert.JSONEq(t, string(str), responseRecorder.Body.String())
  70. })
  71. t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
  72. router := mux.NewRouter()
  73. fs := storage.GetMemoryStorageProvider()
  74. RegisterRoutes(router, []pipelines.IPipeline{pipelines.Pipeline{
  75. Name: "",
  76. Slug: "",
  77. Type: 0,
  78. RemoveMetadata: false,
  79. Steps: []pipelines.Step{},
  80. Output: struct {
  81. Format string `json:"format"`
  82. Quality int `json:"quality"`
  83. }{"jpeg", 10},
  84. }}, fs)
  85. request, _ := http.NewRequest("POST", "/upload", nil)
  86. request.Header["Content-Type"] = []string{"multipart/form-data", "boundary=X-INSOMNIA-BOUNDARY"}
  87. responseRecorder := httptest.NewRecorder()
  88. router.ServeHTTP(responseRecorder, request)
  89. assert.Equal(t, 0x1A6, responseRecorder.Code)
  90. str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==")
  91. assert.JSONEq(t, string(str), responseRecorder.Body.String())
  92. fmt.Println(responseRecorder.Body.String())
  93. })
  94. }