2015-09-15 19:01:49 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-23 19:42:08 -04:00
|
|
|
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
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) == "" {
|
2015-09-15 19:01:49 -04:00
|
|
|
t.Fatalf("Expected version, got empty string")
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerFunc := srv.handleWithGlobalMiddlewares(localHandler)
|
|
|
|
if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|