version_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package middleware
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/server/httputils"
  8. "github.com/docker/docker/pkg/version"
  9. "golang.org/x/net/context"
  10. )
  11. func TestVersionMiddleware(t *testing.T) {
  12. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  13. if httputils.VersionFromContext(ctx) == "" {
  14. t.Fatalf("Expected version, got empty string")
  15. }
  16. return nil
  17. }
  18. defaultVersion := version.Version("1.10.0")
  19. minVersion := version.Version("1.2.0")
  20. m := NewVersionMiddleware(defaultVersion.String(), defaultVersion, minVersion)
  21. h := m(handler)
  22. req, _ := http.NewRequest("GET", "/containers/json", nil)
  23. resp := httptest.NewRecorder()
  24. ctx := context.Background()
  25. if err := h(ctx, resp, req, map[string]string{}); err != nil {
  26. t.Fatal(err)
  27. }
  28. }
  29. func TestVersionMiddlewareWithErrors(t *testing.T) {
  30. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  31. if httputils.VersionFromContext(ctx) == "" {
  32. t.Fatalf("Expected version, got empty string")
  33. }
  34. return nil
  35. }
  36. defaultVersion := version.Version("1.10.0")
  37. minVersion := version.Version("1.2.0")
  38. m := NewVersionMiddleware(defaultVersion.String(), defaultVersion, minVersion)
  39. h := m(handler)
  40. req, _ := http.NewRequest("GET", "/containers/json", nil)
  41. resp := httptest.NewRecorder()
  42. ctx := context.Background()
  43. vars := map[string]string{"version": "0.1"}
  44. err := h(ctx, resp, req, vars)
  45. if !strings.Contains(err.Error(), "client version 0.1 is too old. Minimum supported API version is 1.2.0") {
  46. t.Fatalf("Expected ErrorCodeOldClientVersion, got %v", err)
  47. }
  48. vars["version"] = "100000"
  49. err = h(ctx, resp, req, vars)
  50. if !strings.Contains(err.Error(), "client is newer than server") {
  51. t.Fatalf("Expected ErrorCodeNewerClientVersion, got %v", err)
  52. }
  53. }