server_test.go 689 B

1234567891011121314151617181920212223242526272829303132
  1. package server
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "golang.org/x/net/context"
  7. )
  8. func TestMiddlewares(t *testing.T) {
  9. cfg := &Config{}
  10. srv := &Server{
  11. cfg: cfg,
  12. }
  13. req, _ := http.NewRequest("GET", "/containers/json", nil)
  14. resp := httptest.NewRecorder()
  15. ctx := context.Background()
  16. localHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  17. if versionFromContext(ctx) == "" {
  18. t.Fatalf("Expected version, got empty string")
  19. }
  20. return nil
  21. }
  22. handlerFunc := srv.handleWithGlobalMiddlewares(localHandler)
  23. if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil {
  24. t.Fatal(err)
  25. }
  26. }