2018-02-05 21:05:59 +00:00
|
|
|
package server // import "github.com/docker/docker/api/server"
|
2015-09-15 23:01:49 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-19 11:38:54 +00:00
|
|
|
"context"
|
2015-09-15 23:01:49 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-03-23 22:30:06 +00:00
|
|
|
"strings"
|
2015-09-15 23:01:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-04-08 23:22:39 +00:00
|
|
|
"github.com/docker/docker/api"
|
2015-09-23 23:42:08 +00:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-04-08 23:22:39 +00:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
2015-09-15 23:01:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewares(t *testing.T) {
|
2022-08-17 20:46:26 +00:00
|
|
|
srv := &Server{}
|
2015-09-15 23:01:49 +00:00
|
|
|
|
2024-01-22 17:38:59 +00:00
|
|
|
m, err := middleware.NewVersionMiddleware("0.1omega2", api.DefaultVersion, api.MinSupportedAPIVersion)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srv.UseMiddleware(*m)
|
2016-04-08 23:22:39 +00:00
|
|
|
|
2019-10-12 18:40:19 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
|
2015-09-15 23:01:49 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 23:42:08 +00:00
|
|
|
if httputils.VersionFromContext(ctx) == "" {
|
2017-02-08 03:22:21 +00:00
|
|
|
t.Fatal("Expected version, got empty string")
|
2015-09-15 23:01:49 +00:00
|
|
|
}
|
2016-03-23 22:30:06 +00:00
|
|
|
|
|
|
|
if sv := w.Header().Get("Server"); !strings.Contains(sv, "Docker/0.1omega2") {
|
|
|
|
t.Fatalf("Expected server version in the header `Docker/0.1omega2`, got %s", sv)
|
|
|
|
}
|
|
|
|
|
2015-09-15 23:01:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 02:50:22 +00:00
|
|
|
handlerFunc := srv.handlerWithGlobalMiddlewares(localHandler)
|
2015-09-15 23:01:49 +00:00
|
|
|
if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|