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.

58 lines
1.4 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/gorilla/mux"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestIndexRoute(t *testing.T) {
  13. t.Run("Index route returns valid response", func(t *testing.T) {
  14. request := httptest.NewRequest(http.MethodGet, "/", nil)
  15. responseRecorder := httptest.NewRecorder()
  16. IndexHandler(responseRecorder, request)
  17. assert.Equal(t, responseRecorder.Code, 200, "Response code should be 200")
  18. assert.NotNil(t, responseRecorder.Body, "Response should contain body")
  19. })
  20. }
  21. func TestEndpointRoute(t *testing.T) {
  22. data := Pipeline{}
  23. err := faker.FakeData(&data)
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  28. router := mux.NewRouter()
  29. RegisterPipelineRoutes(router, []IPipeline{data})
  30. request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
  31. responseRecorder := httptest.NewRecorder()
  32. router.ServeHTTP(responseRecorder, request)
  33. assert.Equal(t, responseRecorder.Code, 200)
  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, responseRecorder.Code, 404)
  43. })
  44. }