version_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "runtime"
  8. "testing"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/api/server/httputils"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestVersionMiddlewareVersion(t *testing.T) {
  15. expectedVersion := "<not set>"
  16. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  17. v := httputils.VersionFromContext(ctx)
  18. assert.Check(t, is.Equal(expectedVersion, v))
  19. return nil
  20. }
  21. m := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
  22. h := m.WrapHandler(handler)
  23. req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
  24. resp := httptest.NewRecorder()
  25. ctx := context.Background()
  26. tests := []struct {
  27. reqVersion string
  28. expectedVersion string
  29. errString string
  30. }{
  31. {
  32. expectedVersion: api.DefaultVersion,
  33. },
  34. {
  35. reqVersion: api.MinSupportedAPIVersion,
  36. expectedVersion: api.MinSupportedAPIVersion,
  37. },
  38. {
  39. reqVersion: "0.1",
  40. errString: fmt.Sprintf("client version 0.1 is too old. Minimum supported API version is %s, please upgrade your client to a newer version", api.MinSupportedAPIVersion),
  41. },
  42. {
  43. reqVersion: "9999.9999",
  44. errString: fmt.Sprintf("client version 9999.9999 is too new. Maximum supported API version is %s", api.DefaultVersion),
  45. },
  46. }
  47. for _, test := range tests {
  48. expectedVersion = test.expectedVersion
  49. err := h(ctx, resp, req, map[string]string{"version": test.reqVersion})
  50. if test.errString != "" {
  51. assert.Check(t, is.Error(err, test.errString))
  52. } else {
  53. assert.Check(t, err)
  54. }
  55. }
  56. }
  57. func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
  58. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  59. v := httputils.VersionFromContext(ctx)
  60. assert.Check(t, len(v) != 0)
  61. return nil
  62. }
  63. m := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
  64. h := m.WrapHandler(handler)
  65. req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
  66. resp := httptest.NewRecorder()
  67. ctx := context.Background()
  68. vars := map[string]string{"version": "0.1"}
  69. err := h(ctx, resp, req, vars)
  70. assert.Check(t, is.ErrorContains(err, ""))
  71. hdr := resp.Result().Header
  72. assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/1.2.3"))
  73. assert.Check(t, is.Contains(hdr.Get("Server"), runtime.GOOS))
  74. assert.Check(t, is.Equal(hdr.Get("API-Version"), api.DefaultVersion))
  75. assert.Check(t, is.Equal(hdr.Get("OSType"), runtime.GOOS))
  76. }