4
0

restapi_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package httpservers
  2. /*
  3. The REST API actually has very few tests, as the "real" API behind OliveTin
  4. is is implemented as a gRPC in /internal/grpc. The REST API therefore only
  5. handles HTTP specific stuff like authentication cookies and JWT parsing.
  6. */
  7. import (
  8. "fmt"
  9. "github.com/OliveTin/OliveTin/internal/cors"
  10. "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  11. "net"
  12. "net/http"
  13. "testing"
  14. )
  15. func setupTestingServer(mux *runtime.ServeMux, t *testing.T) *http.Server {
  16. lis, err := net.Listen("tcp", ":1337")
  17. if err != nil || lis == nil {
  18. t.Errorf("Could not listen %v %v", err, lis)
  19. return nil
  20. }
  21. srv := &http.Server{Handler: cors.AllowCors(mux)}
  22. go startTestingServer(lis, srv, t)
  23. return srv
  24. }
  25. func startTestingServer(lis net.Listener, srv *http.Server, t *testing.T) {
  26. if srv == nil {
  27. t.Errorf("srv is nil. Could not listen")
  28. return
  29. }
  30. go func() {
  31. if err := srv.Serve(lis); err != nil {
  32. fmt.Printf("couldn't start server: %+v", err)
  33. }
  34. }()
  35. }
  36. func newReq(path string) (*http.Request, *http.Client) {
  37. client := &http.Client{}
  38. req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:1337/%v", path), nil)
  39. return req, client
  40. }