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.

68 lines
1.7 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/gorilla/mux"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestIndexRoute(t *testing.T) {
  11. t.Run("Index route returns valid response", func(t *testing.T) {
  12. request := httptest.NewRequest(http.MethodGet, "/", nil)
  13. responseRecorder := httptest.NewRecorder()
  14. IndexHandler(responseRecorder, request)
  15. assert.Equal(t, responseRecorder.Code, 200, "Response code should be 200")
  16. assert.NotNil(t, responseRecorder.Body, "Response should contain body")
  17. })
  18. }
  19. func TestEndpointRoute(t *testing.T) {
  20. // TODO: Use mock/fake for dummy pipeline data
  21. givenEndpoint := Pipeline{
  22. Name: "example pipeline",
  23. Slug: "example",
  24. Type: 0,
  25. RemoveMetadata: false,
  26. Steps: []Step{
  27. {
  28. Name: "resize image",
  29. Type: 0,
  30. },
  31. {
  32. Name: "compress image",
  33. Type: 1,
  34. },
  35. },
  36. }
  37. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  38. router := mux.NewRouter()
  39. RegisterPipelineRoutes(router, []IPipeline{givenEndpoint})
  40. request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil)
  41. responseRecorder := httptest.NewRecorder()
  42. router.ServeHTTP(responseRecorder, request)
  43. assert.Equal(t, responseRecorder.Code, 200)
  44. body, _ := json.Marshal(givenEndpoint)
  45. assert.JSONEq(t, string(body), responseRecorder.Body.String())
  46. })
  47. t.Run("Unregistered pipelines return 404", func(t *testing.T) {
  48. router := mux.NewRouter()
  49. request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil)
  50. responseRecorder := httptest.NewRecorder()
  51. router.ServeHTTP(responseRecorder, request)
  52. assert.Equal(t, responseRecorder.Code, 404)
  53. })
  54. }