2018-02-05 16:05:59 -05:00
|
|
|
package server // import "github.com/docker/docker/api/server"
|
2015-09-15 19:01:49 -04:00
|
|
|
|
|
|
|
import (
|
2018-05-19 07:38:54 -04:00
|
|
|
"context"
|
2015-09-15 19:01:49 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-03-23 18:30:06 -04:00
|
|
|
"strings"
|
2015-09-15 19:01:49 -04:00
|
|
|
"testing"
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
2015-09-15 19:01:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewares(t *testing.T) {
|
2016-03-23 18:30:06 -04:00
|
|
|
cfg := &Config{
|
|
|
|
Version: "0.1omega2",
|
|
|
|
}
|
2015-09-15 19:01:49 -04:00
|
|
|
srv := &Server{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:56:54 -04:00
|
|
|
srv.UseMiddleware(middleware.NewVersionMiddleware("0.1omega2", api.DefaultVersion, api.MinVersion))
|
2016-04-08 19:22:39 -04:00
|
|
|
|
2015-09-15 19:01:49 -04:00
|
|
|
req, _ := http.NewRequest("GET", "/containers/json", nil)
|
|
|
|
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 19:42:08 -04:00
|
|
|
if httputils.VersionFromContext(ctx) == "" {
|
2017-02-07 22:22:21 -05:00
|
|
|
t.Fatal("Expected version, got empty string")
|
2015-09-15 19:01:49 -04:00
|
|
|
}
|
2016-03-23 18:30:06 -04: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 19:01:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:50:22 -04:00
|
|
|
handlerFunc := srv.handlerWithGlobalMiddlewares(localHandler)
|
2015-09-15 19:01:49 -04:00
|
|
|
if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|