|
| 1 | +package builtins_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/docker/docker-agent/pkg/hooks" |
| 15 | + "github.com/docker/docker-agent/pkg/hooks/builtins" |
| 16 | +) |
| 17 | + |
| 18 | +// TestHTTPPostSendsBodyToURL pins the happy path: POST with body |
| 19 | +// and Content-Type: application/json, and a nil Output. |
| 20 | +func TestHTTPPostSendsBodyToURL(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + const payload = `{"event":"turn_start"}` |
| 24 | + |
| 25 | + var ( |
| 26 | + gotMethod string |
| 27 | + gotContentType string |
| 28 | + gotBody []byte |
| 29 | + ) |
| 30 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + gotMethod = r.Method |
| 32 | + gotContentType = r.Header.Get("Content-Type") |
| 33 | + gotBody, _ = io.ReadAll(r.Body) |
| 34 | + w.WriteHeader(http.StatusNoContent) |
| 35 | + })) |
| 36 | + t.Cleanup(srv.Close) |
| 37 | + |
| 38 | + fn := lookup(t, builtins.HTTPPost) |
| 39 | + |
| 40 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, []string{srv.URL, payload}) |
| 41 | + require.NoError(t, err) |
| 42 | + assert.Nil(t, out) |
| 43 | + |
| 44 | + assert.Equal(t, http.MethodPost, gotMethod) |
| 45 | + assert.Equal(t, "application/json", gotContentType) |
| 46 | + assert.JSONEq(t, payload, string(gotBody)) |
| 47 | +} |
| 48 | + |
| 49 | +// TestHTTPPostEmptyBodyIsAllowed: omitting the second arg sends an |
| 50 | +// empty body — useful for ping-style webhooks. |
| 51 | +func TestHTTPPostEmptyBodyIsAllowed(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + |
| 54 | + var gotBody []byte |
| 55 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 56 | + gotBody, _ = io.ReadAll(r.Body) |
| 57 | + w.WriteHeader(http.StatusOK) |
| 58 | + })) |
| 59 | + t.Cleanup(srv.Close) |
| 60 | + |
| 61 | + fn := lookup(t, builtins.HTTPPost) |
| 62 | + |
| 63 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, []string{srv.URL}) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Nil(t, out) |
| 66 | + assert.Empty(t, gotBody) |
| 67 | +} |
| 68 | + |
| 69 | +// TestHTTPPostNoOpWithoutURL: a missing or empty URL is a no-op so |
| 70 | +// a misconfigured YAML doesn't break the run loop. |
| 71 | +func TestHTTPPostNoOpWithoutURL(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + fn := lookup(t, builtins.HTTPPost) |
| 75 | + |
| 76 | + cases := [][]string{ |
| 77 | + nil, |
| 78 | + {}, |
| 79 | + {""}, |
| 80 | + {"", "body"}, |
| 81 | + } |
| 82 | + for _, args := range cases { |
| 83 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, args) |
| 84 | + require.NoErrorf(t, err, "args=%v: must not error", args) |
| 85 | + assert.Nilf(t, out, "args=%v: must be a no-op", args) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestHTTPPostSwallowsErrors: neither a non-2xx response nor an |
| 90 | +// unreachable receiver propagates as a hook error. |
| 91 | +func TestHTTPPostSwallowsErrors(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + serverError := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 95 | + w.WriteHeader(http.StatusInternalServerError) |
| 96 | + })) |
| 97 | + t.Cleanup(serverError.Close) |
| 98 | + |
| 99 | + // Bind, capture URL, then close: the port is now guaranteed-unreachable. |
| 100 | + unreachable := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) |
| 101 | + unreachableURL := unreachable.URL |
| 102 | + unreachable.Close() |
| 103 | + |
| 104 | + cases := map[string]string{ |
| 105 | + "non-2xx response": serverError.URL, |
| 106 | + "unreachable receiver": unreachableURL, |
| 107 | + } |
| 108 | + |
| 109 | + fn := lookup(t, builtins.HTTPPost) |
| 110 | + for name, url := range cases { |
| 111 | + t.Run(name, func(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, []string{url, "{}"}) |
| 114 | + require.NoError(t, err) |
| 115 | + assert.Nil(t, out) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestHTTPPostRejectsNonHTTPSchemes: file://, ftp://, javascript: and |
| 121 | +// scheme-less or host-less inputs all surface as a config error |
| 122 | +// rather than being silently dispatched to a transport. |
| 123 | +func TestHTTPPostRejectsNonHTTPSchemes(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + |
| 126 | + fn := lookup(t, builtins.HTTPPost) |
| 127 | + |
| 128 | + cases := []string{ |
| 129 | + "file:///etc/passwd", |
| 130 | + "ftp://example.com/", |
| 131 | + "javascript:alert(1)", |
| 132 | + "not-a-url", |
| 133 | + "http://", |
| 134 | + "http://\x7f\x00.example", |
| 135 | + } |
| 136 | + for _, raw := range cases { |
| 137 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, []string{raw, "{}"}) |
| 138 | + require.Errorf(t, err, "input %q must be rejected", raw) |
| 139 | + assert.Nil(t, out) |
| 140 | + assert.Contains(t, err.Error(), "http_post:") |
| 141 | + assert.Contains(t, err.Error(), "http(s)") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestHTTPPostHonoursContextCancellation: the request returns |
| 146 | +// promptly after ctx deadline instead of waiting for the handler. |
| 147 | +func TestHTTPPostHonoursContextCancellation(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + |
| 150 | + // Bounded sleep so the client must abandon before the response, |
| 151 | + // while keeping httptest.Server.Close() cleanup quick. |
| 152 | + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 153 | + time.Sleep(300 * time.Millisecond) |
| 154 | + })) |
| 155 | + t.Cleanup(srv.Close) |
| 156 | + |
| 157 | + fn := lookup(t, builtins.HTTPPost) |
| 158 | + |
| 159 | + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Millisecond) |
| 160 | + defer cancel() |
| 161 | + |
| 162 | + start := time.Now() |
| 163 | + out, err := fn(ctx, &hooks.Input{SessionID: "s"}, []string{srv.URL, "{}"}) |
| 164 | + elapsed := time.Since(start) |
| 165 | + |
| 166 | + // Network errors (incl. cancellation) are swallowed by design. |
| 167 | + require.NoError(t, err) |
| 168 | + assert.Nil(t, out) |
| 169 | + assert.Less(t, elapsed, 250*time.Millisecond) |
| 170 | +} |
0 commit comments