2018-02-05 16:05:59 -05:00
|
|
|
package authorization // import "github.com/docker/docker/pkg/authorization"
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2017-06-15 04:05:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddleware(t *testing.T) {
|
|
|
|
pluginNames := []string{"testPlugin1", "testPlugin2"}
|
|
|
|
var pluginGetter plugingetter.PluginGetter
|
|
|
|
m := NewMiddleware(pluginNames, pluginGetter)
|
|
|
|
authPlugins := m.getAuthzPlugins()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, 2, len(authPlugins))
|
|
|
|
assert.Equal(t, pluginNames[0], authPlugins[0].Name())
|
|
|
|
assert.Equal(t, pluginNames[1], authPlugins[1].Name())
|
2017-06-15 04:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResponseModifier(t *testing.T) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
modifier := NewResponseModifier(recorder)
|
|
|
|
modifier.Header().Set("H1", "V1")
|
|
|
|
modifier.Write([]byte("body"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, !modifier.Hijacked())
|
2017-06-15 04:05:40 -04:00
|
|
|
modifier.WriteHeader(http.StatusInternalServerError)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, modifier.RawBody() != nil)
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
raw, err := modifier.RawHeaders()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, raw != nil)
|
|
|
|
assert.NilError(t, err)
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
headerData := strings.Split(strings.TrimSpace(string(raw)), ":")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, "H1", strings.TrimSpace(headerData[0]))
|
|
|
|
assert.Equal(t, "V1", strings.TrimSpace(headerData[1]))
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
modifier.Flush()
|
|
|
|
modifier.FlushAll()
|
|
|
|
|
|
|
|
if recorder.Header().Get("H1") != "V1" {
|
|
|
|
t.Fatalf("Header value must exists %s", recorder.Header().Get("H1"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAuthzPlugins(m *Middleware, plugins []Plugin) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.plugins = plugins
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|