package main import ( "bytes" "fmt" "net/http" "net/http/httptest" "testing" ) var apiTests = []struct { path string method string username string password string payload string wantResponseCode int }{ {"/deploy", "POST", "testuser", "testpass", `[{"app":"busybox", "version": "latest"}]`, 202}, {"/deploy", "POST", "testuser", "testpass", `[invalid{"app":"foo", "version": "bar"}]`, 400}, {"/deploy", "GET", "testuser", "", "", 401}, {"/deploy", "GET", "", "", "", 401}, {"/deploy", "GET", "testuser", "testpass", "", 405}, } func TestDeploymentAgentServer(t *testing.T) { handler := http.HandlerFunc(newDeploymentAgentServer().deploymentHandler) inputQueue = new(DeploymentQueue) processQueue = new(DeploymentQueue) go processDeploymentSpecs(&podmanClient{}, &http.Client{}, "http://localhost:8090/v1.0.0/libpod/") basicAuthUsername = "testuser" basicAuthPassword = "testpass" for _, test := range apiTests { t.Run(fmt.Sprintf("%q returns %d on %s with payload %q authenticated as %s:%s", test.path, test.wantResponseCode, test.method, test.payload, test.username, test.password), func(t *testing.T) { payload := []byte(test.payload) request, _ := http.NewRequest(test.method, test.path, bytes.NewBuffer(payload)) request.SetBasicAuth(test.username, test.password) response := httptest.NewRecorder() handler.ServeHTTP(response, request) assertStatus(t, response.Code, test.wantResponseCode) }) } } func assertStatus(t *testing.T, got, want int) { t.Helper() if got != want { t.Errorf("got %d, want %d", got, want) } }